Is there a way in vim to give a list of editor commands? 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)
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
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
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:
ghi
dd
dd
Sounds like a macro to me. Google vim macros for tons of useful information. Basically
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.
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.
If it's something you use often it might be worth mapping the macro to a shortcut command in your .vimrc. For example, here's a few maps 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'm editing any file and discover a new macro I'd like to save I type ,e to open my .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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With