Vi Editor Commands Cheat Sheet



  1. Linux Vi Editor Commands Pdf
  2. Vi Editor Commands Pdf

Click here to download VI cheat sheet ACNS bulletin Vi Text editor Mode The Vi has two Mode one is command mode where the user can only move the cursor to select the text to perform deletion and pasting jobs, the second one insertion which enables when you press the INSERT button from the keyboard to perform insert or change command. Marking text (visual mode) v - starts visual mode From here you can move around as in normal mode (h, j, k, l etc.) and can then do a command (such as y, d, or c) V - starts linewise visual mode. Linux, VI Editor, VIM Editor. VI cheat sheet, VI command, VI Editor, vi replace, vi search, vi search and replace, vi split, vi switch window, vi tab, VIM command, VIM Editor. Sublime Text Editor – Shortcut Cheat Sheet. How to copy files from one S3 bucket to another S3 bucket. The ultimate Vi cheat sheet for anyone learning Vi Commands or the Vi editor in general. Keep this guide close by when using the editor and you will learn how to use Vi in no time. Vim commands will be the same as Vi for the most part – Windows however has Gvim which allows some GUI interaction.

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.

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

Editor
  • 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

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
Linux vi commands cheat sheet

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

: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

Vi Editor Commands Cheat SheetCommands
/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

Editor
: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

Line numbers

:set numberenable lines numeration
:set nonumberdisable lines numeration

Tab operation

Linux Vi Editor Commands Pdf

: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

Vi Editor Commands Pdf

: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.

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.

Some times I have to work on Server(Linux hosted server – Ubuntu), due to hands on experience on UI based editors (IDE, Sublime, Atom, Notepad ++, etc ) I felt difficult to edit any documents(usually programming) via terminal at initial stage. But VI is a powerful tool as any other editor, can perform almost all operation on File edit manipulation.

VI has 2 modes. Insert Mode and Command Mode.

  1. Insert mode: Its to used edit/update content of a file
  2. Command mode: Its help us to perform VI function and it has wide range of functions. Its a default mode of VI.

To switch to ‘Insert mode’ hit letter ‘i‘ or ‘insert‘ button.
To switch to ‘Command mode’ hit ‘Esc‘ button.

Basic Commands:-

:qClose file
:wWrite to File
:wqWrite to file and then close editer.
:12Go to specific line number of file. Number reference line number
GGo to last line of the file

Search & Replace Commands:-

/testSearch word ‘test’ on document. Hit letter ‘n’ to repeat search and ‘N’ for backward search
/test/wordIf search term contains backward slash, escape with forward slash. Search term on above example is “test/word”
?testSearch word ‘test’ on document from bottom to top.
/test|wordSearch more than search term. “test” and “word” both are search criteria.
/test*
OR
/<test
Search word starts with ‘test’ on document, Like test, testing, tested, testimonial, etc.
/test>Search word ends with ‘test’ on document.
:%s/find/replace/gFind and replace content.
:%s/find/replace/giFind and replace content and its not case sensitive.
:%s/find/replace/gwFind and replace content on confirmation.
:10,20s/find/replace/gFind and replace content between lines 10 and 20.
:s/find/replace/Find and replace content at first occurrence on current line.
:s/find/replace/gFind and replace content on current line.

Delete Commands:-

DDelete from cursor position to end of current line. D is equivalent d$
dwDelete from the current cursor position to the beginning of the next word
ddDelete current line
5ddRepeat dd command (delete current line) 5 times
dGDelete lines from current to end of file
d10jDelete from current line to 10 lines forward direction
d10kDelete from current line to 10 lines backward direction
:10,20dDelete lines between range.
:g/test/dDelete all lines containing matching pattern.
:v/test/dDelete all lines that does not containing matching pattern. Reverse of :g command
:v/test|testing/dDelete all lines except those that containing ‘test’ or ‘testing’.

Tab, Split & Switch:

:tabnew fileCreate new tab and open file
g tSwitch to next tab
g TSwitch to previous tab
002 g tSwitch to specific tab by tab number
:split fileSplit window/screen on horizontal
Ctrl + W Ctrl + WSwitch between window
:qaClose all tab or window
:e test.shOpen file or switch from current file
:e# 1Switch file, here number is file number

I have not listed all VI Commands, but these commands can help to modify document via terminal.