A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://learnbyexample.github.io/vim_reference/Command-line-mode.html below:

Command-line mode - Vim Reference Guide

Command-line mode

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:

Add : or c_ 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.

Save changes

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.

By 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.

Quit Vim

Append ! to discard unsaved changes and quit.

Combining Save and Quit

Append ! to override errors. Not all errors can be skipped, for example unsaved changes on a file that hasn't been named yet.

Working with buffers and tabs

Multiple files can be opened in Vim within the same tab page and/or in different tabs. From :h windows-intro:

Buffers

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.

See :h 22.4 and :h buffer-hidden for user and reference manuals on working with buffer lists.

Tabs

Buffer list includes all the files opened in all the tabs.

You can also use the mouse to switch/move tabs in GVim.

Splitting

If filename is not provided, the current one is used.

Vim adds a highlighted horizontal bar containing the filename for each split.

Edit all buffers

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.

Further reading

Setting options

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:

Here are examples for each of these forms:

Usage guidelines:

See :h options.txt for a complete list of usage guidelines and available options.

Search

By default, the cursor is placed at the starting character of the match. There are various options to place the cursor at other locations:

Highlight settings:

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 and N commands. Also, searching will automatically wrap around when it reaches the top or bottom of the file contents, unless you set the nowrapscan option.

Characters like ., ^, $, etc have special meaning in the searchpattern. These will be discussed in detail in the Regular Expressions chapter.

Range

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.

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.

See :h ex-cmd-index for a complete list of : commands.

Search and Replace
:[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.

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.

See the Regular Expressions chapter for more details on the substitute command.

Editing lines filtered by a pattern
:[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.

In addition to the / delimiter, you can also use any single byte character other than alphabets, \, " or |.

See :h :g for more details.

Shell commands

You can also use shell commands from within Vim (assuming you have access to these commands).

See :h :!, :h :sh and :h :r for more details.

Terminal mode

Depending on your shell, you can use the exit command to end the terminal session. Ctrl+d might work too.

There are lot of features in this mode, see :h terminal.txt for more details.

Line number settings

See :h 5.9 for user manual about often used options.

Sessions

See :h 21.4, :h views-sessions and :h 'sessionoptions' for more details.

See 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.

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:

Each time you exit Vim it will store this information in a file, the viminfo file. When Vim starts again, the viminfo 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:

See :h viminfo-read-write for more details.

Motion, editing and completion commands

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.

See :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.

Command-line history

There are separate history lists for : commands, searchpattern, etc. These lists make it easy to reuse (after modifications if necessary) previously executed commands.

See :h cmdline-history for more details. You can change the number of entries that are remembered using the history setting.

Command-line window

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.

See :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