Vim tidbits
Mastering Vim Quickly
Everything in this section taken from the “Mastering Vim Quickly” book by Jovica Ilic
Ch 8 Undo and Redo
Temporal undo/redo
ea[rlier]: go back in time (temporal undo)
lat[er]: go forward in time (temporal redo)
examples:
| command | description |
|---|---|
earlier 2d |
undo changes in last 2 days |
ea 3h |
… |
ea 1m |
… |
later 5m |
redo changes in last 5 minutes |
lat 15s |
… |
earlier 3f |
undo last 3 file states (buffer writes) |
Undo branches
Vim creates an undo branch on every u, representing state of the file before executing undo.
g-/g+: move between these branches
Persistent undo
Vim undo/redo work in current session but go away once the session ends.
We can create an undo directory where vim stores hidden files that stores undo/redo between sessions.
$ mkdir ~/.vim/undodir
Add to .vimrc:
set undodir=~/.vim/undodir
Ch 14 Macros
Macro workflow:
qastarts recording actions into registeraqstops recording@aexecutes the macro@@executes last executed macro10@aexecutes the macro 10 times
Execute macro in multiple files
Argument list of files:
:args app/models/*.rb
Execute macro in register a using Normal mode:
:argdo normal @a
Save all buffers:
:argdo update
Editing a macro
"<register>pto insert on an empty line- edit
^"<register>y$to move cursor to beginning and copy the macro into register
Recursive macros
Intuitive meaning.
Example recursive macro: qaI"<esc>A"<esc>j@aq
Explanation:
qarecord into registeraI"<esc>insert"at beginning of line and go back to normal modeA"<esc>insert"at end of line and go back to normal modej@ago to next line and recursively call macroqstop recording
Using the norm command
norm[al] in Execution mode (:) can be used to conditionally apply macros
Examples:
:6,16norm @vexecute macrovon lines 6 to 16:10,$norm @iexecute macroion all lines >= 10 (to end of file):%norm @mexecute macromon all lines:g/pattern/norm @oexecute macrooon all lines matchingpattern:norm @awhen there are visually selected lines executes macroaon those lines
Ch 18 Effective multiple file editing
Vim has built in commands to access different lists so we can execute commands in bulk
:argdoargument list:bufdobuffer list:windowindow list
The execute and normal commands
exe[cute] evaluates a string as a Vim command.
norm[al] takes a sequence of keys and treats them as a Vim command in Normal mode.
norm[al]! ignores personal key mappings.
bufdo examples
Paste to the end of each buffer
:bufdo exe ":norm Gp" | update
:bufdoover all active buffersexeexecutes the following string as a command":norm Gp"use normal mode to go to end of file and paste| updateuse|to execute another commandupdate, which writes the changes (can also usew)
Note: difference between :w and :up[date] is that :up[date] only updates the file mtime if required, whereas :w always updates the mtime
Execute macro over each buffer
:bufdo exe ":norm! @a" | w
argdo examples
All :bufdo examples apply to :argdo as well
Useful commands:
:argsshow args:args /path/to/files/*/*replace old arglist with new one:arga[dd] /path/to/file.txtadd to arglist:argd[elete] /path/to/file.txtremove from arglist:argdo updatesave changes:argdo undoundo last operation
Find and replace across files with external commands
Get all files with the string Bad (can also use git grep -l instead of ag -l)
:args `ag -l Bad`
Replace Bad with Good across those files, asking to c[onfirm] each match
:argdo %s/Bad/Good/gc | w
Find and replace across files without external commands
After adding files to the arglist
:argdo %s/Bad/Good/ge | w
The e flag tells vim not to issue an error if the pattern is not found.
windo
:windo only affects currently visible buffers.