Notes on Vim’s macros.
- saved macros
- all previous operations, and acts as clipboard/history of all cmd
Useful for pasting what I copied few operations before.
View all macros & history operations
:reg <optional macro letter> (or
@: (shortcut of
:reg a: Show content of register a
- starts with double quote
- To execute previous operation, see
default register: holds last command, such as result of
To paste what you’ve cut 3 or 4 operations prior (http://statico.github.io/vim.html)
add next 10 lines to ‘x’ register
save text to register ‘a’
"ay yank, save to register 'a' "ad delete, save to register 'a' "ap paste using register 'a'
- useful to save commonly pasted items into register!
Insert mode completion
Insert values in Registers while in insert-mode
- Let Vim Do the Typing - Youtube
<C-r><RegisterCharacter>: insert content of register
<c-r>a :insert register a <c-r>3 :paste what was copied 3 operations ago. (see reg or @: to see them) <c-r>. :paste repeat
% filename '*' the clipboard contents (X11: primary selection) '+' the clipboard contents '/' the last search pattern ':' the last command-line '.' the last inserted text
<c-n> matches using vim’s completion
- requires ctag
ctrl-x ctrl-p (or ctrl-n): repeat from pattern found before Or after
- finds a pattern from current document and tries to match repetitive structure
- author tends to use
autocomplete a line
- type partial line and then use this.
- author’s favorite
- works well with certain languages
:set complete : find where autocompletion is found
. = this file, w=window,b=buffer, u=unloaded buffer, t=tags file, i=program's include file (such as import/include), kspell= spell dictionary if spellcheck is enabled
q <macro letter> <commands to record> q
qX ... q saves macro to letter “X” (register X)
<optional: number of times>@<macro letter>
@q: execute macro q
@@: replay previous macro ; i.e. repeat macro – i.e. execute macro again
Run in visual mode
- first, visually select some lines i.e.
vip(select current paragraph)
:normal @qexecute macro “q” from visual mode
By default (in non-compatible mode), macros are saved in file
Save macros in
.vimrc. Macros can be saved in
let @a='0fa' let @b='This is really frequently used line'
(might not work)
:let @a=' Ctrl R Ctrl R a edit the text ' (append ' to finish command) + Enter :reg a view new value
qA...q: use capital letter (shift) to append instead of recording over it..
Helpful Vim key mapping
Map macro to a key (might not work if Space is leader )
:nnoremap <Space> @q
To get rid of annoying register pop-up window, remap q
map q: :q
On a line containing only the number 1, type this (http://peterlyons.com/leveling_up.html):
qaYp<C-a>q → qa start recording. Yp duplicate this line. <C-a> increment the number. q stop recording. @a → write 2 under the 1 @@ → write 3 under the 2 Now do 100@@ will create a list of increasing numbers until 103.
Perform macro on multiple files of buffers
:args *.c :argdo normal @q :wa #save
- Use case: add copyright comments on multiple source codes.
# Snippets from https://gist.githubusercontent.com/gmccreight/7519289/raw/master.vim '<,'>normal @q run the macro recorded into the q register on all selected lines (the '<,'> is automatically added) :let @q="2dw" easily fill the q register with a macro that deletes two words norm what's a good shorthand for "normal" on the #vim_command_line :argdo norm @q run your last macro against all files in the args