Home Explore Blog CI



neovim

10th chunk of `runtime/doc/usr_10.txt`
3a9a6975c9a03edf71bdff0e6f27947b86fc66760a135fb30000000100000e86
 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,
check the values of these options:

	'shell'		specifies the program that Vim uses to execute
			external programs.
	'shellcmdflag'	argument to pass a command to the shell
	'shellquote'	quote to be used around the command
	'shellxquote'	quote to be used around the command and redirection
	'shellslash'	use forward slashes in the command (only for
			MS-Windows and alikes)
	'shellredir'	string used to write the command output into a file

On Unix this is hardly ever a problem, because there are two kinds of shells:
"sh" like and "csh" like.  Vim checks the 'shell' option and sets related
options automatically, depending on whether it sees "csh" somewhere in
'shell'.
   On MS-Windows, however, there are many different shells and you might have
to tune the options to make filtering work.  Check the help for the options
for more information.


READING COMMAND OUTPUT

To read the contents of the current directory into the file, use this:

on Unix: >
	:read !ls
on MS-Windows: >
	:read !dir

The output of the "ls" or "dir" command is captured and inserted in the text,
below the cursor.  This is similar to reading a file, except that the "!" is
used to tell Vim that a command follows.
   The command may have arguments.  And a range can be used to tell where Vim
should put the lines: >

	:0read !date -u

This inserts the current time and date in UTC format at the top of the file.
(Well, if you have a date command that accepts the "-u" argument.)  Note the
difference with using "!!date": that replaced a line, while ":read !date" will
insert a line.


WRITING TEXT TO A COMMAND

The Unix command "wc" counts words.  To count the words in the current file: >

	:write !wc

This is the same write command as before, but instead of a file name the "!"
character is used and the name of an external command.  The written text will
be passed to the specified command as its standard input.  The output could
look like this:

       4      47     249 ~

The "wc" command isn't verbose.  This means you have 4 lines, 47 words and 249
characters.

Watch out for this mistake: >

	:write! wc

This will write the file "wc" in the current directory, with force.  White
space is important here!


REDRAWING THE SCREEN

If the external command produced an error message, the display may have been
messed up.  Vim is very efficient and only redraws those parts of the screen
that it knows need redrawing.  But it can't know about what another program
has written.  To tell Vim to redraw the screen:
>
	CTRL-L
<
==============================================================================

Next chapter: |usr_11.txt|  Recovering from a crash

Copyright: see |manual-copyright|  vim:tw=78:ts=8:noet:ft=help:norl:

Title: Vim: Troubleshooting External Commands, Reading Output, Writing Text, and Redrawing the Screen
Summary
This section covers troubleshooting external commands in Vim, including options like 'shell', 'shellcmdflag', and 'shellredir'. It explains how to read command output into a file using ':read !command' (e.g., ':read !ls' to list directory contents) and how to write text to a command using ':write !command' (e.g., ':write !wc' to count words). Finally, it addresses screen redrawing issues after external commands and suggests using CTRL-L to force a redraw.