使删除删除emacs中行开头(un-indent)的四个空格

时间:2012-01-16 21:45:52

标签: emacs

在emacs中,我希望删除删除行开头的四个空格,以便我可以轻松地取消缩进文本。我有TAB设置插入四个空格(在相关模式中),这将是有帮助的。

例如,如果我有

|        _

其中|代表行的开头(我必须添加它才能使markdown正确呈现),_代表光标,我按删除,我想得到< / p>

|    _

编辑:我刚刚发现这种情况已经发生在某些模式下,例如python-mode。

编辑2:我认为我原来的问题令人困惑。我想要这样的东西。假设我有

|        my text_

,光标位于行的末尾(由_表示)。如果我输入DEL,我想得到

|        my tex_

(明显)。但如果我有

|        m_

我键入DEL,我想要

|        _

如果我再次输入DEL ,我想要

|    _

另外想想另一种方法,就删除键而言,我想将四个空格的标签视为真正的标签。

1 个答案:

答案 0 :(得分:2)

这段代码怎么样,你可以绑定到你想要的任何代码:

(defun remove-indentation-spaces ()
  "remove TAB-WIDTH spaces from the beginning of this line"
  (interactive)
  (indent-rigidly (line-beginning-position) (line-end-position) (- tab-width)))

注意,如果tab-width与您想要的不匹配,请将其硬编码为-4。

如果你希望这个绑定到 DEL ,你可以这样做:

(global-set-key (kbd "DEL") 'remove-indentation-spaces)

或者,在适当的模式映射中定义它,如:

(define-key some-major-mode-map (kbd "DEL") 'remove-indentation-spaces)

更新为在删除字符和4个空格之间切换:

(defun remove-indentation-spaces ()
  "remove TAB-WIDTH spaces from the beginning of this line"
  (interactive)
  (if (save-excursion (re-search-backward "[^ \t]" (line-beginning-position) t))
      (delete-backward-char 1)
    (indent-rigidly (line-beginning-position) (line-end-position) (- tab-width))))
相关问题