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: