Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim: Executing a list of editor commands

Tags:

vim

editor

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

like image 832
user10 Avatar asked Jan 23 '23 22:01

user10


2 Answers

(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
like image 138
toolkit Avatar answered Jan 25 '23 11:01

toolkit


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

  1. In command mode type q followed by some other letter which will be the register in which we store the macro. For example '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. 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).

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.

like image 22
mmrobins Avatar answered Jan 25 '23 13:01

mmrobins