M-m (back-to-indentation
)移动指向该行上的第一个非空白字符。我想做相反的事情:将点移动到该行的最后一个非空白字符。我一直无法找到内置的"为此命令我并不熟悉ELisp写东西,所以请任何帮助。
答案 0 :(得分:6)
(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:]]"))
答案 1 :(得分:4)
通常在这种情况下我想要到达最后一个非空白字符并删除尾随空格,所以我使用它:
M-\ runs the command delete-horizontal-space, which is an interactive
compiled Lisp function in `simple.el'.
在极少数情况下,我想要保留空白,我只使用 Mb Mf (backward-word
,forward-word
)足够接近。
答案 2 :(得分:1)
我编写了此函数以绑定到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)
答案 3 :(得分:0)
我认为phils已经回答了你的问题。只是另一个POW ..拖尾的白色空间非常烦人,看不见并容易出错(?)。所以我有before-save-hook
的钩子来删除它们。
;;; delete nasty hidden white spaces at the end of lines
(add-hook 'before-save-hook 'delete-trailing-whitespace)
所以你的缩进操作对我来说只是C-e
。
答案 4 :(得分:0)
我的版本:移至行尾或最后一个非空格(通过删除结尾空格)
(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"
(Ctrl + e)键:
interactive "^"
)。let (deactivate-mark)
是要确保保留该区域。这取自@justinokamoto;然后添加视觉线末端。 (对不起,我的英语不好)。