Command Line Mode
- Saving changes
- Exit Vim
- Combining Save and Quit
- Editing buffers
- Search
- Search and Replace
- Editing lines filtered by pattern
- Shell Commands
- Miscellaneous
Any operation is completed by pressing Enter key after which the mode changes back to Normal mode
Press Esc key to ignore whatever is typed and return to Normal mode
Saving changes
:wsave changes:w filenameprovide a filename if it is a new file or if you want to save to another file:w!save changes even if file is read-only, provided user has appropriate permissions:wasave changes made to all the files opened
Exit Vim
:qquit the current file (if other tabs are open, they will remain) - if unsaved changes are there, you will get an error message:qaquit all:q!quit and ignore unsaved changes
Combining Save and Quit
:wqsave changes and quit:wq!save changes even if file is read-only and quit
Editing buffers
Multiple files can be opened in Vim within same tab and/or different tab
Buffers
:erefreshes the current buffer:e filenameopen file for editing:e#switch back to previous bufferCtrl+6can also be used
:e#1open first buffer:e#2open second buffer and so on:bnopen buffer next:bpopen buffer previous
Splitting
:split filenameopen file for editing in new horizontal split screen, Vim adds a highlighted horizontal bar with filename for each file thus opened- use
:sp filenamefor short
- use
:vsplit filenameopen file for editing in new vertical split screen- use
:vs filenamefor short
- use
- If filename is not given, the current one is used
Multiple tabs
:tabe filenameopen file for editing in new tab instead of adding to buffers:h :tabefor more info and different options
:tabnto go to next tabgtandCtrl+Page Downcan also be used2gtto move to second tab, the number specified is absolute, not relative
:tabpto go to previous tabgTandCtrl+Page Upcan also be used
:tabrto go to first tab (r for rewind):tablto go to last tab (l for last):tabm 0move current tab to beginning:tabm 2move current tab to 3rd position from left:tabmmove current tab to end
Making changes to all buffers
If multiple buffers are open and you want to apply common editing across all of them, use bufdo command
:silent! bufdo %s/searchpattern/replacestring/g | updatesubstitute across all buffers,silentskips displaying normal messages and!skips error messages as well- It is not an efficient way to open buffers just to search and replace a pattern across multiple files, use tools like
sed,awk,perlinstead :h :bufdo,:h :windoand:h :silentfor more info- how to change multiple files
- Effectively work with multiple files
- When to use Buffers and when to use Tabs
Search
/searchpatternsearch the given pattern in forward direction- Use
ncommand to move to next match andNfor previous match
- Use
?searchpatternsearch the given pattern in backward directionncommand goes to next match in backward direction andNmoves to next match in forward direction
- Press
Esckey to ignore typed pattern search and return to Normal mode
By default, cursor is placed at starting character of match
/searchpattern/s+2places cursor 2 characters after start of match/searchpattern/s-2places cursor 2 characters before start of match/searchpattern/eplaces cursor at end of match/searchpattern/e+4places cursor 4 characters after end of match/searchpattern/e-4places cursor 4 characters before end of match/searchpattern/+3places cursor 3 lines below match/searchpattern/-3places cursor 3 line above match
Highlight settings
:set hlsearchhighlights the matched pattern:set nohlsearchdo not highlight matched pattern:set hlsearch!toggle highlight setting:nohclear highlighted patterns, doesn't affect highlight settings
Search and Replace
General syntax is :range s/searchpattern/replacestring/flagss is short-form for substitute
Space between range and s is optional and may be used for clarity
:. s/a/b/replace first occurrence of character a with character b on current line only:2 s/apple/Mango/ireplace first occurrence of word apple with word Mango only on second lineiflag ignores case sensitivity for searchpattern
:3,6 s/call/jump/greplace all occurrences of call to jump on lines 3 to 6gflag changes default behavior to search and replace all occurrences
:1,$ s/call/jump/greplace all occurrences of call to jump in entire file$points to last line of file
:% s/call/jump/greplace all occurrences of call to jump in entire file%is short-cut for the range1,$
- More on substitute and Regular Expressions is covered in a later chapter.
:h :substituteand:h rangefor more info
Editing lines filtered by pattern
The g command, short for global allows to edit lines that are first filtered based on a searchpattern
:g/call/ddelete all lines containing the word call:1,5 g/call/ddelete lines containing the word call only from first five lines:v/jump/ddelete all lines NOT containing the word jump:g/cat/ s/animal/mammal/greplace 'animal' with 'mammal' only on lines containing the searchpattern 'cat':.,+20 g/^#/ normal >>indent from current line to next 20 lines that start with # character
For more info, see :h :g
Also, check out :h :t and :h :m to copy/move the filtered lines using :g and :h ex-cmd-index for complete list of commands to use with :g
Shell Commands
One can also use shell commands within Vim
:!lsexecute shell command and display output, doesn't change contents of file:.! datereplace current line with output ofdatecommand:%! sortsort all lines of file:3,8! sortsort only lines 3 to 8 of file:r! dateinsert output ofdatecommand below current line:r report.loginsert contents of file report.log below current line- Note that
!is not used as we are reading a file, not using external shell command
- Note that
:.!grep '^Help ' %replace current line with all line starting with Help in current file:shopen a shell session within Vim, useexitcommand to return to editing- Working with external commands
:h :!,:h :shand:h :rfor more info
Miscellaneous
:set numberprefix line numbers (it is a visual guideline, won't modify text):set nonumberremove line number prefix:set number!toggle number setting:set relativenumberprefix line numbers relative to current line- current line is 0, 1 for lines above and below, 2 for two lines above and below and so on
- useful visual guide for commands like
11yy, 6>>, 9jetc
:set norelativenumberremove relative line number prefix:set relativenumber!toggle relative number setting