Mastering Vim: Complete Guide for Linux Users

Introduction to Vim

Vim, short for Vi Improved, is one of the powerful text editors in the Linux ecosystem. Although its interface may seem crude at first, its modal operation allows speed and precision that few editors can match. Learning Vim not only improves productivity when programming, but also deepens understanding of how files and the terminal interact. This post offers a guide from installation to advanced tricks, so any Linux user can master Vim and get the most out of it.

Installation on Linux

In Linux distributions, Vim comes preinstalled or is available in the repositories. On Ubuntu and Debian, simply run sudo apt update && sudo apt install vim. On Fedora use sudo dnf install vim, while on Arch Linux the command is sudo pacman -S vim. If you want the latest version with features, you can compile from the source code on GitHub, following the README instructions. After installation, checking the version with vim –version confirms that the executable is ready to use.

Basic Modes

Vim operates in modes, each designed for a specific task. Normal mode allows navigation and executing commands. Insert mode is activated with i and lets you type text as in a regular editor. Visual mode, accessible with v, makes it easy to select blocks of code or text for operations like copy, cut, or change. Other modes include Command-line mode (initiated with 🙂 to execute commands such as saving or searching, and Replace mode (R) which overwrites existing characters. Mastering the transition between these modes is essential for using Vim efficiently.

Navigation and Movement

In Normal mode, the h, j, k, and l keys move the cursor left, down, up, and right, offering an alternative to the arrow keys. The w and b commands jump to the start of the next or previous word, while e moves to the end of the current word. To move by lines, 0 goes to the beginning of the line and $ to the end. The Ctrl+f and Ctrl+b commands scroll pages forward and backward, and gg and G go to the beginning and end of the file. These combinations allow moving through amounts of text without lifting your hands from the keyboard.

Basic Editing

In Insert mode you can type normally, but Vim offers editing commands from Normal mode. x deletes the character under the cursor, while X deletes the character before it. dd cuts the entire line and p pastes it after the current position; P pastes before. The cw command changes the word from the cursor position to its end, entering Insert mode. cc changes the whole line and S substitutes the entire line and enters Insert mode. These commands can be combined with counts, such as 3dd to delete three lines, increasing editing speed.

Search and Replace

Search is activated with / followed by the pattern and Enter to search forward, or ? to search backward. n repeats the search in the same direction and N in the opposite direction. To substitute text, the command :s/old/new/g replaces all occurrences on the current line; adding % before s applies the substitution to the entire file. Flags such as c to confirm replacement or i to ignore case can be added. Regular expressions are also supported, allowing advanced searches like :s/\bTODO\b/g to change whole words.

Customization with .vimrc

The .vimrc file located in the home directory contains Vim’s configuration. Here you can set options such as set number to show line numbers, set relativenumber for relative numbering, and set tabstop=4 shiftwidth=4 expandtab for indentation. You can also assign mappings, for example nnoremap :w to save with Ctrl+S. Syntax highlighting is enabled with syntax on, and color schemes can be defined with colorscheme desert or colorscheme gruvbox. Saving changes in .vimrc and reloading with :source % applies the settings.

Popular Plugins

Although Vim is powerful, its functionality is expanded with plugin managers. Vundle and vim-plug are two of the commonly used ones. With vim-plug, you add a call to plug#begin(‘~/.vim/plugged’) in .vimrc, followed by lines such as Plug ‘preservim/nerdtree’ for the file explorer or Plug ‘junegunn/fzf.vim’ for fuzzy finding. After saving, you run :PlugInstall in Vim to download and install the plugins. Other notable plugins include YouCompleteMe for advanced completion, alephja/vim-surround for