Notes on Vim’s macros.


Registers contain:

  1. saved macros
  2. 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 :registers)

@: (shortcut of :reg)

:reg a: Show content of register a

History (clipboard)

  • starts with double quote "
  • To execute previous operation, see :reg and type "<op>

default register: holds last command, such as result of dd, dw, yy, etc

To paste what you’ve cut 3 or 4 operations prior (


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

<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

special registers:

% filename
'*' the clipboard contents (X11: primary selection)
'+' the clipboard contents
'/' the last search pattern
':' the last command-line
'.' the last inserted text

auto completion <c-p>,<c-n> matches using vim’s completion

Subcompletion mode filename completion Ctrl-x ctrl-f

Ctag Ctrl-x ctrl-]

  • requires ctag

pattern 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 ctrl-p

autocomplete a line ctrl-x ctrl-l

  • type partial line and then use this.
  • author’s favorite

Omnicompletion ctrl-x ctrl-o

  • 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

Macro Recording

Start recording

q <macro letter> <commands to record> q

qX ... q saves macro to letter “X” (register X)

Execute macros

<optional: number of times>@<macro letter>

@q: execute macro q

@@: replay previous macro ; i.e. repeat macro – i.e. execute macro again

very useful!

Run in visual mode

  • first, visually select some lines i.e. vip (select current paragraph)
  • :normal @q execute macro “q” from visual mode

Save macros

By default (in non-compatible mode), macros are saved in file ~/.viminfo.

Save macros in .vimrc. Macros can be saved in .vimrc.

let @a='0fa'
let @b='This is really frequently used line'

Edit macros

(might not work)

:let @a='
Ctrl R Ctrl R a
edit the text
'   (append ' to finish command) + Enter
:reg a      view new value

Append macro

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 (

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.

Multiple Files

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

# Snippets from

'<,'>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

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