Vim is a powerful text editor loved by many developers for its efficient operation. Here, I’ll introduce the basic operations of Vim and recommended plugins to improve development efficiency.
Basic Vim Operations
Vim has multiple modes, each allowing different operations.
Mode Switching
i: Insert Mode. Allows text input.Esckey: Normal Mode. Allows command execution and cursor movement.v: Visual Mode. Allows text selection and manipulation.:: Command-line Mode. Allows entering and executing commands.
Navigation in Normal Mode
h: Move leftj: Move downk: Move upl: Move rightw: Move to the beginning of the next wordb: Move to the beginning of the previous word{: Move to the beginning of the paragraph}: Move to the end of the paragraphgg: Move to the beginning of the fileG: Move to the end of the file0: Move to the beginning of the line$: Move to the end of the line
Saving and Exiting Files
:w: Save (overwrite):q: Quit (only if no changes):q!: Discard changes and force quit:wq: Save and quit:x: Save and quit (nearly the same as:wq)
Operations in Normal Mode
x: Delete the character at cursor positiondd: Delete the current linedw: Delete from cursor to end of word2dd: Delete 2 lines starting from the current lineu: Undo the last operationCtrl + r: Redo the undone operationyy: Copy (yank) the current linep: Paste below the cursor positionP: Paste above the cursor position2yy: Copy 2 lines starting from the current line.: Repeat the last operation
Search and Replace
/search_term: Search downward?search_term: Search upwardn: Move to the next search resultN: Move to the previous search result:%s/before/after/g: Replace all occurrences in the file:%s/before/after/gc: Replace all occurrences in the file (with confirmation)
Other Useful Operations
o: Insert a new line below the current line and enter Insert ModeO: Insert a new line above the current line and enter Insert ModeJ: Join the current line with the next line
Vim Plugins
To extend Vim’s functionality, a plugin manager and plugins are essential.
Vim Plug
- A lightweight plugin manager for Vim.
- https://github.com/junegunn/vim-plug
Recommended Plugins
- vim-horizon: A plugin to change Vim’s color scheme.
- NERDTree: A plugin that displays a directory tree in the sidebar.
- fzf: A fuzzy finder enabling fast file search, buffer search, and history search.
- vim-fugitive: A plugin to operate Git commands from within Vim.
- vim-gitgutter: A plugin that displays Git change diffs (additions, modifications, deletions) next to line numbers.
- vim-commentary: A plugin to easily comment/uncomment with the
gcccommand. - coc.nvim: A plugin providing IDE-like features such as code completion, LSP (Language Server Protocol) support, and snippets.
.vimrc Configuration Example
By adding the following settings to the ~/.vimrc file, you can customize Vim’s behavior and plugins.
" Set shell to zsh (change to your preferred shell)
set shell=/bin/zsh
" Indentation settings
set shiftwidth=4 " Auto-indent width
set tabstop=4 " Tab character display width
set expandtab " Convert tabs to spaces
set autoindent " Enable auto-indentation
" Search settings
set hlsearch " Highlight search results
" Clipboard settings (sync with system clipboard)
set clipboard=unnamed
" Enable syntax highlighting
syntax on
" Vim Plug initialization
call plug#begin()
Plug 'ntk148v/vim-horizon'
Plug 'preservim/nerdtree'
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'neoclide/coc.nvim', {'branch': 'release'}
call plug#end()
" Color scheme settings
set termguicolors " Enable terminal True Color (for correct color scheme display)
colorscheme horizon " Apply horizon color scheme
" lightline.vim configuration example (a plugin to customize the status line)
" If using lightline.vim, also add Plug 'itchyny/lightline.vim'
" let g:lightline = {}
" let g:lightline.colorscheme = 'horizon'
Frequently Asked Questions (FAQ)
Q. What if I can’t quit Vim?
If you’ve modified the file, :q fails with an error because the changes haven’t been saved. Save first with :wq (save and quit) or :x, or if you’re fine discarding the changes, force-quit with :q!. If you’re still stuck in Insert Mode, press Esc first to return to Normal Mode before entering a command.
Q. Why doesn’t pasting a copied line work the way I expect?
In Vim, yy yanks (copies) the current line, p pastes it below the cursor, and P pastes it above the cursor. Prefixing a number, as in 2yy, copies multiple lines at once. If the paste ends up in an unexpected place, check whether you used p or P.
Q. What if I lose track of which mode I’m in?
Vim has several modes — Insert, Normal, Visual, and Command-line — that you switch between as you work. If you’re not sure which mode you’re in, pressing Esc always returns you to Normal Mode. From there, i takes you to Insert Mode, v to Visual Mode, and : to Command-line Mode — keeping that flow in mind makes it easier to stay oriented.