Vim remains a cornerstone of text editing in the Unix world, favored for its modal editing efficiency and ubiquity in server environments. While modern graphical IDEs dominate local development, knowledge of Vim is essential for working in embedded Linux environments, headless servers, or scenarios where SSH access is restricted or impossible. This guide provides a systematic approach to transforming a standard Ubuntu installation into a powerful Vim-based development workstation.
- Installation and Environment Preparation
By default, Ubuntu ships with a minimal version of vi (often vim-tiny) which lacks features like arrow key navigation in insert mode. To utilize modern capabilities, we first purge legacy packages and install the full Vim suite, including the graphical interface variant.
# Remove legacy vim packages
sudo apt remove vim-tiny vim-common -y
# Install the full-featured Vim and GTK3 GUI support
sudo apt update
sudo apt install vim vim-gtk3 vim-doc -y
- Essential .vimrc Configuration
Vim behavior is controlled via the .vimrc configuration file located in the user's home directory (~/.vimrc). The following configuration establishes a robust baseline for coding, including syntax highlighting, indentation rules, and UI enhancements.
" Enable syntax processing
syntax on
" Display line numbers
set number
" Configure indentation behavior
set tabstop=4 " Number of spaces a tab counts for
set shiftwidth=4 " Number of spaces for auto-indent
set expandtab " Convert tabs to spaces
set autoindent " Maintain indent level on new lines
set smartindent " Smart indentation for C-like languages
" UI Enhancements
set mouse=a " Enable mouse support in all modes
set cursorline " Highlight the current line
set showcmd " Show command in status bar
set wildmenu " Enhanced command completion
set laststatus=2 " Always show status line
set ruler " Show cursor position
" Search settings
set incsearch " Search as characters are entered
set hlsearch " Highlight matches
set ignorecase " Case insensitive search
set smartcase " Override ignorecase if uppercase present
Automatic Pair Completion
Unlike modern IDEs, Vim does not natively auto-close brackets, quotes, or parentheses. We can implement this using insert-mode key mappings (inoremap). The following snippets automatically insert closing pairs and place the cursor inside them.
" Auto-close brackets, quotes, and parentheses
inoremap { {<CR>}<ESC>O
inoremap ( ()<ESC>i
inoremap [ []<ESC>i
inoremap " ""<ESC>i
inoremap ' ''<ESC>i
- Plugin Management with vim-plug
To extend Vim's functionality, we use vim-plug, a minimalist, fast plugin manager. It allows for parallel installation and easy updates.
Installation
Download the plug.vim script to the autoload directory. Ensure curl is installed first.
sudo apt install curl -y
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
Configuration Structure
Add the plugin block to your .vimrc. Plugins listed between plug#begin and plug#end will be managed by vim-plug.
" Initialize plugin manager
call plug#begin('~/.vim/plugged')
" Declare plugins here (examples below)
call plug#end()
- Essential Plugins for Development
File Navigation: NERDTree
NERDTree provides a file system explorer, allowing project navigation similar to a sidebar in GUI editors.
Configuration:
Plug 'preservim/nerdtree'
" Mappings
nnoremap <F3> :NERDTreeToggle<CR>
nnoremap <F2> :NERDTreeMirror<CR>
Key Commands (within NERDTree window):
oor<Enter>: Open file/folder in previous window.t: Open file in new tab.i: Open file in horizontal split.s: Open file in vertical split.q: Close the NERDTree window.
Code Outline: Tagbar
Tagbar generates a ctags-based outline of functions, classes, and variables. This requires exuberant-ctags to be installed on the system.
sudo apt install exuberant-ctags -y
To enable standard library indexing, generate tags for system headers:
cd /usr/include
sudo ctags -R --fields=+l .
Configuration:
Plug 'preservim/tagbar'
" Settings
let g:tagbar_width = 30
let g:tagbar_ctags_bin = '/usr/bin/ctags'
nnoremap <F4> :TagbarToggle<CR>
" Auto-open for C/C++ files
autocmd BufReadPost *.cpp,*.c,*.h,*.hpp call tagbar#autoopen()
set tags+=/usr/include/tags
Status Bar: vim-airline
This plugin replaces the standard status bar with a richer, more informative display, including git branch indicators and encoding information.
Configuration:
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
" Set a theme
let g:airline_theme='bubblegum'
Code Commenting: NERDCommenter
NERDCommenter provides consistent commenting functionality across different file types.
Configuration:
Plug 'preservim/nerdcommenter'
" Create a mapping for easier access
let mapleader = ","
Usage:
,cc: Comment out the current line or selection.,cu: Uncomment the current line or selection.
Variable Highlighting: vim-illuminate
This plugin automatically highlights all occurrences of the word under the cursor, aiding in code reading and refactoring.
Configuration:
Plug 'RRethy/vim-illuminate'
" Customize highlight color (Purple background)
hi illuminatedWord ctermbg=99 guibg=#875fff
Indentation Guides: indentLine
indentLine adds vertical lines to visualize indentation levels, making nested structures easier to read.
Configuration:
Plug 'Yggdroot/indentLine'
" Use a different character for guides
let g:indentLine_char = '¦'
let g:indentLine_enabled = 1