VIM means VI IMproved. VIM is a text editor for most UNIX systems and Apple OS X.
VIM has four modes:
The command mode allows to execute VIM commands. The commands are typed at the bottom of the editor and preceeded by :
.
- Examples of VIM commands:
:set number
Activate line numbers in the text editor (By default vim does not display the line numbers):w
Write the changes to the file:q
Quit the editor:q!
Quit the editor and discard all the changes made since the moment the file was opened:wq
Write changes to the file and quit the editor
Changes made in the editor using the set command are not persistent. That means they will disappear when the editor is closed.
To make changes persistent we can edit the vimrc
file.
Open the file using the command vim ~/.vimrc
and the paste the configuration below.
set number
syntax on
set tabstop=4
set shiftwidth=4
set autoindent
set mouse=a
colorscheme slate
The normal is the default mode. The normal mode allows navigating through the text (text means anything displayed in the editor including source code) and execute operations on the text.
To navigate through the text, instead of using arrowkeys, we can also navigate through the text using the keyboard letters:
-
h Move the cursor to the left
-
j Move the cursor down
-
k Move the cursor up
-
l Move the cursor to the right
-
Keybinding
- u Undo changes
- ctrl + r Redo changes
- cc Removes the content of the line and switch to the insert mode
- D Remove from the current line everything after the cursor
- dd Deletes the entire line
- w or W - Jump to the next word in the text
- b Jump to the previous word (backwords)
- 0 Sends the cursor to the begining of the line
- $ Sends the cursor to the end of the line
- d0 Delete from the cursor's current position to the beginning of the line
- d$ Delete from the cursor's current position to the end of the line
- ci" Find the nearest quotation mark in the cursor's current line, remove the content inside quote ", switch to insert mode and place the cursor between the empty quote (very useful when we intend to remove a text and replace for a new one)
- ci( Find the nearest openning paranthesis in the cursor's current line, remove the content, switch insert to insert mode and to place the cursor inside the paranthesis (very useful to remove the arguments of function and start writing new ones)
- numberG Go to Line
number
- dw Delete the next word (next word means the word after the word of cursor's current position)
- db Delete the previous word
- diw Delete in the middle of the word (delete the current work when the cursor is in the middle of it)
- % Takes the cursor to the pairing closing parenthesis, curly brackets etc.
- == Indent a line
- gg Go to the begin of the file
- G Go to the end of the file
- gg=G Indent the entire file
We can combine numbers and keybinding, that multiplies the action ex: 5k takes 5 lines up, 5h goes 5 characters right, 2u undo 2 times
In the insert mode, whatever is written has no functionality and is just a text!. That means that any keyboard that we press when in the insert mode, is written in the file. There are many ways to enter in the insert mode:
- i Start the insert mode where the cursor is
- I Start in the insert mode placing the cursor at the begining of the line
- a Start the insert mode, one character after the cursor current position(append)
- A Start the insert mode placing the cursor at the end of the line
- o Start the insert mode opening a newline below the current line (open new line)
- O Enter in the insert mode placing the cursor above the current line
To leave the insert mode to the normal mode press esc.
The visual mode is for text selection. There are three visual modes, but here I mention only two:
-
v Start visual mode, and allows to select character by character
-
V Visual mode that allows to select line by line
-
Keybinding
- d Deletes the selected (highlighted) text (You always can undo the deletion using
u
when swtching to normal mode \ - y Copy a text to the clipboard
- yy or Y Copy the entire line
- p - paste a text to the editor below the current line
- P - Paste the text to the editor above the current line
- c - remove the highlighted text and switchs to insert mode
- r - replace a single character
- d Deletes the selected (highlighted) text (You always can undo the deletion using
/myword
- search formyword
inside the next- n Search for the next occurence
- N Search for the previous occurrence
:%s/character/symbol/g
Replace the word character by symbol (global). To remove only a particular thing just select the text.
- ma - Mark this line a point to return if we want to go back just 'a. The character a is arbitrary, one can specify any character from the alphabet, or have multiples waypoints.
Buffers allow to have multiples files open in VIM in the command mode.
:r [file_path]
Read the contents of a file in the current file.:e [file_path]
Open a new buffer:bn
Go to the next buffer:bp
Go to the previous buffer:bd
Delete the current buffer:enew
A create a new empty buffer:vsplit [filename]
Slit the screen vertically- ctrl + ww Switch between screens