Vimtricks
The Book of Knowledge
VimTricks.md
- The Book of Knowledge
Vim Tips and Tricks
Using Vim as a Pipe
vim - -es +'g/foo/p' +'qa!' --not-a-term
vim < /dev/tty <(cat)
Simple Math
CTRL-A - increment number under cursor
CTRL-X - decrement number under cursor
Bouncing
Matchit - use g% to find next/previous match
use % to find match to construct under cursor (if, (, ), {, } etc.
Checking Variables
:verbose set variable?
OR
:echo "Variable:" &variable
Formatting
gq*direction* will format a line, paragraph, etc. based on direction. You
can also pipe to fmt using range!}fmt. Add the -tu or -c to control
indenting, and -p prefix to add a prefix.
Unicode
Ctrl-Vu HEX will input the unicode characer HEX. For Example, insert
a ✓ checkmark when in Insert Mode using Ctrl-Vu2713.
Changing Case
g<change><motion>
g = go
*change* = U Upper, u Lower, ~ Invert
Doubled *change* means "entire line"
*motion* = G end of Document, j Line Above, w Word
To turn one line into title caps (title case), make every first letter of a word uppercase:
:s/\v<(.)(\w*)/\u\1\L\2/g
Case Change Examples
| Command | Effect |
|---|---|
| ~ | Changes the case of current character |
| guu | Change current line from upper to lower. |
| gUU | Change current LINE from lower to upper. |
| guw | Change to end of current WORD from upper to lower. |
| guaw | Change all of current WORD to lower. |
| gUw | Change to end of current WORD from lower to upper. |
| gUaw | Change all of current WORD to upper. |
| g~~ | Invert case to entire line |
| g~w | Invert case to current WORD |
| guG | Change to lowercase until the end of document. |
| gU) | Change until end of sentence to upper case |
| gu} | Change to end of paragraph to lower case |
| gU5j | Change 5 lines below to upper case |
| gu3k | Change 3 lines above to lower case |
Patterns with Line Feeds
The regex construct “_” adds the newline character to the character class.
So if [\s\d] matches any whitespace or digit, \_[\s\d] matches any
whitespace, digit, or linefeed. You can also literally add \n to a class.
Coalesce Blank Lines
Allow only 1 blank line between regions with non-blank lines: :v/\S/,/\S/-j
Non-breaking Space
To find out the character code of the non-breaking space, place the
cursor over it and type use the normal mode command ga. Vim will
display a legend at the bottom. Note the hex representation. If the hex
representation is 00a0, then you could execute the following command to
do the substitution:
:%s/\%u00a0/\~/g
Spelling
To turn on spelling: :setlocal spell spelllang=en_us
Highlighting
Name | Description | Highlight |—|— SpellBad | word not recognized | hl-SpellBad SpellCap | word not capitalised | hl-SpellCap SpellRare | rare word | hl-SpellRare SpellLocal | wrong spelling for selected region | hl-SpellLocal
Searching for Misspellings
| Command | Effect |
|---|---|
| ]s | Move to next misspelled word after the cursor. A count before the |
| command can be used to repeat. ‘wrapscan’ applies. | |
| [s | Like ]s but search backwards, find the misspelled word before the |
| cursor. Doesn’t recognize words split over two lines, thus may stop at words | |
| that are not highlighted as bad. Does not stop at word with missing capital | |
| at the start of a line. | |
| ]S | Like ]s but only stop at bad words, not at rare words or words for |
| another region. | |
| [S | Like ]S but search backwards. |
To add words to your own word list
| Command | Effect |
|---|---|
| zg | Add word under the cursor as a good word to the first name in |
| ‘spellfile’. A count may precede the command to indicate the entry in | |
| ‘spellfile’ to be used. A count of two uses the second entry. | |
| zw | Like “zg” but mark the word as a wrong (bad) word. If the word already |
| appears in ‘spellfile’ it is turned into a comment line. | |
| zug zuw | Undo zw and zg, remove the word from the entry in ‘spellfile’. |
Finding suggestions for bad words
- z= For the word under/after the cursor suggest correctly spelled replaced. This may take a long time. Hit CTRL-C when you get bored.
- CTRL-Xs In Insert mode, when the cursor is after a badly spelled word, you can use CTRL-X s to find suggestions. This works like Insert mode completion. Use CTRL-N to use the next suggestion, CTRL-P to go back.
Renumbering Lists
There are at least four ways to number or renumber lists:
Renumbering using perl
:perl $a=0
:40,44 perldo s/^\d/++$a/e
Renumbering using Visual Mode
Make a blockwise, visual selection on the first character of each list
item: ^<C-V>2j for 2 lines, etc. Insert a 0. at the beginning
of these lines: I0. <Esc> Re-select the visual selection (which is
now all of the 0s) with gv gvg<C-A> and increment them as a sequence
g<C-A>: The entire sequence: ^<C-V>2jI0. <Esc>gvg<C-A>.
Renumbering using Macros
Use <C-A> to increment the line number. Use <C-x> to decrement the line number.
i # insert mode
<ctrl-Y><ctrl-Y><ctrl-Y> # copy the first few characters from the line above
<ESC> # back to normal mode
| # go back to the start of the line
<ctrl-A> # increment the number
j # down to the next line
q # stop recording
or
k # Previous line
^yWjP # yank word, next line, Put
^<ctrl-a>j # begining of line, increment, next line
Now you can play back the recording with @a (the first time; for subsequent times, you can do @@ to repeat the last-executed macro) and it will add a new incremented number to the start of each line.
Renumbering using External Programs
Use :<RANGE>!nl -ba or :%!cat -n commands which will add line numbers
to all the lines in RANGE. This is not as useful if RANGE is
already numbered
Perl
If vim is compiled with perl support, you can run perl commands at the ex
command prompt! To see what version of Perl you have: :perl print $^V or
vim --version | grep -i perl.
Execute Perl command {cmd} for each line in the [range], with $_
being set to the text of each line in turn, without a trailing <EOL>.
Setting $_ will change the text, but note that it is not possible to
add or delete lines using this command. The default for [range] is the
whole file: “1,$”. Here are some things you can try:
:[range]perld[o] {cmd}
:perl $a=1
:perldo $_ = reverse($_);1
:perl VIM::Msg("hello")
:perl $line = $curbuf->Get(42)
Working with Markdown Tables in Vim
These macros assume a Markdown table in the following format.
1. Header Line
2. Table Description Line
- Any Easy Way To Create This:
- Put cursor on Header Line
- `:.t.` To Duplicate It
- `:s/[^|]/-/g` To Replace All Non-Pipes With '-'.
3. One Or More Table Lines
Each line must begin and end with the Pipe character (|).
| Header1 | Header2 |
|----|-|
| Col1 Item1 | Col2 Item1 |
| Col1 Item2 | Col2 Item2 |
After running \ta:
| Header1 | Header2 |
|------------|------------|
| Col1 Item2 | Col2 Item2 |
| Col1 Item1 | Col2 Item1 |
Note that either is a valid Markdown table. The Second example is more readable.
Include the following in your .vimrc.
| Command | Effect |
|---|---|
| \tx | Pretty Print Markdown-Style Tables, No Sort |
| \ta | Fancier Table Cleanup - Sorted By Reverse Col 1 |
" \ta Fancier Table Cleanup - sort -r
function! CleanMDTable()
normal! :set cmdheight=4
normal! vip:s@[-]\{5,\}@-----@ge
normal! vip:s@\s\+@ @ge
?|---?+1,/^$/!sort -r
?|---
normal! vip
'<,'>!column -c 200 -t -W 4 -s '|' -o '|'
normal! vip
?----?s![^|-]!-!g
nohlsearch
normal!
normal! :set cmdheight=1
endfunction
map <silent> <Leader>ta :call CleanMDTable()<CR>
" \tc TitleCase Line
nnoremap <silent> <Leader>tc :.s/\([- ][a-z]\)/\U\1/ge<CR>:.s/^\([a-z]\)/\U\1/e<CR>:noh<CR>
" \tf Format Paragraph
map <Leader>tf vip<CR>!fmt -u<CR>`{<CR>
" \tr Renumber Paragraph
map <Leader>tr :perl $a=0<CR>vip:perldo s/^\d+/++$a/e<CR>
" \ts Toggle Spelling
map <silent><Leader>ts :setlocal spell!<CR>
" \tx Pretty Print Markdown-Style Tables, No Sort
map <Leader>tx vip:s@[-]\{5,\}@----@ge<CR>vip:s/\s\+/ /ge<CR>vip!column -c 200 -t -s "\|" -o "\|"<CR>/----<CR>:.s/[^\|]/-/g<CR>:noh<CR>
" \tw TitleCase Word
nnoremap <silent><Leader>tw gUllguwb
" \xs trims spaces at end of line
nnoremap <silent><Leader>xs :%s/\s\+$//e<CR>
| Command | Effect |
|---|---|
| \xs | Trims Spaces At End Of Line |
| \tw | TitleCase Word |
| \ts | Toggle Spelling |
| \tr | Renumber Paragraph |
| \tf | Format Paragraph |
| \tc | TitleCase line |
| [//]: #( vim: set ai et nu sts=2 sw=2 ts=2 tw=78 filetype=markdown :) |