Updated December 28, 2023

vim cheat sheet

When vi or vim opens a file, it creates a swap file with the extension .swp. The swp files are being stored in the recycle bin on my Synology NAS drive.

vi stands for "visual" editor and vim stands for visual improved. Most Linux distros default to a version of vim. Vim has three major advantages: (1) you can use the arrow keys to move the cursor, (2) it has a visual mode to select and highlight a block of text, and (3) you can use the mouse to move the cursor and highlight text. Rather than doing this by creating an alias, the raspberry pi and probably other distros, used symlinks (symbolic links - pointers) to point the vi command to vim.basic . To see where symlinks point to use the ls -l option.

Insert Mode
i- insert before the cursor
I- insert at the beginning of the line
a- append after the cursor
A- append at the end of the line
o- open new line below
O- open new line above

 

Command Mode - Undo
u- Undo last change
U- Undo all changes on line

 

Cursor - All Modes
- move right one character
- move left one character
- move down one line
- move up one line
<shift>↓- move page forward
<shift>↑- move page backward
<shift>↓ & <shift>↑ do not work in ssh
<fn>delete- delete character

 

Cursor - Command Mode
j- move down one line
k- move up one line
h- move left one charcter
l- move right one charcter
^- move to beginning of line
$- move to end of line
w- move forward a word
b- move backwards a word
H- Home: first char on page
M- Middle of page
L- Last char on page
<ctrl>f- move page forward
<ctrl>b- move page backward
<ctrl>d- move 1/2 page forward
<ctrl>u- move 1/2 page backward
gg- move to beginning of file
G- move to end of file
25G- move to line 25
:25- move to line 25

 

Delete & Replace
x- delete character
r- delete and replace char.
delete- delete char. before cursor
dw- delete word

 

Delete, Yank (copy) & Put (paste)
dd- delete current line
yy- yank (copy) current line
6dd- delete 6 lines
6yy- yank (copy) 6 lines
:4,17d- delete lines 4-17
:4,17y- yank (copy) lines 4-17
p- put (paste) after cursor
ctrl-shift-V- paste from Rpi clipboard

 

visual mode
v char-wise visual
v line-wise visual
ctrl v block-wise visual
esc exit visual mode
ctrl c exit visual mode
"+y- yank to the Apple clipboard
"+p- paste from Apple clipboard
"- means register.
+- specifies the system clipboard register.
y/p- yank/paste

 

Find
:/<string>- search forward
?<string>- search backward
:/- next forward
:?- next backwards
n- next
N- previous

 

Substitute - Find & Replace
:s/<findtext>/<replacement_txt>- current line
:%s/<findtext>/<replacement_txt>- all lines
:#,#s/<findtext>/<replacement_txt>- range

 

Write & Quit
:w write (save) file
:w file_name- write (save) file to file_name
:qquit
:q!quit without saving
:wqwrite & quit
:ZZwrite & quit

 

Editing Multiple Files
:e file_name edit file_name
:edit file_name edit file_name
:bn switch to next buffer
:bp switch to previous buffer
:buffers list buffer numbers
:bbuffer_number switch to buffer_number

 

Split Window
:vsplit file_name vertical split file_name
:split file_namehorizonal split file_name
:vsplit vertical split screen
:splithorizonal split
:set splitright split to the right
:set splitbelowsplit below
<ctrl>w<ctrl>w switch windows
<ctrl>w arrow_keys (left, right, up, down) window
:e <sp><tab> select file
:b <sp><tab>select buffer (file)

 

Code Editing
=- reindent the selected lines
=G- reindent from cursor to end of file
gg=G- reindent the file - two commands
J- Join with Line below
:set number- show line numbers
:set autoindent- auto indent on
:set shiftwidth=3- indent 3 spaces
:set nonumber- hide line numbers
:set number!- hide line numbers
:set noautoindent- auto indent off

 

Text Wrap
:set wrap wrap text
:set nowrap do not wrap
used with set wrap
:set linebreak wrap at end of words
:set nolinebreak go back to char wrap

 

Hidden Characters
:set list- show hidden characters
:set nolist- hide hidden characters
End of Line:
Mircosoft <cr><lf>
Unix & Mac OS <lf>
Old Mac OS <cr>
:set ff?- what is the file format ie dos, unix
vi -b filename (binary)

 

Apple vi (vim) - nothing is set
:syntax on cuts on coloring keywords
:set mouse=a activates using the mouse

.vimrc

My .vimrc file:

.vimrc

References

  1. vim Tutorial
  2. The vi Editor Tutorial - TutortialPoint
  3. Youtube: Vim - Working with Multiple Files (at the same time)
  4. Youtube: Handling Multiple Files in vim
  5. Youtube: Learn How to Use the Vim Text Editor (Episode 1) - Basic Usage (and how to exit Vim)
  6. Youtube: Learn How to Use the Vim Text Editor (Episode 2) - Modes, Navigation and More
  7. Youtube: Learn How to Use the Vim Text Editor (Episode 3) - Getting Started with Buffers
  8. Youtube: Learn How to Use the Vim Text Editor (Episode 4) - Visual Mode
  9. Youtube: Learn How to Use the Vim Text Editor (Episode 5) - Splitting your Vim Window
  10. Youtube: Learn How to Use the Vim Text Editor (Episode 6) - Tips, Tricks and How to Configure Vim