如何配置 emacs 以与其他现代编辑器相同的方式工作,按 Alt+D 或 Alt+Backspace 删除相邻的空格或单个单词?默认情况下,emacs 总是删除一个单词。
通过使用 Emacs 一段时间,我发现即使我可以更改基本功能,但通常在效率方面并没有多大回报。事实上,做了几次之后,我就后悔了,并取消了它。这并非总是如此,有些键绑定确实不舒服或很少有用,但我不认为kill word的工作原理是这样的。事实上,我现在才意识到:我确实尝试过 Eclipse 中的键绑定,但我一直将它与 Emacs 风格的键绑定一起使用......
无论如何,正如我刚才所说,在“修复”该功能之前,请确保它确实损坏了:)我从来没有发现自己需要您描述的那种功能,也许这就是原因:
M-SPC 将单词之间的间距减少到只有一个空格。如果点位于单词之间并且我想删除分隔单词的额外空格,这就是我会使用的。
M-\ 删除所有水平空间。这将连接两个由空格分隔的单词。
如果您想要实现某种“稀疏”格式,如下所示:
int foo = 42;
unsigned int bar = 43;
然后有 M-x
align-regexp
来做到这一点。
我只是从来没有碰巧有a)长时间连续的空白,除非它是缩进,并且在它是缩进的情况下,TAB通常可以更好地处理它。 b)即使有很长的后续空白,我也很少一次将点移动一个字符,因此很难想象我会发现该点被多个空白包围的情况。诸如艺术家模式或点图之类的东西浮现在脑海中,但在代码编辑过程中却不会发生。
最后,如果您尝试编辑任意文本文件,并且想要添加或删除单词之间的水平空格...同样,可以使用 M-x
align-regexp
来做到这一点,或者您也可以如果当时有几行,请使用对矩形进行操作的命令。好吧,当您点击 TAB 时,Emacs 甚至会识别临时选项卡,并尝试对齐文本,例如匹配该点之前的最后一行。最后,如果由于某种原因我无法理解:)我真的需要完全按照你所描述的那样做,那么我会这样做:kM-\BACKSPACE(它可以是任何其他键) “k”的 - 它就在你的手指下,所以输入速度很快:)或者,如果我懒得考虑它:M-SPCM-fM-bC-w - 也许听起来像很多,但无论如何这些都是您一直会使用的命令,因此它不会妨碍您的速度。
(defvar movement-syntax-table
(let ((st (make-syntax-table)))
;; ` default = punctuation
;; ' default = punctuation
;; , default = punctuation
;; ; default = punctuation
(modify-syntax-entry ?{ "." st) ;; { = punctuation
(modify-syntax-entry ?} "." st) ;; } = punctuation
(modify-syntax-entry ?\" "." st) ;; " = punctuation
(modify-syntax-entry ?\\ "_" st) ;; \ = symbol
(modify-syntax-entry ?\$ "_" st) ;; $ = symbol
(modify-syntax-entry ?\% "_" st) ;; % = symbol
st)
"Syntax table used while executing custom movement functions.")
(defun delete-word-or-whitespace (&optional arg)
"http://stackoverflow.com/a/20456861/2112489"
(interactive "P")
(with-syntax-table movement-syntax-table
(let* (
beg
end
(word-regexp "\\sw")
(punctuation-regexp "\\s.")
(symbol-regexp "\\s_\\|\\s(\\|\\s)"))
(cond
;; Condition # 1
;; right of cursor = word or punctuation or symbol
((or
(save-excursion (< 0 (skip-syntax-forward "w")))
(save-excursion (< 0 (skip-syntax-forward ".")))
(save-excursion (< 0 (skip-syntax-forward "_()"))))
;; Condition #1 -- Step 1 of 2
(cond
;; right of cursor = word
((save-excursion (< 0 (skip-syntax-forward "w")))
(skip-syntax-forward "w")
(setq end (point))
(while (looking-back word-regexp)
(backward-char))
(setq beg (point))
(delete-region beg end))
;; right of cursor = punctuation
((save-excursion (< 0 (skip-syntax-forward ".")))
(skip-syntax-forward ".")
(setq end (point))
(while (looking-back punctuation-regexp)
(backward-char))
(setq beg (point))
(delete-region beg end))
;; right of cursor = symbol
((save-excursion (< 0 (skip-syntax-forward "_()")))
(skip-syntax-forward "_()")
(setq end (point))
(while (looking-back symbol-regexp)
(backward-char))
(setq beg (point))
(delete-region beg end)))
;; Condition #1 -- Step 2 of 2
(cond
;; right of cursor = whitespace
;; left of cursor = not word / not symbol / not punctuation = whitespace or bol
((and
(save-excursion (< 0 (skip-chars-forward "\s\t")))
(not (save-excursion (> 0 (skip-syntax-backward "w"))))
(not (save-excursion (> 0 (skip-syntax-backward "."))))
(not (save-excursion (> 0 (skip-syntax-backward "_()")))))
(setq beg (point))
(skip-chars-forward "\s\t")
(setq end (point))
(delete-region beg end))
;; right of cursor = whitespace
;; left of cursor = word or symbol or punctuation
((and
(save-excursion (< 0 (skip-chars-forward "\s\t")))
(or
(save-excursion (> 0 (skip-syntax-backward "w")))
(save-excursion (> 0 (skip-syntax-backward ".")))
(save-excursion (> 0 (skip-syntax-backward "_()")))))
(fixup-whitespace))))
;; Condition # 2
;; right of cursor = whitespace
;; left of cursor = bol | left of cursor = whitespace | right of cursor = whitespace + eol
((and
(save-excursion (< 0 (skip-chars-forward "\s\t")))
(or
(bolp)
(save-excursion (> 0 (skip-chars-backward "\s\t")))
(save-excursion (< 0 (skip-chars-forward "\s\t")) (eolp))))
(setq beg (point))
(skip-chars-forward "\s\t")
(setq end (point))
(delete-region beg end))
;; Condition # 3
;; right of cursor = whitespace or eol
;; left of cursor = word or symbol or punctuation
;; not bol + word or symbol or punctuation
;; not bol + whitespace + word or symbol or punctuation
((and
(or (save-excursion (< 0 (skip-chars-forward "\s\t"))) (eolp))
(or
(save-excursion (> 0 (skip-syntax-backward "w")))
(save-excursion (> 0 (skip-syntax-backward ".")))
(save-excursion (> 0 (skip-syntax-backward "_()"))))
(not (save-excursion (> 0 (skip-syntax-backward "w")) (bolp)))
(not (save-excursion (> 0 (skip-syntax-backward ".")) (bolp)))
(not (save-excursion (> 0 (skip-syntax-backward "_()")) (bolp)))
(not (save-excursion (and (> 0 (skip-syntax-backward "w")) (> 0 (skip-chars-backward "\s\t")) (bolp))))
(not (save-excursion (and (> 0 (skip-syntax-backward ".")) (> 0 (skip-chars-backward "\s\t")) (bolp))))
(not (save-excursion (and (> 0 (skip-syntax-backward "_()")) (> 0 (skip-chars-backward "\s\t")) (bolp)))))
(setq end (point))
(cond
((save-excursion (> 0 (skip-syntax-backward "w")))
(while (looking-back word-regexp)
(backward-char)))
((save-excursion (> 0 (skip-syntax-backward ".")))
(while (looking-back punctuation-regexp)
(backward-char)))
((save-excursion (> 0 (skip-syntax-backward "_()")))
(while (looking-back symbol-regexp)
(backward-char))))
(setq beg (point))
(when (save-excursion (> 0 (skip-chars-backward "\s\t")))
(skip-chars-backward "\s\t")
(setq beg (point)))
(delete-region beg end)
(skip-chars-forward "\s\t"))
;; Condition # 4
;; not bol = eol
;; left of cursor = bol + word or symbol or punctuation | bol + whitespace + word or symbol or punctuation
((and
(not (and (bolp) (eolp)))
(or
(save-excursion (> 0 (skip-syntax-backward "w")) (bolp))
(save-excursion (> 0 (skip-syntax-backward ".")) (bolp))
(save-excursion (> 0 (skip-syntax-backward "_()")) (bolp))
(save-excursion (and (> 0 (skip-syntax-backward "w")) (> 0 (skip-chars-backward "\s\t")) (bolp)))
(save-excursion (and (> 0 (skip-syntax-backward ".")) (> 0 (skip-chars-backward "\s\t")) (bolp)))
(save-excursion (and (> 0 (skip-syntax-backward "_()")) (> 0 (skip-chars-backward "\s\t")) (bolp)))))
(skip-chars-forward "\s\t")
(setq end (point))
(setq beg (point-at-bol))
(delete-region beg end))
;; Condition # 5
;; point = eol
;; not an empty line
;; whitespace to the left of eol
((and
(not (and (bolp) (eolp)))
(eolp)
(save-excursion (> 0 (skip-chars-backward "\s\t"))))
(setq end (point))
(skip-chars-backward "\s\t")
(setq beg (point))
(delete-region beg end))
;; Condition # 6
;; point = not eob
;; point = bolp and eolp
;; universal argument = C-u = '(4)
((and
(not (eobp))
(and (bolp) (eolp))
(equal arg '(4)))
(delete-forward-char 1))) )))
这很可能以前已经解决了,但是我们可以编写自己的代码,而不是寻找代码。太有趣了!
我就是这样做的,希望对你有帮助。
(defun kill-whitespace-or-word ()
(interactive)
(if (looking-at "[ \t\n]")
(let ((p (point)))
(re-search-forward "[^ \t\n]" nil :no-error)
(backward-char)
(kill-region p (point)))
(kill-word 1)))
然后将其绑定到一个键上:
(global-set-key (kbd "M-d") 'kill-whitespace-or-word)
如果您使用基于 CC 模式的缓冲区,您可能正在寻找
Hungry Delete Mode
次要模式。
在多个地方尝试 C-c DEL 和 C-c DELETE 以感受差异。
如果您喜欢它的工作方式,您可以通过执行
M-x c-toggle-hungry-state
将饥饿删除功能切换为适用于标准键,或者将饥饿删除功能重新绑定到您的首选绑定。
如果你仍然认为需要搭载一键来执行前向杀字或空白,那么你可以执行类似于
c-hungry-delete-forward
的操作,或者只是暂时重新绑定c-delete-function
并调用它。
(defun c-hungry-delete-forward-word ()
"Delete the following word or all following whitespace
up to the next non-whitespace character.
See also \\[c-hungry-delete-backwards]."
(interactive)
(let ((c-delete-function (function kill-word)))
(c-hungry-delete-forward)))
查看信息页面
(ccmode) Hungry WS Deletion
了解更多信息。
这是删除空格、特殊字符或单词的解决方案,具体取决于光标后面(向后功能)或前面(向前功能)的内容。
与给出的一些解决方案不同,它不会在行的开头或结尾处中断。
向后删除:
(defun my-backward-kill-spaces-or-char-or-word ()
(interactive)
(cond
((looking-back (rx (char word)) 1)
(backward-kill-word 1))
((looking-back (rx (char blank)) 1)
(delete-horizontal-space t))
(t
(backward-delete-char 1))))
(global-set-key (kbd "<C-backspace>") 'my-backward-kill-char-or-word)
转发删除:
(defun my-forward-kill-spaces-or-char-or-word ()
(interactive)
(cond
((looking-at (rx (char word)) 1)
(kill-word 1))
((looking-at (rx (char blank)) 1)
(delete-horizontal-space))
(t
(delete-forward-char 1))))
(global-set-key (kbd "M-d") 'my-forward-kill-char-or-word)