【发布时间】:2012-03-07 07:30:08
【问题描述】:
M-m (back-to-indentation) 将点移动到行上的第一个非空白字符。我想做相反的事情:将点移动到行上的最后一个非空白字符。我一直无法为此找到“内置”命令,而且我对 ELisp 还不够熟悉,无法编写一些东西,所以请提供任何帮助。
【问题讨论】:
标签: emacs
M-m (back-to-indentation) 将点移动到行上的第一个非空白字符。我想做相反的事情:将点移动到行上的最后一个非空白字符。我一直无法为此找到“内置”命令,而且我对 ELisp 还不够熟悉,无法编写一些东西,所以请提供任何帮助。
【问题讨论】:
标签: emacs
(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 删除空格。
通常在这种情况下,我想获取最后一个非空白字符并删除尾随空格,所以我使用这个:
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-\` 是另一种解决方案,它将点移动到行尾,然后删除多余的空格。谢谢。
我想菲尔斯已经回答了你的问题。只是另一个 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),顺便说一句。
ws-trim-mode(还包括global-变体)来修剪尾随空格。它非常可定制,但默认配置只会修改您编辑的行,使其版本控制安全(即,如果您编辑具有大量尾随空格的文件,则不会引入大量不相关的更改)。 ftp.lysator.liu.se/pub/emacs/ws-trim.el
我写了这个函数来绑定到 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)
【讨论】:
我的版本:移动到行尾或最后一个非空格(通过删除结尾空格)
(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;然后我添加视觉线端。 (抱歉我的英语不好)。
【讨论】: