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

  • 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!.
  • Open remote files (using SCP,but you can also use SFTP):
    vim scp://admin@example.com//home/admin/file.csv
    

Moving through file

  • You can use keyboard arrows or k for up, j for down, h for left and l 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 with n or the previous with N.
  • You can go to the first ocurrence with ggn or to the last ocurrence with GN.

Replacing text

  • Replace current line
      :s/<pattern>/<replace>/g
    
  • Replace all lines
      :%s/<pattern>/<replace>/g
    
  • Comment out a range of lines (from 34 to 58)
      :34,58s/^/# /
    

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”.
  • (In insert mode) Ctrl + K + <digraph code>: insert ‘digraphs’ (special characters that are not available on the keyboard). Type :help digraph-table (after pressing Esc to exit from insert or visual mode) to see available digraphs. Example: Ctrl + K + Co to insert the Copyright symbol.

.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

If you have any suggestion, feel free to contact me via social media or email.