Home Explore Blog CI



neovim

9th chunk of `runtime/doc/usr_10.txt`
d2e5b537085443b84c4ef9a79063c8dc50adf1db5e63a9fc0000000100000a9c
 "gUgU" is shortened to "gUU" and
"g~g~" to "g~~".  Example: >

				g~~
<	Some GIRLS have Fun    ---->   sOME girls HAVE fUN ~

==============================================================================
*10.9*	Using an external program

Vim has a very powerful set of commands, it can do anything.  But there may
still be something that an external command can do better or faster.
   The command "!{motion}{program}" takes a block of text and filters it
through an external program.  In other words, it runs the system command
represented by {program}, giving it the block of text represented by {motion}
as input.  The output of this command then replaces the selected block.
   Because this summarizes badly if you are unfamiliar with Unix filters, take
a look at an example.  The sort command sorts a file.  If you execute the
following command, the unsorted file input.txt will be sorted and written to
output.txt.  This works on both Unix and Windows. >

	sort <input.txt >output.txt

Now do the same thing in Vim.  You want to sort lines 1 through 5 of a file.
You start by putting the cursor on line 1.  Next you execute the following
command: >

	!5G

The "!" tells Vim that you are performing a filter operation.  The Vim editor
expects a motion command to follow, indicating which part of the file to
filter.  The "5G" command tells Vim to go to line 5, so it now knows that it
is to filter lines 1 (the current line) through 5.
   In anticipation of the filtering, the cursor drops to the bottom of the
screen and a ! prompt displays.  You can now type in the name of the filter
program, in this case "sort".  Therefore, your full command is as follows: >

	!5Gsort<Enter>

The result is that the sort program is run on the first 5 lines.  The output
of the program replaces these lines.

	line 55			      line 11
	line 33			      line 22
	line 11		-->	      line 33
	line 22			      line 44
	line 44			      line 55
	last line		      last line

The "!!" command filters the current line through a filter.  In Unix the "date"
command prints the current time and date.  "!!date<Enter>" replaces the current
line with the output of "date".  This is useful to add a timestamp to a file.

Note: There is a difference between "!cmd" (e.g. using it without any file
range) and "{range}!cmd".  While the former will simply execute the external
command and Vim will show the output, the latter will filter {range}lines
through the filter and replace that range by the result of the filter command.
See |:!| and |:range!| for details.

WHEN IT DOESN'T WORK

Starting a shell, sending it text and capturing the output requires that Vim
knows how the shell works exactly.  When you have problems with filtering,

Title: Vim: Using External Programs for Text Filtering
Summary
This section explains how to use external programs like 'sort' to filter text within Vim. The command '!{motion}{program}' is used to execute a system command on a block of text, with the output replacing the selected block. The example shows sorting lines 1 through 5 using '!5Gsort'. The '!!' command filters the current line through a filter, like inserting the date with '!!date'. It also notes the difference between executing a shell command and filtering a range of lines and a note on what to do if it doesn't work.