Vim: Executing a List of Editor Commands

Is there a way in vim to provide a list of editor commands? I want to execute a series of "global" commands, and there are several command templates. Therefore, I would ideally like to create a list of commands (using search and replace in a regular expression), and then run them, instead of entering each command.

Thanks! Gaurav

+4
source share
4 answers

(Update: s / buffer / register / g)

If you can pull out all the commands you want to run in register, then execute register?

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 

These are yanks of all lines with dd and add to the register

Then, if you are in the first line of the file and enter:

 @a 

Then 2 lines will be deleted and the file should look like this:

 ghi dd dd 
+8
source

Sounds like a macro to me. Google vim macros for tons of useful information. Basically

  • In command mode, enter q and then another letter, which will be the register in which we save the macro. For example, "qw".
  • Run the commands you want to keep for future use. It can be anything, including regular expressions, moves, inserts, deletes, etc.
  • When you're done, press q again. Now you have saved your actions for future use. To repeat what you just wrote, type @w (or any other letter you used to register to save the macro).

Macros have a lot of energy. Sometimes you want to save macros between sessions. When I want to do this, you can paste the macro into the buffer by inserting the register that you just saved. For example, if you saved your macro to register w, enter "wp to insert the register w (note that the double quote is not two single quotes). You will see some funny characters when you hit the escape button or enter. Readable methods their spellings are similar, but [^ and ^ M will also work.

Now you can output these lines to the register (I see that someone just sent an answer with this, but in a slightly different way than I did), selecting the text in visual mode and then typing "wy". to register w. Now, to run the register as a macro, enter @w as before.

If this is often used, it might be worth matching the macro with the shortcut command in your .vimrc. For example, here are a few maps that I have in my vimrc to help me easily open and save my .vimrc

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

Now, when I edit any file and open a new macro, I would like to save the type, e, to open my .vimrc. I insert a macro, add a map operator (there are different maps, depending on which mode you are in, a lot of documentation to explain this if you are interested), and save .vimrc with, s, which is also the source of .vimrc so that my new mapped command is available in other files without restarting vim.

+1
source

The following are the mappings for running interactive vim commands:

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

Check out vim help for:

 :h :@ 

So in your case (stupid example):

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

save it as .vim and: so%

0
source

The word "team" is ambiguous here. You can also execute Ex commands that have been pulled into the register using: @ "for the default register, for example. This is especially useful when writing vimscript code, for example, in your .vimrc file, and you want to check functions or mappings, to make sure they are working properly.

So, if you have a buffer containing

 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 normal mode actions

 ggyVG:@" 

to open a slightly different version of this page.

It is also possible to execute code that is stored in a register or in a variable using

 :execute @" 

but there seem to be some strange problems with this: it stops execution when it accesses the comment or the end of the block. I had no such problems: @ ".

0
source

All Articles