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?
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?
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.
Yes, the important word is macro
But it seems like a 'command' such as :set tw=50 would be better included in the .vimrc
file so vim uses it every time you start it up.
As a very quick start, put this in your .vimrc:
" define the function
" '!' means override function if already defined
" always use uppercase names for your functions
function! DoSomething()
:set tw=50
gggqG
endfunction
" map a keystroke (e.g. F12) in normal mode to call your function
nmap <F12> :call DoSomething()<CR>
note: the formatted code above looks rather horrible, but lines starting with " are comments.
Before you go for writing a thousands-lines .vimrc
(which is a good thing, but you can postpone it for a while), I think you might want to look at the plain recording, in particular you may consider using the q
x (where x is any key) for recording, q
to finish recording and @
x to execute recorded macro.
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