- Linux Vi Editor Commands Pdf
- 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:
- 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, l | moving through the text in different directions |
|, 0, home | move the cursor to the beginning of the line |
^ | move to the first non-space symbol in the line |
$, end | ove to the end of the line |
m | move one-half of the screen width |
g | move to the lower line |
e | move to the end of the current word |
E | move to the end of the current word ignoring punctuation marks |
– | move to the first non-space symbol in the line above |
+, Enter | move to the first non-space symbol in the line below |
G | move to the last line |
G | move to line |
H | move to the first screen line |
M | move to the middle screen line |
L | move to the last screen line |
w | move to the beginning of the next word |
b | move cursor to the beginning of the current word |
B | move to the beginning of the word ignoring punctuation marks |
ge | move to the end of the next word |
gE | move 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) |
3w | move to beginning of the fourth word counting from the current position |
4e | move to the end of the fourth word counting from the current position |
: | move to line number |
% | move to the next pair of brackets |
:ju | shift list |
Ctrl+e | scroll one line down |
Ctrl+d | scroll 1/2 of the page down |
Ctrl+f | scroll 1 page down |
Ctrl+y | scroll 1 line up |
Ctrl+u | scroll 1/2 of the page up |
Ctrl+b | scroll 1 page up |
Deleting commands
Del, x | delete the symbol under the cursor |
X | delete the symbol before cursor |
x | delete of symbols, beginning with the one under the cursor |
C | delete all from cursor to the end of the line and switch to Insert mode |
D | delete all from the current symbol to the end of the line |
d | delete n+1 lines |
DEL | delete the next symbol |
cc | change the whole line |
c$ | change the line beginning from the cursor position to the end |
d | delete 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 + hjkl | text highlighting |
SHIFT + v | line highlighting |
vip | paragraph highlighting |
viw | word 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 |
v2j | highlight all up to two lines down |
vt, | highlight all up to the nearest coma |
:syntax on | enable highlighting syntax |
:syntax off | disable highlighting syntax |
Cut / paste commands
P | paste before the current line |
p | paste after the cursor |
[p | paste before the cursor |
i | paste text before the cursor |
I | paste text in the beginning of the line |
CTR+p or CTR+n | automatic text insert (in the editing mode) |
CTR+r,=, | paste expression, for wxample 5*2 – 3 (in the editing mode) |
a | add text after the cursor |
A | add in the end of the line |
o | add line right after the current one |
O | add line after the current one |
dip | cut paragraph |
di( | cut the content of round brackets |
da( | cut the content of round brackets and the brackets themselves |
dd | cut 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
yy | copies the line. Also you may use a number modifier |
y | copy n+1 line |
y2y | copy two lines |
yiw | copy the word |
y | copy 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 |
J | unification of the current line with the next one |
R | replace symbols beginning with the current position |
xp | exchange positions of two symbols |
Vim cases
Vu | shift the line to the lowercase |
VU | shift the line to the uppercase |
g~~ | invert case |
vEU | shift the case of the word under the cursor to the uppercase |
vE~ | invert the case of the word |
ggguG | shift all the text into the lower case |
:setignorecase | case-independent search |
:set smartcase | ignore register while searching if there are no uppercase symbols in the target expression |
:%s/<./u&/g | shift the first letter of each word into the uppercase |
:%s/<./l&/g | shift 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
U | discard 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 wrap | enable word wrap (default) |
:set nowrap | disable word wrap |
Printing
:ha[rdcopy] | print document |
:set printoptions=duplex:off | disable duplex printing |
Folding commands
zc | fold block |
zo | unfold block |
zM | close all blocks |
zR | open all blocks |
za | inverting |
zf | see :set foldmethod=manual |
:set foldenable | enable folding |
:set foldmethod=syntax | syntax based folding |
:set foldmethod=indent | shift based folding |
:set foldmethod=manual | highlight the area using v and say zf |
:set foldmethod=marker | text markers based folding |
:set foldmarker=bigin,end | set markers for the beginning and the end of the block |
Marks
ma | set local marker a |
mB | set global marker B |
‘c | move to local marker c |
‘0 | return to the position where you were working when Vim was closed last |
:marks | display 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]expandtab | change tabbing for the corresponding number of spaces |
Search commands
/door | search for word “door” upside down |
?door | search for word “door” bottom-up |
/jo[ha]n> | search for “john” or “joan” |
n | go to the next search result item |
N | go to the previous search result item |
:10,20s/old/new/g | the same, but for lines from 10th to 20th |
/< …. > | search for four-symbol words |
/fred|joe | search for “fred” or “joe” |
/<dddd> | search for 4 numbers nearby |
/^n | search 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/g | replace all combinations of ‘old’ in the current line for ‘new’ |
:%s/old/new/g | replace all appearances of ‘old’ for ‘new’ |
:%s/old/new/gw | replace all appearances of ‘old’ for ‘new’ with confirmation request |
:%s/old/new/gi | replace ‘old’ for ‘new’ taking the case into account |
:5,30s/old/new/g | replace all appearances of ‘old’ for ‘new’ between 5th and 30th lines |
:10,$s/old/new/g | replace all appearances of ‘old’ for ‘new’ beginning with 10th line and to the end of the file |
:%s/^/Welcome/g | add Welcome at the beginning of each line |
:%s/$/Ending/g | add Ending at the end of each line |
:%s/ *$//g | remove all the spaces |
:g/door/d | remove all the lines containing ‘door’ |
:v/door/d | remove all the lines not containing ‘door’ |
:s/old/new/ | replace the first appearance of ‘old’ for ‘new’ in the current line |
:%s/r//g | remove the carriage return char |
:%s#>[^<]+>##g | clear all HTML-tags from the text |
:%s/^(.*)n$// | remove repeating lines |
:g/^$/d | remove all empty lines |
Ctrl+a | increase the number under the cursor by one |
Ctrl+x | decrease the number under the cursor by one |
ggVGg? | modify the text into Rot13 |
Shell-commands performing
:! | perform in the current directory |
Line numbers
:set number | enable lines numeration |
:set nonumber | disable lines numeration |
Tab operation
Linux Vi Editor Commands Pdf
:tabnew [fname] | create tab |
:tabs | show tabs list |
:tabn | next tab |
:tabp | previous tab |
gt | move to tab #n |
gt | next tab |
gT | previous tab |
Windows operation
:split | tile horizontally |
:vsplit | tile vertically |
Ctr+Wc | close the window |
Ctr+W, hjkl or arrows | switch between windows |
Ctr+W_ | maximize current window |
Ctr+W= | adjust windows size |
10 ctrl-w+ | increase current window by 10 lines |
:vsplit file | split the window vertically |
:sview file | split the window and open file in read-only mode |
:hide | close current window |
:only | close all windows except the current |
:b 2 | open #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=en | enable spelling check |
set spell! | turn on/off spelling check when working |
]s | next incorrect word |
[s | previous 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-r | the list of automatically determined encodings in descending order of priority |
Reading the file
:r | insert filename content after cursor |
Exit commands
Vi Editor Commands Pdf
:wqclose and save file:wqaclose and save all files
:q | close 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]wildmenu | when auto-adding in the command line the possible variants will be shown above it |
:set list | show tabbing and line breaks |
q: | Command history |
. | latest command retry |
:color | choosing colour scheme. Colour schemes: /usr/local/share/vim/vim72/colors/*.vim |
:pwd | previous 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.
- Insert mode: Its to used edit/update content of a file
- 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:-
:q | Close file |
:w | Write to File |
:wq | Write to file and then close editer. |
:12 | Go to specific line number of file. Number reference line number |
G | Go to last line of the file |
Search & Replace Commands:-
/test | Search word ‘test’ on document. Hit letter ‘n’ to repeat search and ‘N’ for backward search |
/test/word | If search term contains backward slash, escape with forward slash. Search term on above example is “test/word” |
?test | Search word ‘test’ on document from bottom to top. |
/test|word | Search 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/g | Find and replace content. |
:%s/find/replace/gi | Find and replace content and its not case sensitive. |
:%s/find/replace/gw | Find and replace content on confirmation. |
:10,20s/find/replace/g | Find and replace content between lines 10 and 20. |
:s/find/replace/ | Find and replace content at first occurrence on current line. |
:s/find/replace/g | Find and replace content on current line. |
Delete Commands:-
D | Delete from cursor position to end of current line. D is equivalent d$ |
dw | Delete from the current cursor position to the beginning of the next word |
dd | Delete current line |
5dd | Repeat dd command (delete current line) 5 times |
dG | Delete lines from current to end of file |
d10j | Delete from current line to 10 lines forward direction |
d10k | Delete from current line to 10 lines backward direction |
:10,20d | Delete lines between range. |
:g/test/d | Delete all lines containing matching pattern. |
:v/test/d | Delete all lines that does not containing matching pattern. Reverse of :g command |
:v/test|testing/d | Delete all lines except those that containing ‘test’ or ‘testing’. |
Tab, Split & Switch:
:tabnew file | Create new tab and open file |
g t | Switch to next tab |
g T | Switch to previous tab |
002 g t | Switch to specific tab by tab number |
:split file | Split window/screen on horizontal |
Ctrl + W Ctrl + W | Switch between window |
:qa | Close all tab or window |
:e test.sh | Open file or switch from current file |
:e# 1 | Switch file, here number is file number |
I have not listed all VI Commands, but these commands can help to modify document via terminal.