What are the possible operations (with keyboard shortcuts) that you could execute on a visually selected text? For the simplicity consider this simple piece:
a b cd 1 p
a b cd 2 y
a b cd 3 t
a b cd 4 h
a b cd 5 o
a b cd 6 n
One specific question: is it possible to execute Python/shell commands/scripts on selections?
A couple more specific questions:
1-) How to in-place sort the python column?
2-) How to switch place of columns?
3-) How to increment values? (say. only the numbers greater than 3 in selection)
Thanks.
To do this correctly, you need to use filters and vim’s execution mode.
A filter is any standard UNIX program (
sort,awk,grep,cut,paste,sed…) that can read from stdin and write to stdout.Vim’s execution mode lets you read in or replace text with the output of a program. If you make a visual selection (say, using
VorC-V), and hit:, your command prompt will immediately be:'<,'>, which means “Apply whatever command follows to the lines included in the visual selection.” At this point, you can write!footo replace the text with the output of program foo.For example, to sort the text by the python column, select it, hit
:, and enter!sort -k5. The whole command will look like:'<,'>!sort -k5Running it will produce:For the other two tasks,
awkis your friend. A command like:'<,'>!awk '{ print $1, $3, $2, $4, $5 }'is will flip the second and third columns (but note that inter-column spacing is collapsed). To increment columns, try something like:'<,'>!awk '{ sub($4, $4+1); print }'.