zhuang@linux:~/reading/practical-vim/05-command-line-mode/$ less

Practical Vim / chapter 05

Command-Line Mode

$ grep tags 05-command-line-mode.md

This post extracts some knowledge from Practical Vim Chapter 5 – Command-Line Mode.

CommandEffect
:[range]delete [x]Delete specified lines [into register x]
:[range]yank [x]Yank specified lines [into register x]
:[line]put [x]Put the text from register x after the specified line
:[range]copy {address}Copy the specified lines to below the line specified by {address}
:[range]move {address}Move the specified lines to below the line specified by {address}
:[range]joinJoin the specified lines
:[range]normal {commands}Execute Normal mode {commands} on each specified line
:[range]substitute/{pattern}/{string}/[flags]Replace occurrences of {pattern} with {string} on each specified line
:[range]global/{pattern}/[cmd]Execute the Ex command [cmd] on all specified lines where the {pattern} matches
SymbolAddress
1First line of the file
$Last line of the file
0Virtual line above first line of the file
.Line where the cursor is placed
'mLine containing mark m
'<Start of visual selection
'>End of visual selection
%The entire file (shorthand for :1,$)

we could use an offset relative to the current line:

vim
:2
:.,,+3p

The . symbol stands for the current line, so :.,.+3 is equivalent to :2,5 in this case.

:copy = :co = :t

As a mnemonic, you can think of it as copy TO.

vim
:[range]normal [normal-action]

Use @: to repeat the last Ex command. After running @: for the first time, we can subsequently repeat it with the @@ command.

Just like in the shell, we can use the <Tab> key to autocomplete commands at the prompt. The <C-d> command asks Vim to reveal a list of possible completions.

At Vim’s command line, the <C-r><C-w> mapping copies the word under the cursor and inserts it at the command-line prompt.

let’s switch to Command-Line mode by pressing the : key. Leave the prompt empty; then press the <Up> arrow key.

If we press / to bring up the search prompt, we can also scroll backward and forward through previous searches with the <Up> and <Down> keys. The search prompt is, after all, just another form of Command-Line mode.

Press q: and meet the command-line window. The command-line window is like a regular Vim buffer, where each line contains an item from our history.

The beauty of the command-line window is that it allows us to change historical commands using the full modal editing power of Vim. Awesome!!!

When the command-line window is open, it always gets the focus.

Note that when we press <CR> in the command-line window, the command is executed in the context of the active window: that is, the window that was active before the command-line window was summoned. Vim doesn’t indicate which is the active window when the command-line window is open, so pay attention if you’re using split windows!

In Command-Line mode, we can use the <C-f> mapping to switch to the command-line window, preserving a copy of the command that was typed at the prompt.

This table summarizes a few of the methods for summoning the command-line window:

CommandAction
q/Open the command-line window with history of search
q:Open the command-line window with history of Ex commands
Ctrl+fSwitch from Command-Line mode to the command-line window

From Vim’s Command-Line mode, we can invoke external programs in the shell by prefixing them with a bang symbol.

In that case, we can use Vim’s :shell command to start an interactive shell session.

The Ctrl + z and fg commands are quicker and easier to use than Vim’s equivalent :shell and exit commands.

We can use the :read !{cmd} command, which puts the output from the {cmd} into our current buffer.

The bang symbol can take on different meanings depending on where it is placed within the command line. Compare these commands:

vim
:write !sh
:write ! sh
:write! sh

The first two commands pass the contents of the buffer as standard input to the external sh command. The last command writes the contents of the buffer to a file called sh by calling the :write! command.

This table summarizes a selection of the most useful methods for calling external commands:

CommandEffect
:shellStart a shell (return to Vim by typing exit)
:!{cmd}Execute {cmd} with the shell
:read !{cmd}Execute {cmd} in the shell and insert its standard output below the cursor
:[range]write !{cmd}Execute {cmd} in the shell with [range] lines as standard input
:[range]!{filter}Filter the specified [range] through external program {filter}

Write Ex Commands to a Script and Source It.

shell
vim vimcasts/episodes-1.html
vim
:source batch.vim

Source the Script to Change Multiple Files.

We could step through those files one by one, sourcing our batch.vim for each one:

shell
vim vimcasts/*.html
vim
:args
[vimcasts/episodes-1.html] vimcasts/episodes-2.html vimcasts/episodes-3.html
:first
:source batch.vim
:next
:source batch.vim
vim
:argdo source batch.vim

With a single command we’ve executed each of the Ex commands in batch.vim across each of the files in the argument list.

zhuang@linux:~/reading/practical-vim/05-command-line-mode/$ comments