How to Use Vim: Mode Switching, Save/Quit, Movement & Editing Commands, and Recommended Plugins

A guide to using Vim, covering mode switching, cursor movement, saving and quitting (:wq/:q!), search and replace, and copy/paste commands, plus recommended plugins like NERDTree, fzf, and coc.nvim, with a .vimrc example.

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'

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.

References