M-m (back-to-indentation
) 将光标移动到行中第一个非空格字符。我想要的是相反的:将光标移动到行末的最后一个非空格字符。我找不到内置的命令来实现这个功能,而且我对 ELisp 不熟悉,所以希望有人能提供帮助。
M-m (back-to-indentation
) 将光标移动到行中第一个非空格字符。我想要的是相反的:将光标移动到行末的最后一个非空格字符。我找不到内置的命令来实现这个功能,而且我对 ELisp 不熟悉,所以希望有人能提供帮助。
(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:]]"))
M-\ runs the command delete-horizontal-space, which is an interactive
compiled Lisp function in `simple.el'.
在极少数情况下,如果我想保留空格,我只需使用 M-b M-f(backward-word
,forward-word
),这通常已经足够接近了。
C-e
。 - Derek SlagerC-e
M-\\
是另一种解决方案,它将光标移动到行尾,然后删除多余的空格。谢谢。 - SabreWolfy我觉得Phil已经回答了你的问题。还有一个POW(Power-of-Whitespace)是,尾随的空白非常让人恼火,不可见且容易出现错误。因此,我为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)
。 - sanityincws-trim-mode
(还包括 global-
变体)来修剪行末空格。它非常可定制,但默认配置仅修改您编辑的行,使其版本控制安全(即,如果您编辑具有大量行末空格的文件,则不会引入大量无关更改)。ftp://ftp.lysator.liu.se/pub/emacs/ws-trim.el - philsmove-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))))
;; 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中获取的;然后我添加了视觉行末尾。(对我的破英语感到抱歉)。
(forward-char)
,这样我就可以只使用C-k
来删除空格了。 - SabreWolfy