简体   繁体   English

vim:执行编辑器命令列表

[英]vim: Executing a list of editor commands

Is there a way in vim to give a list of editor commands? 在vim中有没有办法给出一个编辑器命令列表? I want to execute a series of 'global' commands, and there is some pattern to the commands. 我想执行一系列“全局”命令,命令有一些模式。 So I would ideally like to generate the list of commands (using regex search & substitute), and then run them, instead of having to type in each command. 因此,我希望生成命令列表(使用正则表达式搜索和替换),然后运行它们,而不必输入每个命令。

Thanks! 谢谢! Gaurav 拉夫

(Update: s/buffer/register/g) (更新:s / buffer / register / g)

If you can yank all of the commands you want to run into a register, then execute the register ? 如果你可以将你想要运行的所有命令拉入寄存器,那么执行寄存器?

http://www.bo.infn.it/alice/alice-doc/mll-doc/linux/vi-ex/node24.html http://www.bo.infn.it/alice/alice-doc/mll-doc/linux/vi-ex/node24.html

For example, if you have a file like: 例如,如果您有一个类似的文件:

abc
def
ghi
dd
dd

Then do 然后做

:g/dd/y A

This yanks all lines with dd and appends into register a 这用dd猛拉所有行并附加到寄存器a

Then if you are on the first line of the file, and type: 然后,如果您在文件的第一行,并键入:

@a

Then 2 lines will now be deleted, and the file should look like: 然后将删除2行,文件应如下所示:

ghi
dd
dd

Sounds like a macro to me. 对我来说听起来像一个宏。 Google vim macros for tons of useful information. 谷歌vim宏提供了大量有用的信息。 Basically 基本上

  1. In command mode type q followed by some other letter which will be the register in which we store the macro. 在命令模式下,键入q后跟一些其他字母,这些字母将是我们存储宏的寄存器。 For example 'qw'. 例如'qw'。
  2. Run the commands you want to store for future use. 运行要存储的命令以备将来使用。 This can be anything including regex substitutions, movements, inserts, deletes, etc. 这可以是任何包括正则表达式替换,移动,插入,删除等。
  3. When you are done hit q again. 当你完成再次击中q。 You now have stored your actions for future use. 您现在已存储了您的操作以供将来使用。 To rerun what you just did type @w (or whatever letter you used for the register to save the macro in). 要重新运行您刚刚输入的内容,请键入@w(或您用于寄存器的任何字母以保存宏)。

There's a lot of power in macros. 宏中有很多功能。 Sometimes you want to save macros between sessions. 有时您希望在会话之间保存宏。 When I want to do that, you can paste the macro into a buffer by pasting the register you just saved to. 当我想这样做时,您可以通过粘贴刚刚保存的寄存器将宏粘贴到缓冲区中。 For example, if you saved your macro to register w, type "wp to paste register w (note that's the double quote, not two single quotes). You'll see some funny characters for when you pressed escape or enter. There are more readable ways of writing these like and , but ^[ and ^M will work too. 例如,如果你保存宏来注册w,输入“wp to paste register w(注意这是双引号,而不是两个单引号)。当你按下escape或enter时,你会看到一些有趣的字符。还有更多写这些的可读方式和,但是^ [和^ M也会起作用。

You can now yank those lines into a register (I see someone just posted an answer with this but somewhat differently than I'd do it) by selecting the text in visual mode and then typing "wy. This yanks the selected text into register w. Now to run the register as a macro, type @w as before. 您现在可以将这些行放入一个寄存器(我看到有人刚刚发布了一个答案,但有点不同于我的方法),通过在可视模式中选择文本,然后键入“wy。”将所选文本拖入寄存器w现在将寄存器作为宏运行,像以前一样输入@w。

If it's something you use often it might be worth mapping the macro to a shortcut command in your .vimrc. 如果您经常使用它,则可能值得将宏映射到.vimrc中的快捷命令。 For example, here's a few maps I have in my vimrc to help me easily open and save my .vimrc 例如,这里有一些我在vimrc中的地图,可以帮助我轻松打开并保存我的.vimrc

" For modifying the .vimrc
nmap ,e :e $HOME/.vimrc<cr>
nmap ,s :write!<cr>:source $HOME/.vimrc<cr>

Now when I'm editing any file and discover a new macro I'd like to save I type ,e to open my .vimrc. 现在,当我正在编辑任何文件并发现一个新的宏我要保存我输入时,e打开我的.vimrc。 I paste the macro in, prepend a map statement (there's different maps depending on which mode you're in, plenty of documentation to explain this if you're interested), and save the .vimrc with ,s which also sources the .vimrc so that my new mapped command is available in other files without restarting vim. 我将宏粘贴进去,添加一个map语句(根据你所处的模式有不同的地图,如果你感兴趣的话还有大量的文档可以解释这个),并保存.vimrc,也可以保存.vimrc。这样我的新映射命令在其他文件中可用而无需重新启动vim。

Here is mappings to run interactively vim commands: 以下是以交互方式运行vim命令的映射:

" run selected vimscript
vmap <Leader>r "vy:@v<CR>
" run vimscript line
nmap <Leader>r "vyy:@v<CR>

Check vim help for: 检查vim帮助:

:h :@

So in your case (silly example): 所以在你的情况下(愚蠢的例子):

:g/line/
:g/for/
:g/endfor/

for line in getline(1, 3)
  let @a = line
  @a
endfor

save it as .vim and :so % 保存为.vim和:so%

The word "command" is kind of ambiguous here. “命令”这个词在这里有点含糊不清。 You can also execute Ex commands that have been yanked into a register using :@" eg for the default register. This is especially useful when you're writing vimscript code, eg in your .vimrc file, and you want to test out functions or mappings to make sure they work okay. 您还可以使用以下命令执行已经被拉入寄存器的Ex命令:@“例如,对于默认寄存器。这在您编写vimscript代码时特别有用,例如在.vimrc文件中,并且您想要测试函数或映射以确保它们正常工作。

So if you have a buffer that contains 所以如果你有一个包含的缓冲区

tabedit http://stackoverflow.com/questions/1830886/vim-executing-a-list-of-editor-commands
%s/intuited/The King Of Everything/g
w /tmp/superduperawesome.html
!/usr/bin/sensible-browser /tmp/superduperawesome.html

you can use the normal mode actions 您可以使用正常模式操作

ggyVG:@"

to bring up a slightly different version of this page. 提出这个页面略有不同的版本。

It's also possible to execute the code that's stored in a register, or in a variable, using 也可以使用执行存储在寄存器或变量中的代码

:execute @"

but there seem to be some weird issues with this: it will stop executing when it hits a comment or the end of a block. 但似乎有一些奇怪的问题:它会在命中评论或结束时停止执行。 I haven't had those problems using :@". 我没有遇到过这些问题:@“。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM