开发者

How to make emacs behave closer to the regular editors?

I'm using Emacs 23.1.1 on Ubuntu with Emacs starter kit. I primarily work in the lua-mode.

Is there a way to stop Emacs being so smart about indentation? I'm used to the dumb editors, and press all the required keys manually.

I want to use two spaces per indent, tabs-to-spaces.

When I press RETURN, the new line indentation must match the previous line.

When I press TAB on the leading whitespace, the line contents must be开发者_JAVA百科 indented by one indentation unit.

When I press TAB on the beginning of empty line, the cursor must move one indentation unit to the right.

Oh, and I'd like to get soft word wrap on 80th column and trim-trailing-spaces on save as well.

Update:

(Would put this in a comment, but it needs formatting)

If I use Thomas's solution, auto-indent on RETURN is "fixed", but TAB still indents weirdly:

local run = function(...)
           x

"x" marks the spot where cursor appears after I type the first line and hit RETURN, TAB.


Emacs has a concept of modes, which means that depending on what type of file you're editing it provides special functionality that is useful for that file. Every buffer has one major mode associated and optionally a number of minor modes.

Indentation is one of the things that is typically mode-dependent. That is, you may have to configure indentation separately for every major-mode, because otherwise when you load a new file, its associated major mode may override your indentation settings. It's possible though to write a function that configures indentation and set up Emacs in a way that the function is invoked whenever a new major-mode is started.

In order to realize the settings you want, you'll need to run a few lines of elisp code. (Unfortunately your description of what should happen when you hit TAB leaves out some details, I've implemented the simplest version I could think of below -- if it's not what you want, that can be changed, of course.)

Put the following code in the file named .emacs in your home directory (~):

(setq-default indent-tabs-mode nil) ; use spaces for indentation

(defvar my-indentation-width 2
  "The number of spaces I prefer for line indentation.")

(defun my-enter ()
  "Inserts a newline character then indents the new line just
like the previous line"
  (interactive)
  (newline)
  (indent-relative-maybe))

(defun my-indent ()
  "When point is on leading white-space of a non-empty line, the
line is indented `my-indentation-width' spaces. If point is at
the beginning of an empty line, inserts `my-indentation-width'
spaces."
  (interactive)
  (insert (make-string my-indentation-width ? )))

(defun my-indentation-setup ()
  "Binds RETURN to the function `my-enter' and TAB to call
`my-indent'"
  (local-set-key "\r" 'my-enter)
  (setq indent-line-function 'my-indent))

(defun delete-trailing-whitespace-and-blank-lines ()
  "Deletes all whitespace at the end of a buffer (or, rather, a
buffer's accessible portion, see `Narrowing'), including blank
lines."
  (interactive)
  (let ((point (point)))
    (delete-trailing-whitespace)
    (goto-char (point-max))
    (delete-blank-lines)
    (goto-char (min point (point-max)))))

;; make sure trailing whitespace is removed every time a buffer is saved.
(add-hook 'before-save-hook 'delete-trailing-whitespace-and-blank-lines)

;; globally install my indentation setup
(global-set-key "\r" 'my-enter)
(setq indent-line-function 'my-indent)

;; also override key setting of major-modes, if any
(add-hook 'after-change-major-mode-hook 'my-indentation-setup)

This works for me in Emacs 23, although I may have missed some edge cases. However, these changes are so fundamental that I predict you will run into incompatibilities sooner or later with some major-modes that expect indentation to work they set it up. If you really want to get into Emacs it's worthwhile adapting the habits you inherited from other editors to the way Emacs does things.

For soft word-wrap there is a minor-mode called "longlines" which you can download from here: http://www.emacswiki.org/cgi-bin/emacs/download/longlines.el I haven't used it so I can't tell you how well it works.


Fixing TAB and RETURN:

(global-set-key "\t" 'self-insert-command)
(global-set-key "\r" 'newline-and-indent)

Fill column (haven't tried): say ESC x customize-var, enter fill-column, set to 80.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜