Vim Cheat Cheat

Posted on  by 



Knowing basic Vim commands is useful as most Linux distributions have it installed by default. Once you get use to using Vim commands, mastering Vim should be simple. Until then, keep a Vim cheat sheet at hand. Everyday Vim – A Basic Vim Commands Cheat Sheet Vim is a pretty great text editor, but learning to use it effectively can be a challenge. Even if you keep a quick-reference card or cheatsheet around, it can be difficult to figure out which commands are the most useful.

Vim is a very powerful editor with an enormous set of functions. This article describes the basic commands that are essential to working with Vim. Time you spend on learning Vim commands and functions will pay big dividends for you in your future use of the program. It is essential for you to not just read this article, but also to try all the actions in some test text file so that you can better learn and remember them.

Modes

There are three text operation modes in Vim:

  • Command mode: all keystrokes are interpreted as commands.
  • Insert mode: used for text input.
  • Visual mode: used for highlighting text or a text block, this mode can be considered as a submode for command mode.

To switch from Insert mode or Visual mode to Command mode, press the Esc key.

Cheat

To switch from Command mode to editing mode, press one of the following keys:

  • i – switches to Insert mode before the current cursor position.
  • a – switches to Insert mode after the current cursor position.
  • I – jumps to the first non-space symbol in the current line and switches to Insert mode.
  • A – jumps to the last symbol in the current line and switches to Insert mode.

To switch from Command mode to Visual mode, press one of the following keys:

  • v – switches to Visual mode (symbols highlighting).
  • V – switches to Visual mode (lines highlighting).

Ctrl+v – switches to block Visual mode (highlighting of rectangular text fields)

Commands

Replacing commands

These commands do not work in Insert mode, but in all three modes you can use cursor-moving keys. If you remember at least some of these commands, you will be able to easily move from point A to point B. One important thing to note is that these commands set the range for other commands.

h, j, k, lmoving through the text in different directions
|, 0, homemove the cursor to the beginning of the line
^move to the first non-space symbol in the line
$, endove to the end of the line
mmove one-half of the screen width
gmove to the lower line
emove to the end of the current word
Emove to the end of the current word ignoring punctuation marks
move to the first non-space symbol in the line above
+, Entermove to the first non-space symbol in the line below
Gmove to the last line
Gmove to line
Hmove to the first screen line
Mmove to the middle screen line
Lmove to the last screen line
wmove to the beginning of the next word
bmove cursor to the beginning of the current word
Bmove to the beginning of the word ignoring punctuation marks
gemove to the end of the next word
gEmove to the end of the previous word ignoring punctuation marks
( –move to the previous sentence (to the dot)
)move to the next sentence (to the dot)
{move to the previous paragraph (to the empty line)
}move to the next paragraph (to the empty line)
3wmove to beginning of the fourth word counting from the current position
4emove to the end of the fourth word counting from the current position
:move to line number
%move to the next pair of brackets
:jushift list
Ctrl+escroll one line down
Ctrl+dscroll 1/2 of the page down
Ctrl+fscroll 1 page down
Ctrl+yscroll 1 line up
Ctrl+uscroll 1/2 of the page up
Ctrl+bscroll 1 page up

Deleting commands

Del, xdelete the symbol under the cursor
Xdelete the symbol before cursor
xdelete of symbols, beginning with the one under the cursor
Cdelete all from cursor to the end of the line and switch to Insert mode
Ddelete all from the current symbol to the end of the line
ddelete n+1 lines
DELdelete the next symbol
ccchange the whole line
c$change the line beginning from the cursor position to the end
ddelete the symbol from the current cursor position to the position set by the next command (for example: d$ deletes all the symbols from the current cursor position to the end of the line)

Highlighting commands

v + hjkltext highlighting
SHIFT + vline highlighting
vipparagraph highlighting
viwword highlighting
Shift+v or 0v$line highlighting
^v$line highlighting beginning with the first non-space symbol
vi(highlight everything between the nearest round brackets (similarly, use ‘vi[‘ and ‘vi{‘ for square and curved brackets)
va(highlight all between the nearest round brackets including the brackets themselves
v2jhighlight all up to two lines down
vt,highlight all up to the nearest coma
:syntax onenable highlighting syntax
:syntax offdisable highlighting syntax

Cut / paste commands

Ppaste before the current line
ppaste after the cursor
[ppaste before the cursor
ipaste text before the cursor
Ipaste text in the beginning of the line
CTR+p or CTR+nautomatic text insert (in the editing mode)
CTR+r,=,paste expression, for wxample 5*2 – 3 (in the editing mode)
aadd text after the cursor
Aadd in the end of the line
oadd line right after the current one
Oadd line after the current one
dipcut paragraph
di(cut the content of round brackets
da(cut the content of round brackets and the brackets themselves
ddcut the line where the cursor is. Before any command you may enter a number modifier that denotes the number of times to perform the command. For example: 3dd – deletes three lines beginning with the current one.

Copying commands

Vim Cheat Cheat Sheets

yycopies the line. Also you may use a number modifier
ycopy n+1 line
y2ycopy two lines
yiwcopy the word
ycopy symbols from the current position to the position set by the next command

Other editing commands

<<left shift the current line
>>right shift the current line
Junification of the current line with the next one
Rreplace symbols beginning with the current position
xpexchange positions of two symbols

Vim cases

Vushift the line to the lowercase
VUshift the line to the uppercase
g~~invert case
vEUshift the case of the word under the cursor to the uppercase
vE~invert the case of the word
ggguGshift all the text into the lower case
:setignorecasecase-independent search
:set smartcaseignore register while searching if there are no uppercase symbols in the target expression
:%s/<./u&/gshift the first letter of each word into the uppercase
:%s/<./l&/gshift the first letter of each word into the lowercase
:%s/.*/u&shift the first letter of the first word of each line into the uppercase
:%s/.*/l&shift the first letter of the first word of each line into the lowercase

Latest changes cancelation

Udiscard all changes that affect the current line
u, :u[ndo]discard the previous action (undo)
CTR-R, :red[o]cancel the discard of the previous action (redo)

Saving

To save the changes in the file, you need to do the following: switch to the normal mode by pressing enter the command :write , or its shortened variant :w

Word wrap

Vim Cheat Sheet Pdf

:set wrapenable word wrap (default)
:set nowrapdisable word wrap

Printing

:ha[rdcopy]print document
:set printoptions=duplex:offdisable duplex printing

Folding commands

zcfold block
zounfold block
zMclose all blocks
zRopen all blocks
zainverting
zfsee :set foldmethod=manual
:set foldenableenable folding
:set foldmethod=syntaxsyntax based folding
:set foldmethod=indentshift based folding
:set foldmethod=manualhighlight the area using v and say zf
:set foldmethod=markertext markers based folding
:set foldmarker=bigin,endset markers for the beginning and the end of the block

Marks

maset local marker a
mBset global marker B
‘cmove to local marker c
‘0return to the position where you were working when Vim was closed last
:marksdisplay markers

Shifts

[#]>shift the highlighted to the right
[#]<shift the highlighted to the left
[#]>>shift the line to the right
[#]<<shift the line to the left
set tabstop=#for tabbing # of spaces
set shiftwidth=#in commandх for the shift # of spaces are used
set [no]expandtabchange tabbing for the corresponding number of spaces

Search commands

/doorsearch for word “door” upside down
?doorsearch for word “door” bottom-up
/jo[ha]n>search for “john” or “joan”
ngo to the next search result item
Ngo to the previous search result item
:10,20s/old/new/gthe same, but for lines from 10th to 20th
/< …. >search for four-symbol words
/fred|joesearch for “fred” or “joe”
/<dddd>search for 4 numbers nearby
/^nsearch for 2 empty lines
:bufdo /searchstr/search in all files opened

If the function ‘incsearch’ is enabled, Vim immediately goes to the first search correspondence when you begin to enter the word for search. If the function ‘hlsearch’ is enabled, all of the correspondences are highlighted. To disable highlighting, enter :nohl.

Replacement

:s/old/new/greplace all combinations of ‘old’ in the current line for ‘new’
:%s/old/new/greplace all appearances of ‘old’ for ‘new’
:%s/old/new/gwreplace all appearances of ‘old’ for ‘new’ with confirmation request
:%s/old/new/gireplace ‘old’ for ‘new’ taking the case into account
:5,30s/old/new/greplace all appearances of ‘old’ for ‘new’ between 5th and 30th lines
:10,$s/old/new/greplace all appearances of ‘old’ for ‘new’ beginning with 10th line and to the end of the file
:%s/^/Welcome/gadd Welcome at the beginning of each line
:%s/$/Ending/gadd Ending at the end of each line
:%s/ *$//gremove all the spaces
:g/door/dremove all the lines containing ‘door’
:v/door/dremove all the lines not containing ‘door’
:s/old/new/replace the first appearance of ‘old’ for ‘new’ in the current line
:%s/r//gremove the carriage return char
:%s#>[^<]+>##gclear all HTML-tags from the text
:%s/^(.*)n$//remove repeating lines
:g/^$/dremove all empty lines
Ctrl+aincrease the number under the cursor by one
Ctrl+xdecrease the number under the cursor by one
ggVGg?modify the text into Rot13

Shell-commands performing

:!perform in the current directory
Cheat

Line numbers

:set numberenable lines numeration
:set nonumberdisable lines numeration

Tab operation

:tabnew [fname]create tab
:tabsshow tabs list
:tabnnext tab
:tabpprevious tab
gtmove to tab #n
gtnext tab
gTprevious tab

Windows operation

:splittile horizontally
:vsplittile vertically
Ctr+Wcclose the window
Ctr+W, hjkl or arrowsswitch between windows
Ctr+W_maximize current window
Ctr+W=adjust windows size
10 ctrl-w+increase current window by 10 lines
:vsplit filesplit the window vertically
:sview filesplit the window and open file in read-only mode
:hideclose current window
:onlyclose all windows except the current
:b 2open #2 in current window

Spelling check

mkdir -p ~/.vim/spell
cd ~/.vim/spell
wget http://ftp.vim.org/vim/runtime/spell/en.ascii.sug
wget http://ftp.vim.org/vim/runtime/spell/en.ascii.spl

setlocal spell spelllang=enenable spelling check
set spell!turn on/off spelling check when working
]snext incorrect word
[sprevious incorrect word

Work with encoding

e ++enc=edit file in ??? encoding
w ++enc=< encoding name >save file in new encoding
set fileencodings=utf-8,koi8-rthe list of automatically determined encodings in descending order of priority

Reading the file

:rinsert filename content after cursor

Exit commands

:wqclose and save file:wqaclose and save all files

:qclose file if there is no need for saving, i.e. if there were no changes made in the file. Otherwise VIM will display an error notification as seen in this picture
:q!close file without saving
:qa! or 😡close all files without saving

Other

:set [no]wildmenuwhen auto-adding in the command line the possible variants will be shown above it
:set listshow tabbing and line breaks
q:Command history
.latest command retry
:colorchoosing colour scheme. Colour schemes: /usr/local/share/vim/vim72/colors/*.vim
:pwdprevious catalogue
:cd [path]move to next catalogue
:!perform command

Help topics

:help – help topics for the editor command.

Vim Cheat Sheet Reddit

Remember, you do not need to know all of the commands: you only need to know where to find them when required. For example, see :help usr_toc to view help topics. You can see :help index to search for the exact topic that is interesting to you. For example run /insert mode to see information about Insert mode.





Coments are closed