zhuang@linux:~/reading/practical-vim/05-command-line-mode/$ less
Command-Line Mode
This post extracts some knowledge from Practical Vim Chapter 5 – Command-Line Mode.
| Command | Effect |
|---|---|
:[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]join | Join 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 |
| Symbol | Address |
|---|---|
1 | First line of the file |
$ | Last line of the file |
0 | Virtual line above first line of the file |
. | Line where the cursor is placed |
'm | Line 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:
:2
:.,,+3pThe . 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.
:[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:
| Command | Action |
|---|---|
q/ | Open the command-line window with history of search |
q: | Open the command-line window with history of Ex commands |
Ctrl+f | Switch 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:
:write !sh
:write ! sh
:write! shThe 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:
| Command | Effect |
|---|---|
:shell | Start 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.
vim vimcasts/episodes-1.html:source batch.vimSource the Script to Change Multiple Files.
We could step through those files one by one, sourcing our batch.vim for each one:
vim vimcasts/*.html:args
[vimcasts/episodes-1.html] vimcasts/episodes-2.html vimcasts/episodes-3.html
:first
:source batch.vim
:next
:source batch.vim:argdo source batch.vimWith 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