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
- Replacing text
- Selecting text
- Deleting text
- Undo, copy, cut and paste
- More commands
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
iand 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
:qand pressing Enter. You can save changes and close the file at once by typing
:wq. If you want to close without saving changes, type
Moving through file
- You can use keyboard arrows or
hfor left and
- You can move the cursor to the top of the file with
- To the end of the file with
- To the beginning of the current line with
- To the end of the current line with
- You can search inside a file typing
/and the word (and pressing Enter). Then, you can move to the next ocurrence with
nor the previous with
- You can go to the first ocurrence with
ggnor to the last ocurrence with
# replace current line :s/<pattern>/<replace>/g # replace all lines :%s/<pattern>/<replace>/g
# e.g. :%s/100/500/g
- To select, type
vto 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.
- Delete a text selection simply by typing
- You can delete the current line by typing
- You can delete a word with
Undo, copy, cut and paste
- To undo, type
- To redo: Ctrl + r.
- To copy a selection, type
- To copy the current line, type
- To cut a selection, type
- To paste, type
- 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
Ato go to the end of the line and enter “insert mode”.
oto create a new line below current line and enter “insert mode”.
cwto delete current word and enter “insert mode”.
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