vim
Vim is a highly efficient and powerful text editor used for coding, writing, and general text manipulation. Below are some basic commands and usage tips for getting started with Vim:
Basic Usage
-
Opening a File:
To open a file in Vim, use the following command:vim filename -
Switching Between Modes:
Vim operates in different modes, mainly:- Normal Mode: For navigation and manipulation. This is the default mode when you open Vim.
- Insert Mode: For typing text. You can enter this mode by pressing
ifrom normal mode. - Command-Line Mode: For saving, quitting, and other commands. Access it by pressing
:from normal mode.
Normal Mode Commands
-
Navigation:
- Move cursor up, down, left, and right:
k,j,h,l - Move to the beginning of the line:
0 - Move to the end of the line:
$ - Move to the beginning of the file:
gg - Move to the end of the file:
G - Move by word:
w(next word),b(previous word)
- Move cursor up, down, left, and right:
-
Deleting Text:
- Delete a character:
x - Delete a word:
dw - Delete a line:
dd - Delete to the end of the line:
D
- Delete a character:
-
Undo/Redo:
- Undo the last change:
u - Redo the undone change:
Ctrl + r
- Undo the last change:
-
Copy/Paste:
- Copy (yank) a word:
yw - Copy a line:
yy - Paste the copied content:
p
- Copy (yank) a word:
-
Search:
- Search for a term:
/term - Navigate between search results:
n(next),N(previous)
- Search for a term:
-
Replacing:
- Replace a character:
rfollowed by the character - Replace a word (interactive):
:%s/old/new/gc - Replace a word without confirmation:
:%s/old/new/g
- Replace a character:
-
Jumping Between Matching Parentheses/Brackets:
- Move to matching parenthesis:
%
- Move to matching parenthesis:
Insert Mode Commands
To enter Insert Mode from Normal Mode, press:
i(insert before the cursor)I(insert at the beginning of the line)a(insert after the cursor)A(insert at the end of the line)o(open a new line below the current line)O(open a new line above the current line)
Once in Insert Mode, type your text as usual. To return to Normal Mode, press Esc.
Command-Line Mode Commands
To enter Command-Line Mode, press : from Normal Mode.
-
Saving:
- Save the file:
:w - Save and exit:
:wq - Exit without saving:
:q!
- Save the file:
-
Exiting:
- Quit Vim:
:q - Quit if no changes were made:
:q!
- Quit Vim:
-
Searching and Replacing:
- Search for a term:
:/term - Replace all occurrences of a term:
:%s/old/new/g
- Search for a term:
-
File Information:
- Show file name and status:
:f - Show file path:
:echo expand('%:p')
- Show file name and status:
Visual Mode Commands
Visual Mode allows you to select text and perform actions on it.
- Enter Visual Mode: Press
vto start visual mode (character selection) orVfor line selection. - Select Text: Use arrow keys or
h,j,k,lto extend the selection. - Cut/Copy/Paste in Visual Mode:
- Cut (delete) selected text:
d - Copy (yank) selected text:
y - Paste after the selection:
p
- Cut (delete) selected text:
Other Useful Commands
-
Split Window:
- Open a new horizontal split:
:split filename - Open a new vertical split:
:vsplit filename - Switch between splits:
Ctrl + wfollowed byh,j,k, orl
- Open a new horizontal split:
-
Change Text Case:
- Change the case of a character:
~ - Change a word to uppercase:
gUiw - Change a word to lowercase:
guw
- Change the case of a character:
Advanced Navigation
-
Move by Paragraphs:
- Move to the beginning of the next paragraph:
} - Move to the beginning of the previous paragraph:
{
- Move to the beginning of the next paragraph:
-
Move by Search Results:
- Jump to the next search result:
n - Jump to the previous search result:
N - Search for the word under the cursor:
*(next occurrence),#(previous occurrence)
- Jump to the next search result:
-
Move to Specific Line:
- Go to a specific line number:
:n(replacenwith the line number) - Go to a specific column on a line:
n|(replacenwith the column number)
- Go to a specific line number:
-
Faster Navigation Between Files:
- Move to the first non-blank character on a line:
^ - Move to the end of the current sentence:
) - Move to the beginning of the current sentence:
( - Jump to a matching bracket, parenthesis, or brace:
% - Go to the start or end of the current block:
[{or]}
- Move to the first non-blank character on a line:
-
Jump to Specific Marks:
- Set a mark at the current cursor position:
m{a-z} - Jump to the mark:
'aor`a(lowercase for line, backtick for exact position)
- Set a mark at the current cursor position:
Advanced Editing
-
Advanced Text Manipulation:
- Change a word:
cw - Change until the next character:
c{char} - Change until the end of the line:
C - Delete a word backwards:
dB - Delete everything until the end of the current sentence:
d) - Replace text:
r{char}(replace current character under cursor)
- Change a word:
-
Macro Recording:
- Start recording a macro:
q{register}(e.g.,qato record to registera) - Perform multiple commands while recording.
- Stop recording:
q - Play the macro:
@{register}(e.g.,@ato replay the macro recorded in registera) - Play the macro multiple times:
10@a(plays macroa10 times)
- Start recording a macro:
-
Text Object Motions:
- Change inside a word:
ciw - Change inside a paragraph:
cip - Change inside quotes:
ci" - Delete inside a tag:
dit - Select a sentence:
vas
- Change inside a word:
-
Multiple Cursor Editing (Using Plugins):
- If you want to use multiple cursors, you can use a plugin like vim-multiple-cursors.
- Add multiple cursors using
Ctrl + n, and then modify text at each cursor.
Command-Line Mode Advanced Features
-
Ex Commands for Text Substitution:
- Replace all occurrences of a word:
:%s/old/new/g - Replace all occurrences with confirmation:
:%s/old/new/gc - Replace in a specific range of lines:
:5,10s/old/new/g(replace between lines 5 and 10)
- Replace all occurrences of a word:
-
Execute Shell Commands:
- Run a shell command from within Vim:
:!command - Replace the content of the file with the output of a shell command:
:%!command
- Run a shell command from within Vim:
-
Vim Buffers and Windows:
- List open buffers:
:ls - Switch between buffers:
:b{buffer_number}or:bnext - Delete a buffer (close it):
:bd - Open a new window:
:new filename - Split window horizontally:
:split - Split window vertically:
:vsplit - Switch between windows:
Ctrl + w {h,j,k,l}
- List open buffers:
Advanced Search and Replace
-
Regular Expressions:
- Vim supports regular expressions for more complex search and replace:
- Search for a word that starts with "test":
/\v^test - Replace all digits with
X::%s/\d/X/g - Replace a specific line number range:
:5,10s/old/new/g
- Search for a word that starts with "test":
- Vim supports regular expressions for more complex search and replace:
-
Search and Replace Across Multiple Files:
- Search across all files in the current directory and replace:
:args *.txt | argdo %s/old/new/g - Search recursively through all files in a directory:
:args **/*.py | argdo %s/old/new/g
- Search across all files in the current directory and replace:
Managing Sessions and Projects
-
Saving and Loading Sessions:
- Save your session (buffers, windows, etc.):
:mksession! - Load a session:
:source Session.vim
- Save your session (buffers, windows, etc.):
-
Project Management:
- Use vim-projectionist or NERDTree plugins to manage your project files.
- Quickly navigate between related files, such as opening tests with
:TestFile.
Advanced Window and Tab Management
-
Using Tabs in Vim:
- Open a new tab:
:tabnew - Switch between tabs:
:tabnext,:tabprev - Close the current tab:
:tabclose - List open tabs:
:tabs
- Open a new tab:
-
Window Management:
- Create a new horizontal split:
:split filename - Create a new vertical split:
:vsplit filename - Switch between windows:
Ctrl + wthenh/j/k/l - Close a window:
Ctrl + wthenc - Resize a window:
Ctrl + wthen+or-(to resize up or down)
- Create a new horizontal split:
Advanced Vim Plugins
-
Fuzzy Finder (
fzforCtrlP):- Use fzf or CtrlP for fast file navigation and searching in your project directory.
Ctrl + popens the fuzzy finder for file navigation.
-
Autocompletion (
coc.nvimorYouCompleteMe):- Use coc.nvim for autocompletion powered by the Language Server Protocol (LSP).
- Install plugins for linting, formatting, and autocompletion.
-
Git Integration (
vim-fugitive):- View Git status:
:Gstatus - Stage changes:
:Gadd - View commit history:
:Glog
- View Git status:
-
Syntax Highlighting and File Detection (
vim-polyglot):- Install vim-polyglot for support for multiple languages and enhanced syntax highlighting.
Customization with Vimrc
-
Setting Up Key Mappings:
-
Remap keys in your
.vimrcfile:nnoremap <leader>f :Files<CR> " Map to fuzzy file search vnoremap <leader>c "+y " Copy selected text to system clipboard
-
-
Plugins and Package Management:
-
Use vim-plug or Vundle to manage plugins. For example:
call plug#begin('~/.vim/plugged') Plug 'junegunn/fzf' Plug 'tpope/vim-fugitive' call plug#end()
-
-
Customizing the Status Line:
- Customize the status line using vim-airline or lightline.vim to show helpful information such as the current file name, line number, and Git branch.
-
Enable Clipboard Support:
- For clipboard integration, ensure Vim is compiled with clipboard support. If not, install Vim with
+clipboard. - You can yank to the system clipboard with:
"+yand paste with"+p.
- For clipboard integration, ensure Vim is compiled with clipboard support. If not, install Vim with