Principle
To launch the editor, simply type vim in a terminal. Unlike regular text editors, vim has 4 operating modes: interactive mode, insertion mode, visual mode, and command mode:
- interactive mode is the default mode. It is the mode you are in when you launch
vimor when you are in another mode and press theESCkey. In this mode, you can navigate through a file, copy-paste, delete text, replace text… - visual mode allows you to easily select words, lines, blocks of text and perform operations on them. This mode is activated from interactive mode by pressing
v. - insertion mode is the mode that allows you to write text. From interactive mode, simply press the
ikey to enter insertion mode. - command mode is the mode that allows executing
vimcommands or system commands. From interactive mode, press the:key then type the command you want to execute.
First Manipulations
Difficulty: Easy- Launch
vimin a terminal - Press
ito enter insertion mode - Type a minimal
Cprogram - To save, press
ESC(to exit insertion mode) then type:w test.cto save what you have written (:allows entering command mode,wis the save/write command andtest.cis the filename) - Still in interactive mode, navigate through the file with the keys
h(left),j(down),k(up),l(right). You can also use the arrow keys. - To quit
vim, simply type:qor:q!if you want to force closing (by defaultvimdoes not quit if there are unsaved changes).
It is possible to save and quit with the command
:wq
Navigation
Besides the keys h, j, k, l, others are useful such as:
0which automatically goes to the beginning of the current line$which automatically goes to the end of the current lineggwhich goes to the first lineGwhich goes to the last linexGwherexis a number goes to linex
Cut / Delete
- To delete a character, type
xoryxwhereyis the number of characters to delete - To delete a line, type
ddorxddwherexis the number of lines to delete - To delete a word, type
dw(delete word) orxdwwherexis the number of words to delete - To delete from the beginning of the line to the cursor, type
d0 - To delete from the cursor to the end of the line, type
d$
Copy
- Same principle as for cutting, except the command is
yyto copy a line,ywto copy a word,y0to copy…
Paste
- To paste text, type
porxpwherexis the number of times to paste. Note that pasting happens immediately after where the cursor is; there is no line break.
Replace
- To replace a single letter, place the cursor on the character to replace and type
rswheresis the new letter to insert. - To replace text, it is possible to use the syntax
:s/old/newwhich replaces the first occurrence ofoldwithnew; variants exist::s/old/new/g: replaces all occurrences on the line where the cursor is:#,#s/old/new/g: replaces all occurrences on lines # to # of the file:%s/old/new/g: replaces all occurrences throughout the entire file
Undoing Changes
- To undo a change, type
uorxuwherexis the number of times to go back
Searching for a Word
- To search for a word after where the cursor is, type
/word. To go to the next occurrence, typenorNfor the previous occurrence - To search for a word before where the cursor is, type
?word, the rest is the same
Launching an External Command
- Simply type the command name preceded by
:!such as:!lsto display the content of the current directory
Opening Another File
- In
vimsimply type:e filenameto open the file namedfilename. You can then navigate between files with:e #xwherexdesignates the file number.
The Configuration File
Difficulty: RxThere are many options in vim that make it more pleasant to use (syntax highlighting, line numbers, aliases…). Here are a few that you can write in a .vimrc file that you will place at the root of your home directory (this saves you from having to enable the options every time you open vim).
# Example .vimrc configuration
# You can customize vim with these options
set nocpdisables compatibility modesyn onenables syntax highlightingset syntax =onenables syntax highlightingfiletype indent plugin onindentation based on file typeset nudisplays line numbersset showmatchhighlights missing braces/parenthesesset tabstop =4tab size in spacesset shiftwidth =4tab size in spacesset softtabstop =4tab size in spacesset expandtabconverts tabs to spacesset cursorlinehighlights the line where the cursor isiab #i #includeexample alias: simply type#ithen tab to have it replaced by#include