【问题标题】:Make delete remove four spaces at the beginning of the line (un-indent) in emacs在emacs中删除行首的四个空格(取消缩进)
【发布时间】:2012-01-16 21:45:52
【问题描述】:

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

例如,如果我有

|        _

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

|    _

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

编辑 2: 我认为我最初的问题让人们感到困惑。我想要这样的东西。假设我有

|        my text_

并且光标位于行尾(由_显示)。如果我输入 DEL,我想得到

|        my tex_

(显然)。但是如果我有

|        m_

然后我输入 DEL,我想要

|        _

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

|    _

换个角度想,我想把四个空格的制表符当作真正的制表符来处理,就删除键而言。

【问题讨论】:

  • 你在找M-m吗?

标签: emacs


【解决方案1】:

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

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

更新为在删除 char 和 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))))

【讨论】:

  • 嗯,这很接近我想要的。但是我想将它绑定到DEL,这样它就可以正常删除最后一个字符,但是如果该行之前的所有字符都是空格,则删除四个空格。
  • @asmeurer 已更新以做你想做的事。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-09-28
  • 1970-01-01
  • 2011-05-14
相关资源
最近更新 更多