rss logo

Vi and Vim text editors notes and examples

Vi and Vim are two powerful text editors which are very useful if we take time to understand how they work and what they are capable of.


Configuration file :

Vim files configuration are located in two paths :

  • Global one (for all users) inside /etc folder :
  • And on inside the personal folder :



We can for example put syntax on, in order to activate the color syntax.

Open a file

vim file

Open two, three or four files version and show the differences between them

vimdiff file1 file2 … 

Open files one above the other

vim -o file1 fichier2

Open files side by side

vim -O file1 fichier2

Open a file and go directly to line "x"

vim +"x" file1

Some Commands

Commands to move the cursor or the display
h Move left.
j Move down.
k Move up.
l Move right.
b Move "Back" from a word to the left.
B "Back" same as b but will only stop to a space.
v "Visual" mode.
V "Visual" mode, select by line.
Ctrl+v "Visual" bloc mode.
gv Select the last "visual".
w Move "Word" from a word to the right.
W "Word" same as w but will only stop to a space.
G Move the cursor to the last line.
Ctrl+f Move the display down.
Ctrl+b Move the display up.
$ Move to the end of the line.
0 Move to the beginning of the line.
^ Move to the first character of the line.
{ Move the cursor to the next paragraph.
} Move the cursor to the paragraph before.
Edit mode
dd Cut the current line.
dw Cut "Word".
dl Cut "Letter".
x Cut a character.
X Cut a character to the left.
yy Copy the current line..
yl Copy "Letter".
yw Copy "Word".
D Cut from the current position to the end of the line.
p Paste.
P Paste before the current line.
u Cancel last operation.
Ctrl+r Redo a command previously canceled.
ZZ Save and quit.
Text Mode
. Repeat the last action.
a "Add" insertion mode on character after the cursor.
A "Add" insertion mode at the end of the current line.
i "Insert" insertion mode.
I "Insert" insertion mode at the first non blank character of the current line.
J "Join", join two lines.
o insertion mode one line under the cursor.
O Switch to insertion mode one line under the cursor.
r "Replace" current character by the next typed character.
R "Replace" mode Replace, each new character by a new one. Press Esc to go back to command mode.
cl "Cut Letter" cut a character.
ct+ Cut until + character.
cw "Cut Word" cut from the cursor to the next space, comma or full stop.
cW "Cut Word" cut from the cursor to the next space.
C "Cut" from the cursor to the end of line and go to Insertion mode.
xp Swap two characters.
<ESC> Quit TEXT-INPUT. mode
ctrl+ ou ctrl+a Increment a number.
ctrl- ou ctrl+x Decrement a number.
ctrl+R "Redo" play the last cancelled command.
~ Modify the current character case.
guu or vu Swap current line to lower case.
gUU ou vU Swap current line to upper case.
veu Swap current word to lower case.
vEU Swap current word to upper case.
gqap ou vapgq Justify current paragraph.
Command Mode
:w "Write" Save the file.
:w file "Write" Save the file with a different name inside current folder.
:q "Quit" if file hasn't been altered.
:q! "Quit" even if file altered.
:wq "Write and Quit"
:x Save and quit if edited.
:n Go to "n" line.
:!foo Execute the foo external command.
:chdir or :!chdir Change current directory.
:open or :e file Edit file "file".
:e ++enc=latin1 Re-open a file with a different encoding.
:e ++ff=dos Re-open a file with a different encoding.
:set fileformat? Print the encoding format.
/word Search the word "word" from the top to the bottom. Type n to go to the next word and N to the previous.
?word Search the word "word" from the bottom to the top. Type n to go to the next word and N to the previous.
:$ Move the cursor to the last line.
:syntax on Enable syntax, put some color on the text.
:set nu(mber) Enable the line number print.
:set nonu Disable the line number print.
:set list Print the hidden character (set nolist to disable).
Ctrl+n Word completion (repeat to go down).
Ctrl+p Word completion (repeat to go up).
Ctrl+r then =x+n Insert arithmetic result x+n.
Ctrl+r then % Insert the name and current path of file.
Ctrl+r then / Insert current search buffer.
Multi Windows Mode
Ctrl+w Action
w Change current windows.
n Create a new windows.
+ or - Extend/Reduce current windows.
x Switch two windows.
q Close current windows.
h or j or k or l Move the cursor to a different windows.
:sp Separate a same file in two windows (Action+q to go back to normal mode).
Easter Eggs
:help! No panic!
:help holy-grail You found it, Arthur!
:help 42 Refer to H2G2.
:help UserGettingBored When the user hits CTRL-C. Just kidding! :-)
:Ni! Do you demand a shrubbery?


  • Delete end line blanck spaces :
  • Replace the 12 end line next characters ($) by </br> :
  • Replace check_command check_kav1!serv01,4,5,6 by check_command check_kav1!serv01 1 3 5 /home/ from the line 10 to 50 and only if check_command is present :
:10,50g/check_command/s/\(.*\!\w*\).*/\1 1\ 3\ 5\ \/home\/

Explanations :

  • 10,50 : work from line 10 to 50
  • g/check_command : work only with lines containing check_command.
  • s/\(.*\!\w*\).*/ : Part to replace. .*\! will match check_command check_kav1, \w* is equal to [a-zA-Z0-9] repeated once or more and will match serv01 each elements inside the bracket (aka check_command check_kav1) will be recorded inside the buffer \1.
  • /\1 1\ 3\ 5\ \/home\/ : Part replaced. \1 buffer n°1 (aka check_command check_kav1). 1\ 3\ 5\ \/home\/ will give 1 3 5 /home/.
  • Delete empty lines
  • Delete every ip adress
  • Replace <pre by <ul><pre or </pre by </ul></pre>

Explanations :

  • \= : matches 0 or 1 more of the preceding characters



It's possible to put some text inside buffers which allow to keep inside memory some elements that we can use later. To do so, we will use following combination "+letter

  • Example, to copy inside a buffer current line :
  • Then to paste :

Automatic line break each 72 characters

echo 'set textwidth=72' >> ~.vimrc


  • Edit file .vimrc and add set spelllang=fr line
echo 'set spelllang=fr' >> ~/.vimrc
  • Create spell folder inside ~/.vim or /usr/share/vim/vimcurrent/ for all system users, and we copy this files :
  • We can enable the spell-checker :
:set spell

We will use z= on every red words to access to a correction list.

Unicode characters

From insertion mode we can generate unicode characters, example here with horizontal ellipsis «»

  • Enter in insert mode : i
  • Press Ctrl+u to enter in unicode mode
  • Type 2026 and press enter


Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address