Vim

Development Tool

Vim

Overview

Vim is an open-source text editor developed by Bram Moolenaar. Created as an improved version of the Vi editor, it is beloved by many programmers for its unique modal editing approach and high extensibility.

Details

Vim was released in 1991, developed as an abbreviation for "Vi IMproved." It comes pre-installed on almost all Unix-like systems and is an essential tool for remote server work via SSH and command-line environment development. Currently, it is maintained nearly 100% by Bram Moolenaar.

Vim's greatest feature is its modal editing system. Users edit by switching between different modes: Normal mode (command execution), Insert mode (text input), Visual mode (range selection), and others. This unique operation method allows all operations to be performed rapidly using only the keyboard without a mouse.

It provides advanced text editing features including powerful macro functionality with regular expressions, rich plugin system, customization through Vimscript, buffer and window management, tab functionality, and split display. Configuration is managed through the vimrc file and can be finely customized according to user preferences.

Advantages and Disadvantages

Advantages

  • Available Everywhere: Pre-installed on almost all Linux distributions
  • Extremely Lightweight: Operates with minimal system resources
  • Fast Operation: Efficient keyboard-centric operation through modal editing
  • Free and Open Source: Completely free to use
  • Powerful Customization: Flexible extensibility through Vimscript
  • Remote Work Ready: Optimal for server work via SSH
  • Version Control Friendly: Text-based configuration easily managed with Git

Disadvantages

  • Steep Learning Curve: Requires time to master the unique operation method
  • Limited Maintenance: Primarily developed by Bram Moolenaar alone
  • GUI Feature Limitations: Terminal-based with limited GUI integration features
  • Difficult for Beginners: Challenging until basic operations are learned
  • Plugin Management: Advanced features require plugin knowledge
  • Lack of Modern Features: Slower introduction of new features compared to Neovim
  • Configuration Complexity: Optimal setup requires deep knowledge

Key Links

Code Examples

Basic vimrc Configuration

" .vimrc - Basic Configuration
set nocompatible              " Disable Vi compatibility mode

" Basic settings
set number                    " Show line numbers
set relativenumber           " Show relative line numbers
set ruler                    " Show cursor position
set showcmd                  " Show commands
set showmode                 " Show mode
set cursorline              " Highlight cursor line
set laststatus=2            " Always show status line

" Indentation settings
set autoindent              " Auto indent
set smartindent             " Smart indent
set cindent                 " C-style indent
set tabstop=4               " Tab width
set shiftwidth=4            " Shift width
set expandtab               " Convert tabs to spaces
set softtabstop=4           " Soft tab

" Search settings
set incsearch               " Incremental search
set hlsearch                " Highlight search results
set ignorecase              " Ignore case
set smartcase               " Case sensitive when uppercase included

" Display settings
set wrap                    " Line wrapping
set linebreak               " Wrap at word boundaries
set showmatch               " Highlight matching brackets
set list                    " Show invisible characters
set listchars=tab:▸\ ,trail:▫,extends:❯,precedes:❮,nbsp:Ø

" File settings
set encoding=utf-8          " Character encoding
set fileencoding=utf-8      " File encoding
set fileformat=unix         " File format
set backup                  " Create backup files
set backupdir=~/.vim/backup " Backup directory
set undofile                " Create undo files
set undodir=~/.vim/undo     " Undo directory

" Colors and theme
syntax enable               " Syntax highlighting
colorscheme desert          " Color scheme
set background=dark         " Background color

" Key mappings
let mapleader = ","         " Set leader key
nnoremap <Leader>w :w<CR>   " ,w to save
nnoremap <Leader>q :q<CR>   " ,q to quit
nnoremap <Leader>h :nohlsearch<CR> " ,h to clear search highlight
nnoremap j gj               " Move by display lines
nnoremap k gk               " Move by display lines

" Split window settings
set splitbelow              " Horizontal split opens below
set splitright              " Vertical split opens right
nnoremap <C-h> <C-w>h       " Ctrl+h to move to left window
nnoremap <C-j> <C-w>j       " Ctrl+j to move to bottom window
nnoremap <C-k> <C-w>k       " Ctrl+k to move to top window
nnoremap <C-l> <C-w>l       " Ctrl+l to move to right window

Plugin Management (vim-plug)

" Plugin configuration
call plug#begin('~/.vim/plugged')

" Essential plugins
Plug 'tpope/vim-sensible'          " Basic settings
Plug 'tpope/vim-surround'          " Surround text operations
Plug 'tpope/vim-commentary'        " Comment operations
Plug 'tpope/vim-fugitive'          " Git integration

" File explorer
Plug 'preservim/nerdtree'          " File tree
Plug 'Xuyuanp/nerdtree-git-plugin' " NERDTree Git integration

" Search and navigation
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'           " Fuzzy finder
Plug 'easymotion/vim-easymotion'  " Fast navigation

" Completion and LSP
Plug 'neoclide/coc.nvim', {'branch': 'release'} " Completion engine
Plug 'dense-analysis/ale'          " Linter

" Appearance
Plug 'vim-airline/vim-airline'     " Status line
Plug 'vim-airline/vim-airline-themes' " Themes
Plug 'morhetz/gruvbox'             " Color scheme

" Language support
Plug 'sheerun/vim-polyglot'        " Multi-language support
Plug 'pangloss/vim-javascript'     " JavaScript
Plug 'leafgarland/typescript-vim'  " TypeScript

call plug#end()

" Plugin configuration
let g:gruvbox_contrast_dark = 'hard'
colorscheme gruvbox

" NERDTree configuration
nnoremap <Leader>n :NERDTreeToggle<CR>
let NERDTreeShowHidden=1

" FZF configuration
nnoremap <Leader>f :Files<CR>
nnoremap <Leader>b :Buffers<CR>
nnoremap <Leader>g :Ag<CR>

" Airline configuration
let g:airline_theme='gruvbox'
let g:airline_powerline_fonts = 1
let g:airline#extensions#tabline#enabled = 1

" CoC configuration
inoremap <silent><expr> <TAB>
  \ pumvisible() ? "\<C-n>" :
  \ <SID>check_back_space() ? "\<TAB>" :
  \ coc#refresh()

function! s:check_back_space() abort
  let col = col('.') - 1
  return !col || getline('.')[col - 1]  =~# '\s'
endfunction

Custom Function Examples

" Custom function definitions

" Function to trim trailing whitespace
function! TrimWhitespace()
    let l:save = winsaveview()
    keeppatterns %s/\s\+$//e
    call winrestview(l:save)
endfunction

" Function to copy current file
function! CopyFile()
    let filename = expand('%:t')
    let new_name = input('New filename: ', filename)
    if new_name != '' && new_name != filename
        execute 'saveas ' . new_name
    endif
endfunction

" Function to escape HTML tags
function! HtmlEscape()
    silent s/&/\&amp;/eg
    silent s/</\&lt;/eg
    silent s/>/\&gt;/eg
endfunction

" Function to duplicate current line
function! DuplicateLine()
    let line = getline('.')
    call append('.', line)
endfunction

" Function key mappings
nnoremap <Leader>tw :call TrimWhitespace()<CR>
nnoremap <Leader>cf :call CopyFile()<CR>
vnoremap <Leader>he :call HtmlEscape()<CR>
nnoremap <Leader>dl :call DuplicateLine()<CR>

Autocommand Examples

" Autocommand configuration
augroup vimrc_autocmds
    autocmd!
    
    " File type specific settings
    autocmd FileType python setlocal tabstop=4 shiftwidth=4 expandtab
    autocmd FileType javascript setlocal tabstop=2 shiftwidth=2 expandtab
    autocmd FileType html setlocal tabstop=2 shiftwidth=2 expandtab
    autocmd FileType css setlocal tabstop=2 shiftwidth=2 expandtab
    autocmd FileType markdown setlocal wrap linebreak
    
    " Actions on save
    autocmd BufWritePre *.py,*.js,*.html,*.css :call TrimWhitespace()
    
    " Actions on file read
    autocmd BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif
    
    " Templates for new files
    autocmd BufNewFile *.py 0r ~/.vim/templates/python.py
    autocmd BufNewFile *.html 0r ~/.vim/templates/html.html
    
    " Window resize adjustment
    autocmd VimResized * wincmd =
    
augroup END

Macro and Register Usage

" Macro examples

" 1. Start macro recording with q, record to register a
" qa
" I"<Esc>A"<Esc>j
" q
" Above macro adds double quotes to beginning and end of line

" 2. Execute macro
" @a (execute once)
" 10@a (execute 10 times)

" Register usage examples
" "ayy     Yank current line to register a
" "ap      Paste from register a
" "Ayy     Append yank to register a
" "*yy     Yank to system clipboard
" "+yy     Yank to selection clipboard

" Useful command examples
" :reg     Display register contents
" :marks   Display mark list
" :jumps   Display jump list
" :changes Display change list

" Advanced search and replace examples
" :%s/\v(pattern)/replacement/gc  " Replace with confirmation
" :g/pattern/d                    " Delete lines matching pattern
" :v/pattern/d                    " Delete lines not matching pattern
" :%s/\n//g                       " Remove all newlines