Vim Usage
Updated: October 19, 2024
Instructional guide for using vim.
Table of Contents
- Ex Commands
- Commands
- CTRLs
- Help
- Movement
- Insert Mode
- Searching
- Find
- Marking
- Folding
- Registers
- Windows
- Netrw
- Paging
- Shifting Lines
- Tips
- Nouns
- Text Objects
- Intermediate
THE BASICS
EX COMMANDS
: # command mode
:e filename # open new file in vim
:h dig # digraphs
# line numbers
:set nu # add line numbers
:set number
:set relativenumber
:set nonu # remove line numbers
:set nonumber
:set norelativenumber
:set nu! # toggles line numbers
# change color for line numbers
:highlight LineNr term=bold cterm=NONE ctermfg=DarkGrey ctermbg=NONE gui=NONE guifg=DarkGrey guibg=NONE
# change gutter column width
:set numberwidth=3
# save multiples files at once
:wa # save files and keep working
:xa # save files and close vim
:update # only saves if buffer has changed - :w writes even if nothing changed
control outputs with command . represents a line, % represents a whole file
:!{cmd} # execute shell command, display output
:r !{cmd} # place output below cursor
:w ! {cmd} # send current buffer as stdin to command
:.!{cmd} # replace current line with command output
:%!{cmd} # replace entire buffer with command output
:'<,'>!{cmd} # replace selection with command output
Examples:
:r !date
results in:
Sun Sep 15 23:34:18 EDT 2024
:.!jq .
results in: current files contents in nice json format using .name would filter out name from the file
after doing visual selection, use : then a command
:'<,'>!sort -n
results in: sort current selection of numbered list in ascending order
COMMANDS
^ # beginning of line
$ # end of line
cs)} # change paren with brace without adding whitespace
cs({ # change paren with brace with whitespace
y # yank (copy)
v # visually select (highlight)
p # paste
u # undo
^R # redo
r # replace
n # next in ascending order finder | shift ascending order
~ # invert casing | follow with number to flip amount of chars
. # repeat last command
; # repeat last key combo
G # move to end of buffer
ge # backwards by end of each word
gf # open file under cursor
gg # move to start of buffer
gi # restarts insert mode in last insert point used
gv # load last visual block
gx # open url under cursor
% # matching {}[]()
]] #
[[ #
][ #
[] #
} # move forward by paragragh
) # move forward by sentence
gu # lowercase
gU # uppercase
g; # go to last change (thing edited)
f # forward to whatever you specifiy f/ moves forward the next /
t # move till / so just before the slash (til)
# F and T go backwards for the above
eg closedcode/ #c2tc
\> # indent
< # unindent
CTRL
ctrl
6 # toggle between 2 most recent files
k # followed by 2 letters or 3-digit# to create a digraph
n # cycle ctrl-p results
p # get an autucomplete while typing a word
r # redo (multiple times) after a u
HELP
:help x # normal mode command
:help c_ # get help for a command mode key
:help i_ # get help for an insert mode key
:help : # command line command (:quit etc)
:help - # vim command argument (-r)
:help v_ # visual mode command
MOVEMENT
0 # beginning of line
h # left
j # down
k # up
l # right
b # move word backward
w # move word forward
e # end of a word
gg # first line
G # last line
gg=G # format indentation of whole file
226G # move to line 226
INSERT MODE
i # insert where the cursor is currently located
I # insert at beginning of line
a # insert at end of a word
A # insert at end of a line
o # insert a new line below
O # insert a new line above
s # substitute character under cursor and insert in it's place
S # delete line and insert in it's place
DELETE
x # delete character at cursor
X # delete character before cursor
d # delete that can be used with {motion}
D # delete from cursor to eol
dd # delete current line
c # delete and enter insert mode
cw # delete to end of current word and insert
cc # same as S
C # delete from cursor to eol and insert
:bd # buffer delete??
SEARCHING
* # turn the word you are on into a search term
# # search backwards
# n | N next on search terms forward and back
/ # search going forward
? # search going backward (swaps n movements around)
FIND
find: <name> # will fuzzy find if is in vimrc
:b <name> # if a file has been opened before it can be loaded easily from the buffer
# Use ^n and ^p to go back in forth in suggestion list
^w # cancel completion and stay in insert mode
^x^n # for just this file (auto-completion)
^x^f # file finder
^x^] # tags only
MARKING
:marks
:noh # turn off highlighting
v # mark freely
V # mark blockwise (rectangle)
^v # column selection
ggVG then "+y # mark whole document, then yank into system buffer, to paste outside of vim
ma #
FOLDING
Must be set with set foldmethod
there are six options:
- manual: manually define folds
- indent: more indent means a higher fold level
- expr: specify an expression to define folds
- syntax: folds defined by syntax highlighting
- diff: folds for unchanged text
- marker: folds defined by makers in the text
All fold commands start with z (looks like a piece of paper being folded if views from the side)
# CREATION
zf{motion} # create a fold
{Visual}zf # use V to select section to turn into a fold
zF{count} # create fold for count lines
# DELETION only work when when set foldmethod=manual
zd # deletes one fold at the cursor
zD # deletes recursively at the cursor
zE # remove all folds in the window
# OPEN
zo # open one fold under the cursor, can use count with it
zO # open all folds recursively under the cursor
za # when on closed fold, open it, can repeat za to open the nested one too
zA # when on closed fold, open all folds recursively
zv # view cursor line, open enough folds to make the line of cursor not folded
zR # open all folds
zn # fold none, reset foldenable, all folds will be open
# CLOSE
zc # close one fold under the cursor, can use count with it
zC # close all folds recursively under the cursor
zm # fold more, subtract one from fold level, foldenable will be set
zM # close all folds, foldenable will be set
# RESETS
zi # toggle foldenable setting
zN # fold normal, set foldenable, all fold will be as they were before
# MOVEMENT
[z # move to start of current open fold
]z # move to end of current open fold
zj # move downwards to start of new fold
zk # move upwards to the end of previous fold
REGISTERS (CLIPBOARD)
:registers # lists registers and their content
^r {register} # insert contents of numbered or named register
example
visual select and then "ky then "kp
a-z # yanked text replaces content of register
A-Z # yanked text appends to content of register
" # the unnamed or default register
+ # the system defualt register
* # select/drop registers
_ # /dev/null (used to avoid wiping out register)
WINDOWS
Windows are not buffers. They are a view onto a buffer. You can have multiple windows viewing into a single buffer. If the buffer is edited in one window, it will show up in the other windows viewing that same buffer. Closing a window does not close the buffer!
ctrl+w then:
o # unsplit windows = only window
s # horizontal split
v # vertical split
c # close a split
x # swaps two windows with each other
j # move cursor to window below
k # move cursor to window above
h # move cursor to window to the left
l # move cursor to window to the right
b # move cursor to bottom window
p # move cursor to previous window (jump back and forth between two windows)
J # move window below
K # move window above
H # move window to the left
L # move window to the right
:sp :split <file> # open file in new split window
:vsp # vertical split
:ls # lists buffers
NETRW
:Explore # opens netrw in current window; can use :Ex
:Sexplore # opens netrw in a horizontal split; can use :Sex
:Vexplore # opens netrw in a vertical split: can use :Vex
:Hex! # horizontal split opposite side
:Vex! # vertical split opposite side
:Tex # new tab, dir of current open buffer
:Lex # vertical split full height current working dir
:Lex! # vertical split current working dir opposite side
i # cycle through view types
I # toggle banner
qf # display information on a file
enter # opens file or directory
o # opens in new horizontal split
v # opens in new vertical split
t # opens in new tab
p # preview the file without moving cursor from netrw
x # open file or dir with system default application
- # go up one directory
u # go back to previously visited directory
U # go forward to subsequently visited directory
c # make browsing directory the current working directory
gn # make the directory under the cursor the top of the tree
gh # toggle hidden files
a # cycle between all files
s # cycle sort order (name, time, filesize)
r # reverse sort order
% # create new file
d # create new directory
D # delete file/directory under cursor
R # rename/move file/directory
mb # bookmark current directory
gb # go to previous bookmarked directory
mf # toggle whether a file/directory is marked
mt # mark directory under cursor as copy target
mc # execute copy operation
mu # unmark all marked items
mF # unmark files
PAGING
^F # full page down
^B # full page back up
^U # half page up
^D # half page down
H # head of screen
M # middle of screen
L # last of screen
SHIFTING WHOLE LINES
:m+ # move a line down
:m-2 # move a line up one
ddkP # swap line with the line above (another alternative)
HOLY GRAIL (tips)
:help holy-grail
:q! # quit without saving | ZQ
:wq # save and quit a file | ZZ
:wqa # save and quit all open files in vim
:w !sudo tee % # used to make a file write when you forgot to open it with sudo
:w !sudo dd of=% > /dev/null # does same thing as tee
:w !cmd # pipes contents of current buffer to another command
NOUNS
w # word
b # back by a word
2j # down 2 lines
f,F # find next occurence
t,T # find upto but not including next occurence
? # reverse search
TEXT OBJECTS
aw # a word (works from anywhere in a word, includes surrounding whitespace)
iw # inner word (works from anywhere in a word, does not include surrounding whitespace)
it # inner tag (the contents of an HTML tag)
i" # inner quotes
ip # inner paragraph
as # a sentence
INTERMEDIATE
%s # substitute
:%s/searchwordtoreplace/newword/g # g applies to all instances that word is found c for confirmations
> # put in a tab space >j would do line underneath also
ciw # replace a word