Vim: Vi Improved
Vim (Vi Improved) is an enhanced version of the vi editor, created by Bram Moolenaar in 1991. It extends vi with numerous features while maintaining backward compatibility, making it one of the most popular and powerful text editors for developers.
Why Choose Vim?
- Powerful and extensible: Thousands of plugins and customizations
- Efficient editing: Modal editing with powerful commands
- Native Linux integration: Seamlessly integrates with Linux workflows
- Active community: Large ecosystem of plugins and configurations
- Programming-focused: Excellent support for Linux development workflows
- Highly customizable: Extensive configuration options
- System administration: Perfect for Linux server management and configuration
Installation
Linux Distributions
# Ubuntu/Debian
sudo apt update
sudo apt install vim
# CentOS/RHEL/Fedora
sudo yum install vim
# or
sudo dnf install vim
# Arch Linux
sudo pacman -S vim
# Alpine Linux
sudo apk add vim
Building from Source
# Clone Vim repository
git clone https://github.com/vim/vim.git
cd vim
# Configure build
./configure --with-features=huge \
--enable-multibyte \
--enable-python3interp \
--enable-gui=gtk3 \
--enable-cscope
# Compile and install
make
sudo make install
Vim Modes
Vim extends vi’s modal concept with additional modes:
1. Normal Mode
- Default mode for navigation and commands
- Press
Esc
to enter from any other mode
2. Insert Mode
- For typing text
- Enter with
i
,a
,o
,I
,A
,O
3. Visual Mode
- For selecting text
v
for character-wise selectionV
for line-wise selectionCtrl+v
for block-wise selection
4. Command-Line Mode
- For ex commands
- Enter with
:
,/
,?
5. Replace Mode
- For overwriting text
- Enter with
R
Advanced Text Editing
Visual Mode Operations
# Select text and perform operations
v # Start visual selection
V # Select entire lines
Ctrl+v # Block selection
# Operations on selected text
d # Delete selection
y # Yank (copy) selection
c # Change selection
> # Indent selection
< # Unindent selection
= # Auto-indent selection
Advanced Copy/Paste
# Named registers
"ayy # Yank line to register 'a'
"ap # Paste from register 'a'
"Ayy # Append line to register 'a'
# Special registers
"+y # Yank to system clipboard
"+p # Paste from system clipboard
"*y # Yank to selection clipboard
"*p # Paste from selection clipboard
""p # Paste from unnamed register
".p # Paste last inserted text
Text Objects
# Inner text objects
iw # Inner word
is # Inner sentence
ip # Inner paragraph
it # Inner tag
i" # Inner quotes
i( # Inner parentheses
i{ # Inner braces
i[ # Inner brackets
# Around text objects
aw # Around word (includes spaces)
as # Around sentence
ap # Around paragraph
at # Around tag
a" # Around quotes
a( # Around parentheses
a{ # Around braces
a[ # Around brackets
# Usage examples
diw # Delete inner word" # Change inner quotes
ciyap # Yank around paragraph
Configuration and Customization
Vimrc Configuration
" ~/.vimrc - Basic configuration
" General settings
set nocompatible " Disable vi compatibility
set number " Show line numbers
set relativenumber " Show relative line numbers
set ruler " Show cursor position
set showcmd " Show command in status line
set showmatch " Show matching brackets
set hlsearch " Highlight search results
set incsearch " Incremental search
set ignorecase " Ignore case in search
set smartcase " Smart case sensitivity
set autoindent " Auto-indent new lines
set smartindent " Smart indentation
set expandtab " Use spaces instead of tabs
set tabstop=4 " Tab width
set shiftwidth=4 " Indent width
set softtabstop=4 " Soft tab width
set wrap " Wrap long lines
set linebreak " Break lines at word boundaries
set scrolloff=5 " Keep 5 lines visible around cursor
set sidescrolloff=5 " Keep 5 columns visible around cursor
set backspace=indent,eol,start " Allow backspace over everything
set wildmenu " Enhanced command completion
set wildmode=longest:full,full " Command completion mode
set laststatus=2 " Always show status line
set encoding=utf-8 " Use UTF-8 encoding
set fileencoding=utf-8 " File encoding
set termencoding=utf-8 " Terminal encoding
" Visual settings
syntax enable " Enable syntax highlighting
set background=dark " Dark background
colorscheme desert " Color scheme
set cursorline " Highlight current line
set colorcolumn=80 " Show column at 80 characters
" Search settings
set path+=** " Search in subdirectories
set wildignore+=*.o,*.obj,*.pyc,*.class " Ignore compiled files
" Backup and swap settings
set backup " Enable backups
set backupdir=~/.vim/backup " Backup directory
set directory=~/.vim/swap " Swap file directory
set undofile " Persistent undo
set undodir=~/.vim/undo " Undo directory
" Create directories if they don't exist
if !isdirectory($HOME."/.vim/backup")
call mkdir($HOME."/.vim/backup", "p")
endif
if !isdirectory($HOME."/.vim/swap")
call mkdir($HOME."/.vim/swap", "p")
endif
if !isdirectory($HOME."/.vim/undo")
call mkdir($HOME."/.vim/undo", "p")
endif
" Key mappings
let mapleader = "," " Set leader key
nnoremap <leader>w :w<CR> " Quick save
nnoremap <leader>q :q<CR> " Quick quit
nnoremap <leader>x :x<CR> " Quick save and quit
nnoremap <leader>h :noh<CR> " Clear search highlighting
nnoremap <C-h> <C-w>h " Window navigation
nnoremap <C-j> <C-w>j
nnoremap <C-k> <C-w>k
nnoremap <C-l> <C-w>l
" Function key mappings
nnoremap <F2> :set number!<CR> " Toggle line numbers
nnoremap <F3> :set paste!<CR> " Toggle paste mode
nnoremap <F4> :set wrap!<CR> " Toggle line wrapping
nnoremap <F5> :source ~/.vimrc<CR> " Reload vimrc
" Auto commands
autocmd BufWritePre * :%s/\s\+$//e " Remove trailing whitespace on save
autocmd BufRead,BufNewFile *.py set filetype=python
autocmd BufRead,BufNewFile *.js set filetype=javascript
autocmd BufRead,BufNewFile *.html set filetype=html
Advanced Vimrc Features
" Advanced ~/.vimrc additions
" Status line customization
set statusline=%f " File name
set statusline+=%m " Modified flag
set statusline+=%r " Read-only flag
set statusline+=%h " Help flag
set statusline+=%w " Preview flag
set statusline+=%= " Right align
set statusline+=%y " File type
set statusline+=[%{&ff}] " File format
set statusline+=[%{&fenc}] " File encoding
set statusline+=\ %l/%L " Line number/total lines
set statusline+=\ %c " Column number
set statusline+=\ %P " Percentage through file
" Custom functions
function! ToggleBackground()
if &background == 'dark'
set background=light
else
set background=dark
endif
endfunction
nnoremap <F6> :call ToggleBackground()<CR>
" Word count function
function! WordCount()
let s:old_status = v:statusmsg
let position = getpos(".")
exe ":silent normal g\<c-g>"
let stat = v:statusmsg
let s:word_count = 0
if stat != '--No lines in buffer--'
let s:word_count = str2nr(split(v:statusmsg)[11])
let v:statusmsg = s:old_status
end
call setpos('.', position)
return s:word_count
endfunction
" File type specific settings
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4
autocmd FileType javascript setlocal expandtab shiftwidth=2 softtabstop=2
autocmd FileType html setlocal expandtab shiftwidth=2 softtabstop=2
autocmd FileType css setlocal expandtab shiftwidth=2 softtabstop=2
autocmd FileType yaml setlocal expandtab shiftwidth=2 softtabstop=2
autocmd FileType markdown setlocal wrap linebreak nolist textwidth=0 wrapmargin=0
" Abbreviations
iabbrev teh the
iabbrev adn and
iabbrev seperate separate
iabbrev recieve receive
Plugin Management
Using vim-plug
# Install vim-plug
curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
" Add to ~/.vimrc
call plug#begin('~/.vim/plugged')
" Essential plugins
Plug 'tpope/vim-sensible' " Sensible defaults
Plug 'tpope/vim-surround' " Surround text objects
Plug 'tpope/vim-commentary' " Comment/uncomment
Plug 'tpope/vim-fugitive' " Git integration
Plug 'scrooloose/nerdtree' " File explorer
Plug 'ctrlpvim/ctrlp.vim' " Fuzzy file finder
Plug 'vim-airline/vim-airline' " Status line
Plug 'vim-airline/vim-airline-themes' " Airline themes
Plug 'airblade/vim-gitgutter' " Git diff in gutter
Plug 'dense-analysis/ale' " Linting and fixing
Plug 'ycm-core/YouCompleteMe' " Code completion
Plug 'preservim/tagbar' " Tag browser
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim' " FZF integration
" Language-specific plugins
Plug 'vim-python/python-syntax' " Python syntax
Plug 'pangloss/vim-javascript' " JavaScript syntax
Plug 'mxw/vim-jsx' " JSX syntax
Plug 'rust-lang/rust.vim' " Rust support
Plug 'fatih/vim-go' " Go support
" Color schemes
Plug 'morhetz/gruvbox' " Gruvbox theme
Plug 'dracula/vim', { 'as': 'dracula' } " Dracula theme
Plug 'joshdick/onedark.vim' " One Dark theme
call plug#end()
" Plugin configurations
let g:airline_theme='gruvbox'
let g:gruvbox_contrast_dark='hard'
colorscheme gruvbox
" NERDTree configuration
nnoremap <F7> :NERDTreeToggle<CR>
let NERDTreeShowHidden=1
" CtrlP configuration
let g:ctrlp_map = '<c-p>'
let g:ctrlp_cmd = 'CtrlP'
let g:ctrlp_working_path_mode = 'ra'
" ALE configuration
let g:ale_linters = {
\ 'python': ['flake8', 'pylint'],
\ 'javascript': ['eslint'],
\}
let g:ale_fixers = {
\ 'python': ['autopep8', 'black'],
\ 'javascript': ['prettier'],
\}
Installing Plugins
" In Vim command mode
:PlugInstall " Install plugins
:PlugUpdate " Update plugins
:PlugClean " Remove unused plugins
:PlugStatus " Check plugin status
Programming with Vim
Code Completion
# Built-in completion
Ctrl+n # Next completion
Ctrl+p # Previous completion
Ctrl+x Ctrl+f # File name completion
Ctrl+x Ctrl+l # Line completion
Ctrl+x Ctrl+k # Dictionary completion
Ctrl+x Ctrl+o # Omni completion
Debugging Integration
" GDB integration
packadd termdebug
:Termdebug program_name
" Debugging commands
:Break " Set breakpoint
:Clear " Clear breakpoint
:Step " Step into
:Over # Step over
:Continue " Continue execution
Real-World Development Scenarios
Scenario 1: Python Development Setup
" Python-specific vimrc additions
autocmd FileType python setlocal expandtab shiftwidth=4 softtabstop=4
autocmd FileType python setlocal textwidth=79
autocmd FileType python setlocal colorcolumn=80
autocmd FileType python nnoremap <buffer> <F9> :exec '!python' shellescape(@%, 1)<cr>
" Python debugging
autocmd FileType python nnoremap <buffer> <F10> :exec '!python -m pdb' shellescape(@%, 1)<cr>
" Virtual environment detection
py3 << EOF
import os
import sys
if 'VIRTUAL_ENV' in os.environ:
project_base_dir = os.environ['VIRTUAL_ENV']
activate_this = os.path.join(project_base_dir, 'bin/activate_this.py')
exec(open(activate_this).read(), dict(__file__=activate_this))
EOF
Scenario 2: Web Development Workflow
" Web development configuration
autocmd FileType html,css,javascript setlocal expandtab shiftwidth=2 softtabstop=2
" Live reload setup
autocmd BufWritePost *.html,*.css,*.js silent !browser-sync reload
" Emmet integration
let g:user_emmet_leader_key=','
" Auto-close tags
let g:closetag_filenames = '*.html,*.xhtml,*.phtml,*.jsx'
" Prettier formatting
autocmd BufWritePre *.js,*.jsx,*.css,*.html PrettierAsync
Scenario 3: Linux System Administration
" Linux configuration file editing
autocmd BufRead,BufNewFile /etc/* setlocal nobackup nowritebackup
autocmd BufRead,BufNewFile /etc/systemd/system/* setlocal filetype=systemd
autocmd BufRead,BufNewFile *.conf setlocal syntax=conf
autocmd BufRead,BufNewFile /etc/nginx/* setlocal filetype=nginx
autocmd BufRead,BufNewFile /etc/apache2/* setlocal filetype=apache
" Linux log file viewing
autocmd BufRead,BufNewFile /var/log/* setlocal readonly
autocmd BufRead,BufNewFile /var/log/* normal G
autocmd BufRead,BufNewFile /var/log/syslog setlocal filetype=messages
autocmd BufRead,BufNewFile /var/log/auth.log setlocal filetype=messages
" Sudo save function for system files
command! W w !sudo tee % > /dev/null
command! Wsudo w !sudo tee % > /dev/null
" Quick Linux config editing
nnoremap <leader>ev :vsplit $MYVIMRC<cr>
nnoremap <leader>sv :source $MYVIMRC<cr>
nnoremap <leader>eh :vsplit /etc/hosts<cr>
nnoremap <leader>es :vsplit /etc/ssh/sshd_config<cr>
nnoremap <leader>en :vsplit /etc/nginx/nginx.conf<cr>
" Systemd service file templates
autocmd BufNewFile *.service 0r ~/.vim/templates/systemd.service
autocmd BufNewFile *.timer 0r ~/.vim/templates/systemd.timer
" Linux-specific commands
command! Systemctl !sudo systemctl
command! Journalctl !journalctl
command! Dmesg !dmesg | tail -20
Advanced Vim Techniques
Macros and Automation
# Recording macros
qa # Start recording macro 'a'
# ... perform actions ...
q # Stop recording
# Playing macros
@a # Execute macro 'a'
@@ # Repeat last macro
10@a # Execute macro 'a' 10 times
# Editing macros
:let @a='...' # Edit macro 'a' directly
Regular Expressions
# Search patterns
/\v<word> # Very magic mode - exact word
/\c<Word> # Case insensitive search
/\C<Word> # Case sensitive search
# Substitution patterns
:%s/\v(\w+)\s+(\w+)/\2 \1/g # Swap words
:%s/\v^(.{80}).*/\1/g # Truncate lines to 80 chars
:%s/\v\s+$//g # Remove trailing whitespace
Custom Commands
" Custom commands in vimrc
command! -nargs=1 Search execute 'vimgrep /<args>/gj **/*.py' | copen
command! Todo execute 'vimgrep /TODO\|FIXME\|XXX/gj **/*' | copen
command! RemoveTrailingSpaces %s/\s\+$//e
command! -range=% FormatJSON <line1>,<line2>!python -m json.tool
" Usage
:Search function_name
:Todo
:RemoveTrailingSpaces
:FormatJSON
Performance Optimization
Vim Performance Tips
" Performance optimizations
set lazyredraw " Don't redraw during macros
set ttyfast " Fast terminal connection
set synmaxcol=200 " Limit syntax highlighting column
set regexpengine=1 " Use old regex engine for speed
" Disable unused features
set noshowmatch " Don't show matching brackets
let loaded_matchparen = 1 " Disable matchparen plugin
Large File Handling
" Large file handling
autocmd BufReadPre * if getfsize(expand("%")) > 10000000 | syntax off | endif
" Function for large files
function! LargeFile()
syntax off
set eventignore+=FileType
setlocal bufhidden=unload
setlocal buftype=nowrite
setlocal undolevels=-1
endfunction
autocmd BufReadPre * if getfsize(expand("%")) > 50000000 | call LargeFile() | endif
Troubleshooting Common Issues
Performance Problems
# Profile Vim startup
vim --startuptime startup.log
# Check what's slowing down Vim
:profile start profile.log
:profile func *
:profile file *
# ... use Vim normally ...
:profile pause
:noautocmd qall!
Plugin Conflicts
# Start Vim without plugins
vim -u NONE -N
# Start with minimal config
vim -u ~/.vimrc.minimal
# Debug plugin loading
vim -V9myVim.log
Recovery and Backup
# Recover from swap file
vim -r filename.txt
# List swap files
vim -r
# Disable swap files (not recommended)
set noswapfile
Vim’s power lies in its extensibility and efficiency. While it has a steep learning curve, mastering Vim can significantly improve your editing speed and workflow, especially for programming and system administration tasks.