当我意识到我需要(ed)在上面的行上定义变量(或类似的东西)时,我经常发现自己在一行上打字。我想要的是
- 从一行的任意位置按 C-return 并让光标移动到上面新插入的空白行,缩进正确(或至少与原始行相同)。
- 可以拉出任何文本...
- 和 Cu C 空间回到原来的位置
我已经做到了#1,但是我的 emacs-fu 不够强大,无法完成其余的工作。
这是我谦虚的解决方案:
(defun my-insert-before-line ()
(interactive)
(save-excursion
(beginning-of-line)
; I've changed the order of (yank) and (indent-according-to-mode)
; in order to handle the case when yanked line comes with its own indent
(yank)(indent-according-to-mode)
; could be as well changed to simple (newline) it's metter of taste
; and of usage
(newline-and-indent)))
希望能帮助到你。
如果您不是禅宗大师 emacs 兄弟,您可以执行以下操作。
Emacs 有一个记录宏的东西,kmacro-start-macro 和 kmacro-end-macro。
录制宏后,执行 name-last-kbd-macro。然后访问 .emacs,然后执行 insert-kbd-macro。
然后你有一个定义你的宏的 fset 语句。它可能看起来很有趣,而且它不像 elisp 那样可维护,但是如果你将它塞进你的 .emacs 中,那个宏(以那个名字命名)将可用于你的任何编辑会话。您也可以将其绑定到键序列。
回答我自己的问题可能是不好的形式,但 Cheeso 的回答激励我十年来第二次做一些 lisp 编程(我的原始版本是一个命名的键盘宏,但它跨越了整个杀戮/标记环)。这是我想出的
(defun insert-and-indent-line-above ()
(interactive)
(push-mark)
(let*
((ipt (progn (back-to-indentation) (point)))
(bol (progn (move-beginning-of-line 1) (point)))
(indent (buffer-substring bol ipt)))
(newline)
(previous-line)
(insert indent)))
(global-set-key [ (control return) ] 'insert-and-indent-line-above)
可能有很多更好的方法可以做到这一点,但是两个小时的 lisp-hacking 很难被称为浪费时间 :-)