We can do actions on a selection, just like any other ranges. While we have text
selected in visual mode, with :
we can enter command mode and the prompt will
be pre filled with '<,'>
.
'<
: Start of selection.'>
: End of selection.
It works like any other ranges like 12,15
which means from line 12 to line 15.
Save to a new file
We can save the selected content into a new file with w
, the command will look
like: '<,'>w newfilename
.
Pipe to a shell command
We can do some fancy magic with '<,'>!cmd
. The output of the command will
replace the selected content. A few examples:
'<,'>!sort
: sort selected lines with thesort
command.'<,'>!sort | uniq'
: Sort and remove duplicated lines.'<,'>!pandoc -f markdown -t html'
: Convert markdown to HTML.'<,'>!python3'
: Evaluate withpython3
and replace with the output.