- Reselect last visual block mode selection:
gv
- Select entire current line:
V
- Can be used in both visual and command modes
- When used in visual mode, all lines with at least 1 selected character will be selected in full
- When used in command mode, whatever line the cursor is on will be used for selection
- Select quoted string (incl. quotes):
va"
- Select 'a'll/'a'n of object including white space
- Select contents of quoted string:
vi"
- Select an 'i'nner object without white space
- Delete quoted string (incl. quotes):
da"
- Delete 'a'll/'a'n of object including white space
- Delete contents of quoted string:
di"
- Delete an 'i'nner object without white space
- Delete contents of XML tag:
dit
- Delete an 'i'nner contents of 't'ag
- Delete entire code block:
d%
- With cursor at beginning of first line
- Delete from current line to EOF:
dG
- Delete from current line to BOF:
dgg
- Delete all blank lines in file:
:%!grep .
- Works because
.
is regex for any one character - Won't necessarily work on Windows (but who cares)
- There are other ways to do this in vim, but this is memorable and versatile
- Works because
- Delete blank lines in range:
:1,5!grep .
- Delete blank lines in marker range:
:'x,.!grep .
- Replace multiple blank lines with one:
:%!cat -s
- From the
cat
manpage:-s : Squeeze multiple adjacent empty lines, causing the output to be single spaced.
- From the
- Delete matching lines:
:g/pattern/d
- Invert with:
:g!/pattern/d
- Invert with:
- Delete empty lines:
:g/^$/d
- Delete from cursor to end of line:
D
- Move cursor to previous location:
^o
- Jump to previous empty line:
{
- Jump to next empty line:
}
- Jump to start of text on line (based off indentation) and insert:
I
- Jump to auto indent and enter insert mode:
S
- Rather than inserting at the beginning of the line, this will insert where the line should begin
- Find case-insensitively:
/word\c
- Make new line after current and enter insert mode:
o
- Make new line before current and enter insert mode:
O
- Jump to right after end of word and enter insert mode (like
A
does for lines):ea
- Scroll to move current line to center of screen:
zz
- Scroll current line to top:
zt
- Scroll current line to bottom:
zb
- Scroll current line to top:
- Jump to next fold:
zj
- Jump to previous fold:
zk
Where x
is the character you'd like to jump to:
- Jump to next occurrence of character on current line:
fx
- Jump to previous occurrence of character on current line:
Fx
- Next occurrence:
;
- Previous occurrence:
,
- Set a marker:
mx
- Where
x
is a character to name marker
- Where
- Navigate to exact location of marker: `x (I couldn't escape this at the beginning of a codeblock)
- Navigate to beginning of line of marker:
'x
- An uppercase and lowercase marker are distinct
- Jump to matching bracket:
%
- Increment selected number:
^a
- Decrement selected number:
^x
- When doing a find and replace, add a
c
to confirm each substitution- For example,
:%s/foo/bar/gc
- For example,
- You can surround the current word with quotes like so:
ciw""<esc>P
- Replace the character right under your cursor with:
r<char>
- Removes the need to switch to insert mode and back again
- In place of using a
%
in a substitution command (which selects entire file - ex::%s/foo/bar
), you can use ranges- Apply from first to current line:
:1,.s/foo/bar
- Apply from current to last line:
:.,$s/foo/bar
- Apply from current to marker line:
:.,'xs/foo/bar
- Apply from first to current line:
Adapt the following examples as needed:
~
: Changes the case of current characterg~~
: Invert case to entire lineg~w
: Invert case from cursor to end of wordg~iw
: Invert case of entire current wordg~G
: Invert case for rest of fileguG
: Change to lowercase for rest of filegU)
: Change until end of sentence to upper casegu}
: Change to end of paragraph to lower case
Vim can be used as a terminal emulator multiplexer. Primarily, this is for viewing multiple files at once, but could also be used for having both terminal(s) and file(s) open in one vim process.
- Open a new vim window below (split screen "hot dog"):
^ws
- Open a new vim window beside (split screen "hamburger"):
^wv
- Open a new terminal above (split screen "hot dog"):
:term
- Open a new terminal beside (split screen "hamburger")
:vert term
- Swap ('e'xchange) current window with next:
^wx
In addition to windowing, vim also supports tabulation.
- Open an empty buffer in a new tab:
:tabnew
- Open an existing file in a new tab:
:tabedit filename
- Go to next tab:
gt
- Go to previous tab:
gT
- Close a tab with the normal buffer close:
:q
- List every file open in each tab:
:tabs
Folds in vim are ways to hide blocks of text/code when you don't need to see them. In other words, this feature allows you to "collapse" a function.
- Create and collapse a 'f'old:
zf
- This expects that you've selected the text you'd like to fold in visual block mode
By placing your cursor on any part of an existing fold, you can:
-
Collapse or 'c'lose the fold:
zc
-
Expand or 'o'pen the fold:
zo
-
Open all folds:
zR
-
Close all folds:
zM
-
Eliminate/delete all folds:
zE
-
Automatically create fold for each stage:
:%g/^\s*stage('.*{/normal! $zf%
Vim supports a VS code-like prediction menu. By default it's hidden but there's plugins to automatically show it when typing.
- Open predictive menu while in insert mode:
^p
- If opened by mistake, to put original text back at cursor:
^n
- Then resume typing or press escape
- Cycle forward through predictions:
^p
- Cycle backward through predictions:
^n
- Use current prediction:
↩
or just start typing
Open NetRW: :Ex
Make a new directory: d
Rename file/directory: R
The following Vim command can be used to format the current Terraform file in the buffer:
silent execute '%!'.g:terraform_binary_path.' fmt -no-color -'
You could also run this on a write hook like so:
https://vi.stackexchange.com/questions/25299/how-to-properly-hook-an-action-on-save-in-vim
I made some progress on this but I'm not sure that it'll work due to hooking onto the BufWritePre
event. It seems that you can't make changes after this event has been called. I'll maybe look into this later.
"For *.tf:
"autocmd BufWritePre tf,terraform-vars execute '%!terraform fmt -no-color -'
autocmd BufWritePre tf,terraform-vars silent execute '%!terraform fmt -no-color -'
- Pretty prent a JSON file open in the editor:
:%!python -m json.tool
- Delete a word and enter insert mode "'c'hange 'w'ord":
cw
- Deletes everything right of cursor, so if you want to replace an entire word, run at beginning of it
- Same idea applies to anything else, for instance:
ci{
orca(
- Swap current and line below:
ddp
- If syntax highlighting bugs out, refreshing syntax can help:
:syntax sync fromstart
- Yank from cursor to end of line:
Y