如何使emacs更接近常规编辑器?

时间:2011-01-09 06:59:23

标签: emacs

我在Ubuntu上使用Emacs 23.1.1和Emacs starter kit。我主要在lua模式下工作。

有没有办法阻止Emacs对缩进这么聪明?我习惯了愚蠢的编辑,并手动按下所有必需的键。

我想每个缩进使用两个空格,制表符到空格。

当我按 RETURN 时,新行缩进必须与上一行匹配。

当我在前导空格上按 TAB 时,行内容必须缩进一个缩进单位。

当我在空行的开头按 TAB 时,光标必须向右移动一个缩进单位。

哦,我想在80 th 列上进行软文字换行,并在保存时修剪尾随空格。

更新

(会把它放在评论中,但需要格式化)

如果我使用Thomas的解决方案, RETURN 上的自动缩进是“固定的”,但 TAB 仍然有点缩进:

local run = function(...)
           x

“x”表示在我键入第一行并点击 RETURN TAB 后光标出现的位置。

2 个答案:

答案 0 :(得分:3)

Emacs有模式的概念,这意味着根据您正在编辑的文件类型,它提供了对该文件有用的特殊功能。每个缓冲区都有一个主要模式关联,可选择一些次要模式。

缩进是通常依赖于模式的事情之一。也就是说,您可能必须为每个主要模式单独配置缩进,因为否则在加载新文件时,其关联的主要模式可能会覆盖缩进设置。虽然可以编写一个函数来配置缩进并设置Emacs,但每当新的主模式启动时都会调用该函数。

为了实现您想要的设置,您需要运行几行elisp代码。 (不幸的是,当你点击TAB时你应该发生的事情的描述会遗漏一些细节,我已经实现了下面我想到的最简单的版本 - 如果它不是你想要的,那当然可以改变。)

将以下代码放在主目录(.emacs)中名为~的文件中:

(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)

这在Emacs 23中对我有用,虽然我可能错过了一些边缘情况。然而,这些变化是如此根本,我预测你迟早会遇到一些主要模式的不兼容性,这些主要模式期望缩进工作他们设置它。如果您真的想进入Emacs,那么将您从其他编辑器继承的习惯调整为Emacs的工作方式是值得的。

对于软自动换行,有一个名为“longlines”的小模式,你可以从这里下载:http://www.emacswiki.org/cgi-bin/emacs/download/longlines.el我没有用它,所以我不能告诉你它有多好用。

答案 1 :(得分:2)

修复TAB和RETURN:

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

填充栏(尚未尝试):说ESC x customize-var,输入fill-column,设置为80。

相关问题