【问题标题】:Emacs: move point to last non-whitespace characterEmacs:将点移动到最后一个非空白字符
【发布时间】:2012-03-07 07:30:08
【问题描述】:

M-m (back-to-indentation) 将点移动到行上的第一个非空白字符。我想做相反的事情:将点移动到行上的最后一个非空白字符。我一直无法为此找到“内置”命令,而且我对 ELisp 还不够熟悉,无法编写一些东西,所以请提供任何帮助。

【问题讨论】:

    标签: emacs


    【解决方案1】:
    (defun my-move-end-of-line-before-whitespace ()
      "Move to the last non-whitespace character in the current line."
      (interactive)
      (move-end-of-line nil)
      (re-search-backward "^\\|[^[:space:]]"))
    

    【讨论】:

    • 谢谢。我在末尾添加了(forward-char),这样我就可以使用C-k 删除空格。
    【解决方案2】:

    通常在这种情况下,我想获取最后一个非空白字符并删除尾随空格,所以我使用这个:

    M-\ runs the command delete-horizontal-space, which is an interactive
    compiled Lisp function in `simple.el'.
    

    在极少数情况下,我想保留空格,我只使用 Mb Mf (backward-word, forward-word),这通常足够接近。

    【讨论】:

    • 这很有用,但要求该点已经移动到正确的位置。
    • 好点。如果还没有在行尾,您需要事先添加C-e
    • 是的,所以C-e `M-\` 是另一种解决方案,它将点移动到行尾,然后删除多余的空格。谢谢。
    【解决方案3】:

    我想菲尔斯已经回答了你的问题。只是另一个 POW .. 尾随空格非常烦人,不可见并且容易出现错误(?)。所以我有一个钩子让before-save-hook 删除它们。

    ;;; delete nasty hidden white spaces at the end of lines
    (add-hook 'before-save-hook 'delete-trailing-whitespace)
    

    所以你的缩进操作对我来说只是C-e

    【讨论】:

    • 你可以更简单地写成(add-hook 'before-save-hook 'delete-trailing-whitespace),顺便说一句。
    • @sanityinc 是的。你说的对。删除了多余的 lambda。谢谢
    • 我推荐优秀的ws-trim-mode(还包括global-变体)来修剪尾随空格。它非常可定制,但默认配置只会修改您编辑的行,使其版本控制安全(即,如果您编辑具有大量尾随空格的文件,则不会引入大量不相关的更改)。 ftp.lysator.liu.se/pub/emacs/ws-trim.el
    • @phils 很高兴知道这一点。我认为在编辑其他人的文件时它会非常有用。感谢告知
    【解决方案4】:

    我写了这个函数来绑定到 C-e(通常是move-end-of-line)。 C-e 照常工作,但如果您的指针已经在行尾,它将删除尾随空格。

    (defun my/smarter-move-end-of-line (arg)
      "Move to the last non-whitespace character in the current line.
    
    Move point to end of this line. If point is already there, delete
    trailing whitespace from line, effectively moving pointer to last
    non-whitespace character while also removing trailing whitespace.
    
    If ARG is not nil or 1, move forward ARG - 1 lines first."
      (interactive "^p")
      (setq arg (or arg 1))
    
      ;; Move lines first
      (when (/= arg 1)
        (let ((line-move-visual nil))
          (forward-line (1- arg))))
      (let ((orig-point (point)))
        (move-end-of-line 1)
        (when (= orig-point (point))
          (delete-horizontal-space))))
    

    重新映射 C-e:

    ;; remap C-e to 'smarter-move-end-of-line'
    (global-set-key [remap move-end-of-line]
            'my/smarter-move-end-of-line)
    

    【讨论】:

      【解决方案5】:

      我的版本:移动到行尾或最后一个非空格(通过删除结尾空格)

      (defun smart-end-of-line ()
        "Move to end of line or last non-space (by deleting ending spaces)"
        (interactive "^")
        (let ((p (point)))
          (end-of-visual-line)
          (if (= p (point)) (end-of-line))
          (if (= p (point)) (let (deactivate-mark) (delete-horizontal-space)))))
      (global-set-key [end] 'smart-end-of-line)
      (global-set-key "\C-e" 'smart-end-of-line)
      

      [end]"\C-e" (control+e) 键:

      • 将光标(点)移动到视线的末端。
      • 如果它已经在视线的末端,则将其移动到视线的末端。
      • 如果它已经存在,则通过删除所有结尾空格将其移动到该行的最后一个非空白字符。
      • 移动时,保留区域 (interactive "^")。
      • let (deactivate-mark) 用于确保保留该区域。

      这取自@justinokamoto;然后我添加视觉线端。 (抱歉我的英语不好)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-08-04
        • 1970-01-01
        • 2010-10-01
        • 2018-03-04
        • 2011-06-10
        • 2015-04-27
        • 2021-10-08
        相关资源
        最近更新 更多