Vim
Vim
Vim Modes
Vim operates in several modes, each with a different purpose:
- Normal Mode (Command Mode):
- The default mode when you open Vim.
- Used for navigating and editing text.
- Common commands:
h
,j
,k
,l
(move cursor),x
(delete character),dd
(delete line),yy
(copy line),p
(paste).
- Insert Mode:
- Entered by pressing
i
,a
,o
, etc. - Used for inserting or modifying text.
- Return to Normal mode by pressing Esc.
- Entered by pressing
- Visual Mode:
- Entered by pressing
v
(character-wise),V
(line-wise), orCtrl+v
(block-wise). - Used to select text for operations like copying or deleting.
- Commands:
y
(yank/copy),d
(delete),>
,<
(indentation).
- Entered by pressing
- Command-Line Mode: Entered by pressing
:
in Normal mode. Used for executing commands like saving, quitting, searching, and more. Example::w
(save),:q
(quit),:wq
(save and quit),:x
(save and quit).
Basic Vim Commands
- Opening a File:
vim filename
: Opens a file in Vim.vim -R filename
: Opens a file in read-only mode.
- Saving and Quitting:
:w
: Save the file.:w filename
: Save as a new file.:q
: Quit Vim.:wq
: Save and quit.:q!
: Quit without saving.ZZ
: Save and quit (Normal mode).
- Navigation:
h
,j
,k
,l
: Move cursor left, down, up, right.0
: Move to the beginning of the line.$
: Move to the end of the line.gg
: Move to the beginning of the file.G
: Move to the end of the file.Ctrl+d
,Ctrl+u
: Scroll half-page down, up.Ctrl+f
,Ctrl+b
: Scroll full-page down, up.
- Editing:
i
: Enter Insert mode before the cursor.a
: Enter Insert mode after the cursor.o
: Open a new line below and enter Insert mode.O
: Open a new line above and enter Insert mode.x
: Delete the character under the cursor.dw
: Delete word.dd
: Delete line.u
: Undo.Ctrl+r
: Redo.y
: Yank (copy) selected text.p
: Paste after the cursor.P
: Paste before the cursor.
- Search and Replace:
/pattern
: Search for a pattern.n
: Repeat search forward.N
: Repeat search backward.:%s/old/new/g
: Replace all occurrences of old with new in the entire file.:s/old/new/g
: Replace in the current line.
Advanced Vim Features
- Registers:
- Vim has named registers (
"a
,"b
, …) for copying and pasting. - Use
"ay
to yank into registera
and"ap
to paste from registera
.
- Vim has named registers (
- Macros:
- Record and replay sequences of commands.
q<register>
: Start recording a macro into a register (e.g.,qa
).q
: Stop recording.@a
: Replay the macro in registera
.
- Marks:
- Set marks to jump between points in a file.
m<letter>
: Set a mark (e.g., ma).'a
: Jump to the marka
.
- Buffers, Windows, and Tabs:
- Buffers: Multiple files opened in Vim.
:bnext
,:bprev
: Navigate between buffers.
- Windows: Split screen for editing multiple files simultaneously.
:split filename
,:vsplit filename
: Split horizontally or vertically.Ctrl+w + h/j/k/l
: Navigate between windows.
- Tabs: Open files in tabs.
:tabnew filename
: Open a file in a new tab.gt
,gT
: Navigate between tabs.
- Buffers: Multiple files opened in Vim.
- Text Objects:
- Operate on text objects like words, sentences, paragraphs.
diw
: Delete inside word.dap
: Delete a paragraph.
Customization
- .vimrc File:
- Vim configuration file located in your home directory.
- Example settings:
set number " Show line numbers syntax on " Enable syntax highlighting set tabstop=4 " Set tab width to 4 spaces set expandtab " Convert tabs to spaces set shiftwidth=4 " Indentation width set autoindent " Enable automatic indentation
- Plugins:
- Extend Vim’s functionality with plugins.
- Vim Plugin Managers:
- Vundle: Plugin manager for Vim.
- Pathogen: Simplifies plugin management.
- vim-plug: A minimalist plugin manager.
- Example .vimrc for plugins:
call plug#begin('~/.vim/plugged')
Plug 'scrooloose/nerdtree' " File explorer
Plug 'tpope/vim-fugitive' " Git integration
call plug#end()
- Themes:
- Change the color scheme of Vim.
- Example: ```vim colorscheme desert ” Set color scheme to desert
### Using Vundle
```sh
git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim
vim +PluginInstall +qall
inside .vimrc
set nocompatible " Required for Vundle
filetype off " Required for Vundle
" Set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" Let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
" === Example Plugins ===
" NERDTree: File system explorer for Vim
Plugin 'preservim/nerdtree'
" vim-airline: Lean & mean status/tabline for Vim
Plugin 'vim-airline/vim-airline'
" vim-fugitive: A Git wrapper so awesome, it should be illegal
Plugin 'tpope/vim-fugitive'
" lightline.vim: A lightweight statusline
Plugin 'itchyny/lightline.vim'
" syntastic: Syntax checking plugin
Plugin 'vim-syntastic/syntastic'
" vim-surround: Surround text objects easily
Plugin 'tpope/vim-surround'
" ctrlp.vim: Fuzzy file, buffer, MRU, tag, etc. finder
Plugin 'ctrlpvim/ctrlp.vim'
" vim-commentary: Comment stuff out
Plugin 'tpope/vim-commentary'
" vim-gitgutter: Shows a git diff in the 'gutter' (sign column)
Plugin 'airblade/vim-gitgutter'
" YouCompleteMe: Fast, as-you-type, fuzzy-search code completion
Plugin 'ycm-core/YouCompleteMe'
" vim-polyglot: Language pack for Vim (syntax highlighting for many languages)
Plugin 'sheerun/vim-polyglot'
" vim-indent-guides: A Vim plugin for visually displaying indent levels in code
Plugin 'nathanaelkane/vim-indent-guides'
" vim-markdown: Markdown syntax highlighting and editing
Plugin 'preservim/vim-markdown'
" vim-easymotion: EasyMotion provides a much simpler way to use some motions in Vim
Plugin 'easymotion/vim-easymotion'
" vim-startify: A fancy start screen for Vim
Plugin 'mhinz/vim-startify'
call vundle#end() " Required for Vundle
filetype plugin indent on " Required for Vundle