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.
Intro
Configuration file :
Vim files configuration are located in two paths :
-
Global one (for all users) inside
/etc
folder :
/etc/vimrc
- And on inside the personal folder :
~/.vimrc
or
/home/USER/.vimrc
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). |
Completion | |
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? |
Examples
- Delete end line blanck spaces :
:%s/\s\+$//g
- Replace the 12 end line next characters (
$
) by</br>
:
:s/$/<\/br>/12
- 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 (akacheck_command check_kav1
).1\ 3\ 5\ \/home\/
will give1 3 5 /home/
.
- Delete empty lines
:g/^$/d
- Delete every IP adress
:g/^\(\d\{,3}\.\d\{,3}\.\d\{,3}\.\d\{,3}\)//
- Replace
<pre
by<ul><pre
or</pre
by</ul></pre>
:s/<\(\/\=\)\(pre\)/<\1ul><\1\2/g
Explanations :
\=
: matches 0 or 1 more of the preceding characters
Others
Buffers
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 :
"ayy
- Then to paste :
"ap
Automatic line break each 72 characters
echo 'set textwidth=72' >> ~.vimrc
spell-checker
- Edit file
.vimrc
and addset 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 :
wget ftp://ftp.vim.org/pub/vim/runtime/spell/fr.latin1.spl
wget ftp://ftp.vim.org/pub/vim/runtime/spell/fr.latin1.sug
wget ftp://ftp.vim.org/pub/vim/runtime/spell/fr.utf-8.spl
wget ftp://ftp.vim.org/pub/vim/runtime/spell/fr.utf-8.sug
- 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