An operation in Command-line mode is completed by pressing the Enter key after which the mode changes back to the Normal mode. Press Esc key to ignore whatever is typed and return to the Normal mode.
Documentation links:
Save changesAdd
:
orc_
prefix for built-in help on Command-line mode, for example :h :w and :h c_CTRL-R. Use single quotes around options, :h 'autoindent' for example.
:w
is short for :write
)w!
will create a new file if it doesn't exist):wa
is short for :wall
)Appending
!
forces Vim to override errors, provided you have the appropriate permissions. For example, if you have edited a read-only file, :w will result in an error and :w! will save the changes. Another case where you'll get an error is :w filename if the file already exists. Using :w! filename will override the error.
Quit VimBy default, the entire file content is used for these commands. You can use a range (discussed later in this chapter) to work with selective lines.
:q
is short for :quit
)
:qa
is short for :quitall
)
Combining Save and QuitAppend
!
to discard unsaved changes and quit.
Working with buffers and tabsAppend
!
to override errors. Not all errors can be skipped, for example unsaved changes on a file that hasn't been named yet.
Multiple files can be opened in Vim within the same tab page and/or in different tabs. From :h windows-intro:
Buffers
- A buffer is the in-memory text of a file.
- A window is a viewport on a buffer.
- A tab page is a collection of windows.
:e
is short for :edit
)e
and #
):bn
is short for :bnext
)
:bp
is short for :bprevious
)
Use :set hidden if you want to switch to another buffer even if there are unsaved changes in the current buffer. Instead of this setting, you can also use :hide edit filename to hide the current unsaved buffer. You'll still get an error if you try to quit Vim without saving such buffers, unless you use the
!
modifier.
See :h 'autowrite' option if you want to automatically save changes when moving to another buffer.
TabsSee :h 22.4 and :h buffer-hidden for user and reference manuals on working with buffer lists.
:tabe
is short for :tabedit
)
filename
isn't specified, you'll get an unnamed empty window:tabn
is short for :tabnext
)
:tabp
is short for :tabprevious
)
:tabr
is short for :tabrewind
)
:tabl
is short for :tablast
)N
tabs from the start (:tabm
is short for :tabmove
)
N
positions to the rightN
positions to the leftBuffer list includes all the files opened in all the tabs.
SplittingYou can also use the mouse to switch/move tabs in GVim.
hjkl
or arrow keys, switch in the respective directionHJKL
(uppercase), moves the current split to the farthest possible location in the respective directionIf filename is not provided, the current one is used.
Edit all buffersVim adds a highlighted horizontal bar containing the filename for each split.
If multiple buffers are open and you want to apply a common editing task for all of them, one option is to use the bufdo
command.
silent
skips displaying normal messages!
skips error messagessed
, awk
and perl
instead.
Further reading
From :h options.txt:
Vim has a number of internal variables and switches which can be set to achieve special effects. These options come in three forms:
- boolean can only be on or off
- number has a numeric value
- string has a string value
Here are examples for each of these forms:
+=
allows you to append to an existing string valueUsage guidelines:
set {option}
switch on the given boolean setting
set {option}!
toggle the given boolean setting
set inv{option}
can also be usedset no{option}
switch off the given boolean setting
set {option}?
get the current value of the given option (works for all three forms)
expandtab
or noexpandtab
depending on whether it is switched on or offset {option}
get the current value of number or string option
SearchSee :h options.txt for a complete list of usage guidelines and available options.
/
for searching, n will move in the forward direction?
for searching, n will move in the backward directionBy default, the cursor is placed at the starting character of the match. There are various options to place the cursor at other locations:
b
(begin) instead of s
, but it'll change to s
after the command is executed2
characters after the start of the match (i.e. the third character of the match)2
characters before the start of the match4
characters after the end of the match4
characters before the end of the match3
lines below the match3
lines above the matchHighlight settings:
hlsearch
settings:noh
is short for :nohlsearch
)Using an empty pattern will repeat the last searched pattern. So, you can use something like //s+3 to repeat the last search with this new offset. Empty pattern can be used with substitution command as well (discussed later in this chapter). See :h last-pattern for more details.
You can prefix a count value to the
/
,?
,n
andN
commands. Also, searching will automatically wrap around when it reaches the top or bottom of the file contents, unless you set thenowrapscan
option.
RangeCharacters like
.
,^
,$
, etc have special meaning in thesearchpattern
. These will be discussed in detail in the Regular Expressions chapter.
By default, certain commands like :d
and :s
apply to the current line whereas commands like :w
and :perldo
apply to the entire file. You can prefix a range to change the lines that are acted upon.
:d
is short for the :delete
command).
) to the given filename
:w
works on the entire file$
):m
is short for the :move
command)
m
is the line number after which you want to place the lines specified by the range:t
(or :co
or :copy
) command if you want to copy instead of moving%
is a shortcut for the 1,$
range)/
is used)
?pattern?
to find a match in the backward directionpat1
as well as the line after (total 2
lines)
;
will set the line matched by the first pair of the range as the current line for the second pairpat1
as well as two lines before (total 3
lines)pat1
after the fifth line
;
again here, the search will be based on the current cursor line if you use ,
instead of ;
a
to the line marked by b
If you press : after a visual selection, you'll automatically get
:'<,'>
as the visual range. If you prefix a number before pressing :, you'll get a range with that many lines — for example 10: will give you:.,.+9
as the range.
See :h 10.3 and :h cmdline-ranges for more details.
Search and ReplaceSee :h ex-cmd-index for a complete list of
:
commands.
:[range]s[ubstitute]/{pattern}/{string}/[flags] [count]
General syntax for the s
command (short for substitute
) is shown above. Space between the range
and s
is optional, which you can use for readability.
a
with b
on the current line only
.
represents the current line)apple
with Mango
on the second line only
i
flag matches the searchpattern
case insensitivelycall
with jump
on lines 3
to 6
g
flag performs search and replace for all the matching occurrencescall
with jump
from the fifth line to the end of the filecall
in the file with jump
%
is a shortcut for the range 1,$
You can leave the
searchpattern
as empty to reuse the previously searched pattern, which could be from/
,?
,*
,s
command, etc. See :h last-pattern for more details.
Editing lines filtered by a patternSee the Regular Expressions chapter for more details on the substitute command.
:[range]g[lobal]/{pattern}/[cmd]
General syntax for the g
command (short for global
) is shown above. This command is used to edit lines that are first filtered based on a searchpattern
. You can use g!
or v
to act on lines not satisfying the filtering condition.
call
d
Normal mode command, the deleted contents will be saved to the default "
register"a
register#
to the start of the filecall
only for the first five linesjump
animal
with mammal
only for the lines containing cat
20
lines only if the line starts with #
normal
when you need to use Normal mode commands on the filtered linesnormal!
if you don't want user defined mappings to be consideredIn addition to the
/
delimiter, you can also use any single byte character other than alphabets,\
,"
or|
.
Shell commandsSee :h :g for more details.
You can also use shell commands from within Vim (assuming you have access to these commands).
!
waits for motion similar to the d
and y
commands, !G will give :.,$!%
is a shortcut for the range 1,$
:sort
command3
to 8
!
is not used here since there is no shell commandHelp
in the current file
%
here refers to the current file contentsexit
command to quit the sessionTerminal mode
splitbelow
option is setsplitright
option is setN
here)
Depending on your shell, you can use the
exit
command to end the terminal session.Ctrl+d
might work too.
Line number settingsThere are lot of features in this mode, see :h terminal.txt for more details.
0
, lines above and below the current line are assigned 1
, two lines above and below are assigned 2
and so onSessionsSee :h 5.9 for user manual about often used options.
sessionoptions
settingproj.vim
file
vim -S proj.vim
restore a session from the command line when launching VimSee :h 21.4, :h views-sessions and :h 'sessionoptions' for more details.
ViminfoSee stackoverflow: How to save and restore multiple different sessions in Vim? for custom settings to automate the save and restore process and other tips and tricks. See also Learn-Vim: Views, Sessions, and Viminfo.
From :h 21.3:
After editing for a while you will have text in registers, marks in various files, a command line history filled with carefully crafted commands. When you exit Vim all of this is lost. But you can get it back! The
viminfo
file is designed to store status information:
- Command-line and Search pattern history
- Text in registers
- Marks for various files
- The buffer list
- Global variables
Each time you exit Vim it will store this information in a file, the
viminfo
file. When Vim starts again, theviminfo
file is read and the information restored.
The :mksession
command doesn't save the viminfo
file. You'll have to save and restore this file separately:
!
isn't used, you'll get a merged output based on the current internal Viminfo contents and the file contentsproj.viminfo
file
!
overwrites any existing internal settingsvim -i proj.viminfo
restore Viminfo from the command line when launching VimMotion, editing and completion commandsSee :h viminfo-read-write for more details.
Once you are in Command-line mode (after typing :
or /
or ?
), you can use the commands discussed below. Many of these commands are similar to those available in the Insert mode.
"a
register1024
2022/02/02
wildmode
settingcpoptions
settingCommand-line historySee :h usr_20.txt for a nice tutorial on working effectively in the Command-line mode. See :h cmdline-editing and :h cmdline-completion for more details.
There are separate history lists for :
commands, searchpattern
, etc. These lists make it easy to reuse (after modifications if necessary) previously executed commands.
Command-line windowSee :h cmdline-history for more details. You can change the number of entries that are remembered using the
history
setting.
You can also view, edit and execute the history of commands using a special Command-line window. You can open this special window from Normal mode as well as Command-line mode. This window will be in Normal mode by default, which will allow you to move around easily. You can also edit any of the history commands if you wish.
:
commands (from Normal mode):
or search pattern windows automatically, any text you've typed so far will be preserved as the most recent commandSee :h cmdline-window for more details. You can change the number of entries that are remembered using the
history
setting.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4