热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Emacs命令在光标上方插入和缩进行-Emacscommandtoinsertandindentlineabovecursor

Ifrequentlyfindmyselftypingonaline,whenIrealizeIneed(ed)avariabledefinition(orsomet

I frequently find myself typing on a line, when I realize I need(ed) a variable definition (or something similar) on the line above. What I would like is to

当我意识到我需要(编辑)上面一行的变量定义(或类似的东西)时,我经常发现自己在一条线上打字。我想要的是

  1. press C-return from anywhere on a line and have the cursor move to a newly inserted blank line above, with correct indentation (or at least the same as the original line).
  2. 从一行的任何地方按C-return,让光标移动到上面新插入的空白行,并有正确的缩进(或至少与原始行相同)。

  3. be able to yank any text...
  4. 能够抽出任何文字......

  5. and C-u C-space to get back to the original position
  6. 和C-u C空间回到原来的位置

I've managed to do #1, but my emacs-fu isn't strong enough to do the rest.

我已经设法做了#1,但是我的emacs-fu还不够强大,不能做其余的事情。

4 个解决方案

#1


Here's my humble solution:

这是我谦虚的解决方案:


(defun insert-before-line ()
  (interactive)
  (let ((pos (point))
        (cur-max (point-max)))
    (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) 
    (goto-char (+ pos (- (point-max) cur-max)))))

Hope it helps.

希望能帮助到你。

#2


Here's what you can do if you are not a Zen master emacs dude.

如果你不是禅宗大师emacs老兄,你可以做的就是这些。

Emacs has a record-macro thing, kmacro-start-macro and kmacro-end-macro.

Emacs有一个记录宏的东西,kmacro-start-macro和kmacro-end-macro。

After recording your macro, do name-last-kbd-macro. then visit .emacs, and do insert-kbd-macro.

录制宏后,执行name-last-kbd-macro。然后访问.emacs,并执行insert-kbd-macro。

You then have an fset statement that defines your macro. It may look funny, and it is not as maintainable as elisp, but if you stuff it into your .emacs, that macro (by that name) will be available to any of your editing sessions. And you can bind it to a key sequence as well.

然后,您有一个定义宏的fset语句。它可能看起来很有趣,并且它不像elisp那样可维护,但是如果你将它填入你的.emacs中,那个宏(通过那个名字)将可用于你的任何编辑会话。您也可以将它绑定到键序列。

#3


Probably bad form to answer my own question, but Cheeso's answer motivated me to do some lisp programming for the second time in ten years (my original version was a named keyboard macro, but it stepped all over the kill/mark-rings). Here's what I came up with

回答我自己的问题可能是糟糕的形式,但Cheeso的回答促使我在十年内第二次做一些lisp编程(我的原始版本是一个命名的键盘宏,但它遍布了kill / mark-rings)。这就是我想出的

(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)

there are probably many better ways of doing this, but two hours of lisp-hacking can hardly be called wasted time :-)

可能有很多更好的方法可以做到这一点,但两个小时的lisp-hacking几乎不能称为浪费时间:-)

#4


Just hold your thought on the variable declaration, finish typing your statement, then type RET (though C-j is usually more DWIM) and type your next line for the variable declaration, then hit C-x C-t to transpose the lines.

只需按住你对变量声明的想法,完成键入你的语句,然后键入RET(虽然C-j通常更多是DWIM)并键入你的下一行变量声明,然后点击C-x C-t来转置行。


推荐阅读
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • 本文介绍了如何在C#中启动一个应用程序,并通过枚举窗口来获取其主窗口句柄。当使用Process类启动程序时,我们通常只能获得进程的句柄,而主窗口句柄可能为0。因此,我们需要使用API函数和回调机制来准确获取主窗口句柄。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • 本文探讨了 Objective-C 中的一些重要语法特性,包括 goto 语句、块(block)的使用、访问修饰符以及属性管理等。通过实例代码和详细解释,帮助开发者更好地理解和应用这些特性。 ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
  • 利用存储过程构建年度日历表的详细指南
    本文将介绍如何使用SQL存储过程创建一个完整的年度日历表。通过实例演示,帮助读者掌握存储过程的应用技巧,并提供详细的代码解析和执行步骤。 ... [详细]
author-avatar
義忠仁倫冧沫Bob
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有