Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim - how to store and execute commonly used commands?

Tags:

text

vim

editor

If I wanted to process a batch of text files with the same set of commands for example:

:set tw=50
gggqG

Can I save the above and run it with a shortcut command?

like image 949
Fred Avatar asked Aug 24 '09 11:08

Fred


2 Answers

If you want to use it only once, use a macro as specified in some of the other answers. If you want to do it more often, you can include the following line in your .vimrc file:

:map \r :set tw=50<CR>gggqG

This will map \r to cause your two lines to be executed whenever you press \r. Of course you can also choose a different shortcut, like <C-R> (Ctrl+R) or <F12> or something.

like image 102
jqno Avatar answered Oct 20 '22 18:10

jqno


The following in .vimrc will define a new command Wrap that does what you want.

command! Wrap :set tw=50 | :normal gggqG

Call it with :Wrap

like image 31
Randy Morris Avatar answered Oct 20 '22 17:10

Randy Morris