9

如何将 emacs 配置为与按Alt+DAlt+Backspace删除相邻空格或单个单词的其他现代编辑器相同的方式工作?默认情况下,emacs 总是删除一个单词。

4

4 回答 4

11

在使用 Emacs 的一段时间后,我发现即使我可以更改基本功能,但在效率方面通常不会带来太多回报。事实上,在我做了几次之后,我开始后悔并取消了它。这并非一直如此,一些键绑定真的很不舒服或很少有用,但我认为 kill word 的工作方式并非如此。事实上,我现在才意识到:我确实尝试过 Eclipse 中的键绑定,但我一直在将它与 Emacs 风格的键绑定一起使用......

无论如何,正如我刚才所说,在您“修复”该功能之前,请确保它真的被破坏了 :) 我从来没有发现自己需要您描述的那种功能,也许这就是原因:

  1. M-SPC将单词之间的空格减少到只有一个空格。如果该点位于单词之间并且我想删除分隔单词的额外空格,这就是我会使用的。

  2. M-\删除所有水平空间。这将连接两个以空格分隔的单词。

  3. 如果您要实现的是某种“稀疏”格式,例如:


int foo          = 42;
unsigned int bar = 43;

那么就M-xalign-regexp必须这样做。

  1. 我只是从来没有碰巧有 a) 很长的后续运行 whitepsace,除非它是缩进,并且在它是缩进的情况下,TAB通常会更好地处理它。b)即使有很长的后续空格,我也很少一次移动一个字符,所以很难想到我会发现这个点被几个空格包围的情况。想到艺术家模式或点图之类的东西,但在代码编辑期间不会发生。

  2. 最后,如果你想,好吧,假设只是编辑一个任意文本文件,并且你想在单词之间添加或删除水平空格......同样,有M-xalign-regexp这样做,或者你可以使用对矩形进行操作的命令,如果那是当时的几行。好吧,Emacs 甚至会识别临时选项卡,并会尝试对齐文本,例如匹配点之前的最后一行,当您点击TAB.

最后,如果由于某种原因我无法理解 :) 我真的需要完全按照你的描述做,那么我会这样做:(kM-\BACKSPACE它可以是任何其他键而不是“k” - 它就在你的手指下,所以输入起来很快 :) 或者,如果我懒得去想它:M-SPCM-fM-bC-w- 可能听起来很多,但这些是你一直在使用的命令,所以它不会妨碍你速度方面。

于 2013-07-31T07:22:02.637 回答
10
(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))) )))
于 2013-12-08T17:58:08.420 回答
5

这很可能以前已经解决了,但是我们可以编写自己的代码,而不是寻找代码。玩得很尽兴!

我就是这样做的,希望对你有帮助。

(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)
于 2013-07-30T23:37:02.710 回答
1

如果您使用的是基于 CC 模式的缓冲区,您可能正在寻找Hungry Delete Mode次要模式。

尝试C-c DELC-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了解更多信息。

于 2013-07-31T00:04:47.610 回答