vim:将游标后面的其余部分作为vim命令执行

时间:2012-09-28 10:31:32

标签: vim

我有时会发现自己正在编写文本,其中部分文件是由外部程序生成的。例如,考虑包含

的C源文件
/*
 * To regenerate the following data, place the cursor at the beginning
 * of the next line after this comment, then run
 *   ma:r!find /foo -name '*.in' | xargs whatever | some complicated processing
 * and merge the result with
 *   'a!}sort -u
 */
some
generated
stuff
here

我最终使用鼠标选择第一个命令(ma:...),粘贴&运行它,等待命令完成,选择'a!}sort -u并粘贴&运行。这是不优雅的,因为当我认为它可以完全自动化时,它只是半自动的。我阅读了:execute和朋友的vim在线帮助,但看起来这并不是我想要的。我正在考虑用正确的命令填充vim寄存器,然后执行寄存器内容。到目前为止,在线:help registers没有提供线索。

理想情况下,新评论会说类似

/*
 * To regenerate the following data, place the cursor on the 'j' on the
 * next line in this comment, then execute it with <SHORT-MAGIC-VIM-INCANTATION>
 *   jjma:r!find /foo -name '*.in' | xargs whatever | ...<CR>'a!}sort -u<CR>
 */

如何实现这一目标?

2 个答案:

答案 0 :(得分:4)

我会让它处理Ex命令,而不是普通模式命令(例如你的例子中的ma);通过:normal ma很容易执行后者,但是将Ex命令输入正常模式有逃避问题(例如,如何编码必须结束Ex命令的 Enter ?)< / p>

这是一个自定义命令:

command! -bar -range ExecuteFromCursor execute 'normal! "zy$' | @z

这是一个映射:

nnoremap <Leader>e "zy$:@z<CR>

答案 1 :(得分:3)

编辑 Ingo执行寄存器的方法比我的方法更简单(@"而不是<^R>")。

我的方法的优势在于您可以像按照其他方式一样以交互方式编辑命令行,然后按Enter键执行。

我会做

y$:<^R>"
  • y$ 抽到行尾
  • : 启动命令模式
  • C-r 插入笨拙的文字

按Enter键,获利。


我曾经有类似的映射。但是我很懒,经常只做 Y 并编辑命令行。在Windows上,我在vim命令行上找到 Shift-Insert 更平滑,所以我然后使用“+ Y 复制到Windows剪贴板。但我离题了