March 12, 2018

Vim Hints

Vim is an editor with a very large operation set which expands even further when plugins are factored in. Due to this vastness it is easy to forget some useful commands and capabilities.

This post is mainly intended to remind myself of some of these capabilities since my brain can house only so much Vim knowledge.

Normal mode

Open the URL under the cursor in a browser.

gx

Delete the character immediately to the left of the cursor.

X

Insert Mode

Carry out a one time normal mode command. The zz-centering command is useful in this situation.

Ctrl-o<command>

Insert from the named register. The 0 yank register is a useful register to insert from.

Ctrl-r<reg>

Insert a mathematical value, for example 48343 + 1684.

Ctrl-r=<mathematical expression>

Visual mode

Repeat the last visual selection.

gv

Change current visual selection to inversed or forced-lower or forced-upper case.

~
u
U

Visually select current inner or around paragraph.

vip
vap

Visually select current inner or around sentence.

vis
vas

Visually select current inner or around tag.

vit
vat

Sort visual selection.

:'<,'>sort

Unique the visual selection.

:'<,'>sort u

Substitute old with new in a rectangular visual block.

:'<,'>s/\%Vold/new/c

Command Mode

Undo to an earlier time, for example 50s or 10m (50 seconds or 10 minutes).

:earlier <time>

Redo to a later time, for example 50s or 10m.

:later <time>

Read in command line generated output.

:r !<command>

For example, append all txt filenames from the current directory.

:r !ls *.txt

Delete all lines containing pattern.

:g/<pattern>/d

Delete all lines not containing pattern.

:v/<pattern>/d

Cursor Positioning and Movement

Move the current cursor line to the top of the screen.

zt

Move the current cursor line to the center of the screen.

zz

Move the current cursor line to the bottom of the screen.

zb

Scroll horizontally half a screen to the right.

zL

Move the cursor to the top line of screen (High).

H

Move the cursor to the middle line of screen (Mid).

M

Move the cursor the bottom line of screen (Low).

L

Set a mark in the current file.

mm

Return to the m mark in the current file.

'm

Set a global mark in any file.

mM

Return back to the global M mark.

'M

Move forward and backward between paragraphs.

{
}

Move forward and backward between sentences.

(
)

Move to the end of the previous word or WORD.

ge
gE

Scroll window up one line.

Ctrl-e

Scroll window down one line.

Ctrl-y

Navigate to definition under cursor, often by using tags.

Ctrl-]

Navigate back to the previous location (out).

Ctrl-o

Navigate forwards (in). This is the opposite of Ctrl-o.

Ctrl-i

Spelling

Suggest a spelling correction.

z=

Add word under cursor to your local spell file.

zg

Move to next spelling error.

]s

Move to the previous spelling error.

[s

Files To Edit

Edit all txt files in the current directory hierarchy.

% vim  **/*.txt

When using Bash, please enable globstar in your ~/.bashrc file via the shopt -s globstar statement.

Edit all files that the ripgrep utility recursively finds contains the term FOO.

% vim $(rg -l FOO)

Grepper plugin

Usage hint for the vim-grepper plugin.

Restrict a ripgrep based search to a particular filetype.

:GrepperRg -t<type> <pattern>

Ragtag plugin

Insert mode usage hints for the vim-ragtag plugin.

Close the previous open tag.

<CTRL-x>/

Convert the current word into open and close tags.

<CTRL-x><Space>

Same as previous except split over multiple lines.

<CTRL-x><Enter>

Unimpaired plugin

Usage hints for the vim-unimpaired plugin.

Navigate backward and forward through the buffer list.

[b
]b

Navigate up and down through the quickfix list.

[q
]q

Navigate up and down through the location list.

[l
]l

Navigate backward and forward through the file list.

[a
]a

Add a blank line above or below the current line.

[<Space>
]<Space>

Move the current line, or visual lines, up dow down.

[e
]e

Paste above or below the current line.

[p
]p

Paste above or below the current line and increase the indent.

>P
>p

Paste above or below the current line and re-indent.

=P
=p

Toggle search highlights.

yoh

Toggle relative line numbers.

yor

Toggle line wrapping.

yow

Toggle cursor cross-hairs.

yox

Fugitive plugin

Usage hints for the vim-fugitive plugin.

Show :Gstatus help.

g?

Stage or unstage a file in :Gstatus.

-

Toggle inline diff in :Gstatus.

=

Display a vdiff on the current file in :Gstatus.

dv

Commit the current staged files in :Gstatus.

cc

Ruby plugin

Usage hints for the vim-ruby plugin.

Navigate up and down to the start of method definitions.

[m
]m

Navigate up and down to the end of method definitions.

[M
]M