Modes
Vim offers multiple modes, including Normal, Insert, Command and Visual.
Normal mode is the mode Vim starts in and which is used for navigation and text manipulation. You can return to normal mode by pressing ESC.
Insert mode is used for inserting text and can be accessed several ways, the most common being "i" to Insert, "a" to Append, "c" to change.
Command mode is to enter commands such as searching, reading files, saving and exiting. It is accessed by ":" key.
Visual mode like normal mode is used for navigation and text manipulation, there are additional sub-modes - visual, visual-line and visual-block modes. Visual mode is accessed by pressing a variety of "v" key combinations.
In the various modes most commands execute immediately, the exception to this is Command mode where you need to press the Enter key to execute. |
Insert Mode
Key |
Action |
|
insert at cursor |
|
insert at the beginning of the line |
|
append after the cursor |
|
append at the end of the line |
|
open blank line below current line (no need to press return) |
|
open blank line above current line |
|
to exit insert mode (and return to normal mode) |
Cursor movements can be combined with insert mode commands
* Bi
- insert at the start of the current word
* ea
- append at the end of the current word
* Wi
- insert at the start of next word
* jI
- insert at the start of next line
* jA
- append at the end of next line
Command Mode
Key |
Action |
|
enter command mode |
|
to action command |
|
to exit command mode (and return to normal mode) |
The command is only executed after pressing the Enter
key.
Visual Mode
Key |
Action |
|
enter visual mode |
|
enter visual-line mode |
|
enter visual-block mode |
|
to exit visual mode (and return to normal mode) |
Examples of usage, after making visual selection perform the following actions:
visual-line mode
* delete complete lines - d
* yank (copy) - y
visual-block mode
* Insert text to start of multiple lines - I
, <text,whitespace>, Esc
, Esc
* Delete characters from the start of multiple lines - d
Cursor Movement
Key |
Action |
|
move left (also left cursor) |
|
move down (also down cursor) |
|
move up (also up cursor) |
|
move right (also right cursor) |
|
jump forward to start of words (punctuation considered words) |
|
jump forward to start of words (spaces separate words) |
|
jump forward to end of words (punctuation considered words) |
|
jump forward to end of words (spaces separate words) |
|
jump backward to start of words (punctuation considered words) |
|
jump backward to start of words (spaces separate words) |
|
jump to start of line (zero) |
|
jump to first non-blank character of line |
|
jump to end of line |
|
go to end of file |
|
go to start of file |
|
move to top of screen (no screen movement) |
|
move to middle of screen (no screen movement) |
|
move to bottom of screen (no screen movement) |
Prefix a movement command with a number to repeat the move that number of times.
Editing
Key |
Action |
|
delete character from right |
|
delete character to left |
|
replace a single character (does not use insert mode) |
|
replace multiple characters |
change c+option |
start change command (will enter Insert mode) |
|
change to the end of word |
|
change an entire line |
|
change to the end of line |
|
change to end of file |
delete d+option |
start delete command (will stay in Normal mode) |
|
delete word |
|
delete entire line |
|
delete to the end of the line |
|
delete to the end of the file |
|
delete character at cursor and substitute text (enters Insert mode) |
|
delete line at cursor and substitute text (same as cc) |
|
transpose two letters (technically a combo to delete and paste) |
|
undo |
|
redo |
|
repeat last command |
|
join current line with line below |
Commands seem similar but there are slight differences. Some command will execute and can be repeated by pressing the same key. Other commands will perform the action and then automatically switch to Insert mode. Also, a command can be performed in multiple modes with slightly different keystrokes.
Commands are combined with cursor movement commands, so it is possible to:
* dgg
- delete to beginning for file
* c5w
- change 5 words
* 4dj
- delete current and 4 other lines
|
|
Cut, Copy, Paste
Key |
Action |
delete |
delete, i.e. cut (see previous) |
yank (y+option) |
yank, i.e. copy |
|
yank word |
|
yank line |
|
yank 2 lines |
|
yank current and next line |
put (p+option) |
put, i.e. paste |
|
put clipboard contents after cursor |
|
put clipboard contents before cursor |
|
paste 2 copies |
Search and Replace
Key |
Action |
|
search for pattern forward from cursor |
|
search for pattern backward from cursor |
|
repeat search in same direction |
|
repeat search in opposite direction |
|
replace all old with new throughout file |
|
replace all old with new throughout file with confirmations |
|
search from start of file to current cursor position, replace all old with new |
|
search from current cursor position to end of file, replace all old with new |
Open, Save and Exit
Key |
Action |
|
write (save) the file, but don't exit |
|
write to new file |
|
write lines 1-5 to new file |
|
write (save) and quit |
|
quit (fails if anything has changed) |
|
quit and discard changes |
|
read in file to current cursor position |
|
read in the first 5 lines of file to current cursor position |
|
edit file in new buffer |
|
edit file in new buffer and split window |
|
switch between windows |
|
perform shell command, output will overwrite current line |
Marks
|
list of marks - includes mark name, line, column and text |
|
set current position for mark 'a' |
|
jump to position of mark 'a' |
|
jump start of line of mark 'a' |
|
yank from current position to mark 'a' |
Can use a variety of commands from current position to position of mark 'a'. Most often use this command to copy and paste multiple lines of text. Similar results can be achieved in Visual mode.
Ranges
|
perform action on current line |
|
perform action on line 1 |
|
perform action on lines 1-5 |
|
perform action on last line |
|
perform action on all lines |
|
perform action on lines 5 to last |
|
perform action on current to last line |
|
perform action on line below current to last |
|
perform action on line below to 5 lines below current line |
|
peform action from mark a
to mark b
|
Ranges are combined on the Command line to perform a task on the lines specified. The default range, for most commands, is the current line. While for other commands the default range is all lines.
:1,5d
- delete lines 1-5
:5,.y
- yank lines 5 to current
:.,$s/old/new/g
- replace old with new from current line to end of file
'a,'by
- yank from 'a' to 'b'
Miscellaneous
|
display line numbers |
:set number!
or :set nonumber
|
turn off line numbers |
:colorscheme <colorscheme>
|
change colorscheme |
|
show version and other information |
|
show hidden characters |
|
do not show hidden characters |
Ctrl-V
, cursor highlight, I
, #
, Esc
, Esc
|
multi-line comment |
Ctrl-V
, cursor highlight, x
|
delete multi-line comment |
|
remove search highlight |
|
replace DOS carriage returns (if dos2unix
is not installed) |
Commands can be abbreviated, e.g. :nohl
is shorthand for :nohlsearch
Configuration
Global defaults can be configured in /etc/vimrc
but setting personal defaults is preferred.
Shared colour scheme files are located in /usr/share/vim/vimXX/colors
Personal defaults can be configured in ~/.vimrc
Personal colour scheme files added to ~/.vim/colors
Specifiy default colour scheme in ~/.vimrc
colorscheme <colorscheme>
|
|
Created By
Metadata
Favourited By
Comments
managedkaos, 15:22 23 Jul 21
Nice!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by PeterCeeAU