Essential Vim Operations and Recommended Plugins

A guide to basic Vim operations including mode switching, navigation, search and replace, along with recommended plugins like NERDTree, fzf, and coc.nvim.

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.
  • Esc key: Normal Mode. Allows command execution and cursor movement.
  • v: Visual Mode. Allows text selection and manipulation.
  • :: Command-line Mode. Allows entering and executing commands.
  • h: Move left
  • j: Move down
  • k: Move up
  • l: Move right
  • w: Move to the beginning of the next word
  • b: Move to the beginning of the previous word
  • {: Move to the beginning of the paragraph
  • }: Move to the end of the paragraph
  • gg: Move to the beginning of the file
  • G: Move to the end of the file
  • 0: 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 position
  • dd: Delete the current line
  • dw: Delete from cursor to end of word
  • 2dd: Delete 2 lines starting from the current line
  • u: Undo the last operation
  • Ctrl + r: Redo the undone operation
  • yy: Copy (yank) the current line
  • p: Paste below the cursor position
  • P: Paste above the cursor position
  • 2yy: Copy 2 lines starting from the current line
  • .: Repeat the last operation

Search and Replace

  • /search_term: Search downward
  • ?search_term: Search upward
  • n: Move to the next search result
  • N: 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 Mode
  • O: Insert a new line above the current line and enter Insert Mode
  • J: Join the current line with the next line

Vim Plugins

To extend Vim’s functionality, a plugin manager and plugins are essential.

Vim Plug

.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'

References