在emacs中,使delete删除行开头的四个空格(取消缩进)



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

例如,如果我有

|        _

其中|表示行的开头(我必须添加它才能使markdown正确渲染),_表示光标,我按delete键,我想得到

|    _

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

第2版:我觉得我最初的问题让人困惑。我想要这样的东西。假设我有

|        my text_

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

|        my tex_

(显然)。但如果我有

|        m_

我输入DEL,我想要

|        _

如果我再次键入DEL,我需要

|    _

换句话说,就删除键而言,我想将四个空格选项卡视为真正的选项卡。

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

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

最新更新