Vim basics
Vim is one of the most popular text editors in Linux. If you haven’t used yet, these are the basic commands to start.
Table of Contents
- Opening a file with Vim, inserting text, saving and close
- Moving through file
- Searching
- Replacing text
- Selecting text
- Deleting text
- Undo, copy, cut and paste
- Windows
- More commands
- .vimrc
Opening a file with Vim, inserting text, saving and close
- Open a file or directory (from a terminal).
vim text.txt vim .
- Open a file or directory (inside Vim):
:e text.txt :e .
- Inside Vim, type
i
and press Enter to enter “insert mode”. Then you can edit the file. - To save changes, first exit “insert mode” by pressing Escape. Then, type
:w
(and press Enter) to save changes. - Close Vim typing
:q
and pressing Enter. You can save changes and close the file at once by typing:wq
. If you want to close without saving changes, type:q!
.
Moving through file
- You can use keyboard arrows or
k
for up,j
for down,h
for left andl
for right. - You can move the cursor to the top of the file with
gg
. - To the end of the file with
G
. - To the beginning of the current line with
0
. - To the end of the current line with
$
.
Searching
- You can search inside a file typing
/
and the word (and pressing Enter). Then, you can move to the next ocurrence withn
or the previous withN
. - You can go to the first ocurrence with
ggn
or to the last ocurrence withGN
.
Replacing text
# replace current line
:s/<pattern>/<replace>/g
# replace all lines
:%s/<pattern>/<replace>/g
# e.g.
:%s/100/500/g
Selecting text
- To select, type
v
to enter “visual mode” (first press Escape to exit “insert mode” if you were in it) and use arrows or k,j,h,l to select the text.
Deleting text
- Delete a text selection simply by typing
d
. - You can delete the current line by typing
dd
. - You can delete a word with
dw
.
Undo, copy, cut and paste
- To undo, type
u
. - To redo: Ctrl + r.
- To copy a selection, type
y
. - To copy the current line, type
yy
. - To cut a selection, type
x
. - To paste, type
p
.
Windows
- Split window horizontally: Ctrl + ws
- Split window vertically: Ctrl + wv
- Change to the right window: Ctrl + wl
- Change to left window: Ctrl + wh
- Change to the window above: Ctrl + wk
- Change to the window below: Ctrl + wj
More commands
- Type
A
to go to the end of the line and enter “insert mode”. - Type
o
to create a new line below current line and enter “insert mode”. - Type
cw
to delete current word and enter “insert mode”.
.vimrc
It’s a configuration file located in $HOME
. If you create this file, default settings will be disabled. These are some examples of the options you can set in Vim (you can write a comment by starting the line with a "
).
" Enable autoindentation
filetype indent on
" Use spaces instead of tabs for indentation
set expandtab
" Set number of spaces (2) for indentation
set shiftwidth=2
" Set the size of a tab (2 spaces)
set tabstop=2
" Enable syntax highlighting
syntax enable
" Set encoding (utf-8)
set encoding=utf-8
" Ignore case when searching
set ignorecase
" Show first result while typing the search term
set incsearch
" Highlight all results when searching
set hlsearch
" Show line numbers
set number
Featured content: