Vim - Command Line
Ex commands strike far and wide
Using the :
key in vim activates command mode. This allows us to enter commands that were originally built for the “ex” editor. Most operations performed in Normal mode have an analaogue in the command line.
Simple example:
:print
or :p
prints current line
:delete
or :d
deletes current line
:substitute
or :s
is used for find/replace. Skipping the “find” pattern applies it to selections made using a previous “find”, such as using “*”
Specifying ranges
Move to line using :{number}
This can be combiend with commands as :1p
. A range of lines is specified as :{start}{end}
. $
denotes the last line and %
denotes all lines and is equivalent to 1,$
.
:%p
prints all lines
:%s/find/replace
does a find/replace on the entire file
Visual selection for ranges
{number}G
goes to the numbered line. Visual mode can also be used for selections that apply to commands. When text is selected, the :
key shows the range in command line as :'<,'>
. This applies the command to the selection. '<
stands for the first line of visual selection and '>
stands for the last line of visual selection.
Patterns as ranges
/<html>/,/<\/html>/p
prints lines from to
Offsets
Use +{number}
or -{number}
with any address (including patterns) to offset up or down.
/<html>/+1,/<\/html>-1/p
prints lines from to , excluding the lines with the tags themselves.
Copy/Move
:copy{address}
or :co{address}
- works on current line by default. Can prefix address to copy from a different address. :t
(copy TO) is another synonym. :move
or :m
works in the same way.
Repeat last Ex command
@:
repeats the last Ex command.
Normal mode commands on ranges
:normal {command}
can be used to apply a normal mode command to a range (visual or otherwise).
Example:
'<,'>normal .
applies the last change to each line in the selected visual range.
:%normal A;
append a semi-colon to the end of each line in the file.
:normal
moves the cursor to the beginning of each line before executing the command.
Copy word to command line
When in command line mode (after pressing :
), pressing <C-r><C-w>
copies the word under the cursor to the command line
Command Line Window
q:
opens up the command line window. This allows us to edit the command line history using all vim commands as if it is a buffer.
Pass buffer contents to shell and back
:read !{cmd}
runs {cmd}
and inserts the stdout into the buffer. Example :read !ls
puts the file listing into the buffer
:write !sh
runs each line in the buffer in the shell as a command, passing them to stdin.
Pass range to command for filtering
:2,$!sort -t',' -k2
: Pass all lines starting at the second line to “stdin” of the sort command given and put the output back in the buffer in the same spot. This particular example sorts the given comma separated lines by the second field, while ignoring the first line inthe buffer (the header).
!{motion command}
pre-fills the range in the command for ease of use. Example !G
will pre-fill the range from current line to end of file and then we can type in the rest of the command to filter the data through.
Batch files
Commands can be stored in text fils and then executed using the :source {file}
command.
See Also
References
[1] Drew Neil, “Practical Vim”