Vim - A Comprehensive Guide

Master Vim: a comprehensive guide covering file management, movement, insertion, editing, search & replace, multiple windows, buffers, and more. Learn essential Vim commands and techniques for efficient text editing.

Vim

File Management

Learn the essential commands for managing files within Vim.

:e              reload file
:q              quit
:q!             quit without saving changes
:w              write file
:w {file}       write new file
:x              write file and exit

Movement

Mastering Vim's movement commands is key to efficient editing.

    k
  h   l         basic motion
    j

w               next start of word
W               next start of whitespace-delimited word
e               next end of word
E               next end of whitespace-delimited word
b               previous start of word
B               previous start of whitespace-delimited word
0               start of line
$               end of line
gg              go to first line in file
G               go to end of file
gk              move down one displayed line
gj              move up one displayed line

Insertion

Learn how to enter insert mode and the different ways to insert text.

#   To exit from insert mode use Esc or Ctrl-C
#   Enter insertion mode and:

a               append after the cursor
A               append at the end of the line
i               insert before the cursor
I               insert at the beginning of the line
o               create a new line under the cursor
O               create a new line above the cursor
R               enter insert mode but replace instead of inserting chars
:r {file}       insert from file

Editing

Explore Vim's powerful editing commands for efficient text manipulation.

u               undo
yy              yank (copy) a line
y{motion}       yank text that {motion} moves over
p               paste after cursor
P               paste before cursor
<Del> or x      delete a character
dd              delete a line
d{motion}       delete text that {motion} moves over

Search and Replace

Learn to use Vim's powerful search and replace functionality.

:s/foo/bar/    replace the first match of 'foo' with 'bar' on the current line only
:s/foo/bar/g   replace all matches (`g` flag) of 'foo' with 'bar' on the current line only
:%s/foo/bar/g  replace all matches of 'foo' with 'bar' in the entire file (`:%s`)
:%s/foo/bar/gc ask to manually confirm (`c` flag) each replacement

Repeating Commands

Learn how to repeat commands for increased efficiency.

# Preceding a motion or edition with a number repeats it 'n' times
# Examples:
50k         moves 50 lines up
2dw         deletes 2 words
5yy         copies 5 lines
42G         go to line 42

Multiple Windows

Manage multiple files and views simultaneously with Vim's window management features.

:e filename      - edit another file
:split filename  - split window and load another file
ctrl-w up arrow  - move cursor up a window
ctrl-w ctrl-w    - move cursor to another window (cycle)
ctrl-w_          - maximize current window
ctrl-w=          - make all equal size
10 ctrl-w+       - increase window size by 10 lines
:vsplit file     - vertical split
:sview file      - same as split, but readonly
:hide            - close current window
:only            - keep only this window open
:ls              - show current buffers
:.! <command>    - shell out

Buffers

Efficiently manage multiple files using Vim's buffer system.

# move to N, next, previous, first last buffers
:bn              - goes to next buffer
:bp              - goes to prev buffer
:bf              - goes to first buffer
:bl              - goes to last buffer
:b 2             - open buffer #2 in this window
:new             - open a new buffer
:vnew            - open a new vertical buffer
:bd 2            - deletes buffer 2
:wall            - writes all buffers
:ball            - open a window for all buffers
:bunload         - removes buffer from window
:taball          - open a tab for all buffers

Navigation and Search

Enhance your workflow with these additional navigation and search commands.

# Pointers back
ctrl-o

# Pointers forward
ctrl-i

# Super search
ctrl-p

Advanced Techniques

Explore more advanced techniques to further enhance your Vim skills.

# To sort  a visual range on column 1 as a number:
:'<,'>!sort -gk 1 -t ,

# Map (in normal mode) the F2 key to a bash call `uuidgen`, then trim the `\n`
# from the result, and put that in the expression register `"=`, then put that
# before the cursor:
nmap <F2> "= system("uuidgen")[:-2]<C-M>P

# Delete every line that has a FOO in it. See `:help global`.
# The _ in the d _ command ensures registers and clipboards are not changed.
:g/FOO/d _