Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Change <tt> to <code>, perhaps also minor tweak.)
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=374
 
|id=374
|previous=373
+
|previous=371
 
|next=375
 
|next=375
|created=November 24, 2002
+
|created=2002
 
|complexity=basic
 
|complexity=basic
 
|author=zzapper
 
|author=zzapper
 
|version=6.0
 
|version=6.0
 
|rating=32/18
 
|rating=32/18
  +
|category1=
  +
|category2=
 
}}
 
}}
Here are some examples of Vim's filtering commands, showing how to get text into or out of a file.
+
Here are some examples of filtering which show how to save output from Vim commands, or read text from a file, or send text to an external program while capturing the output from that program.
   
  +
==Capturing output==
Redirection to Paste register * (or use any other register a-z).
 
  +
Redirection can capture the output generated by Vim commands.
   
 
Redirection to clipboard register <code>+</code> (or use any other register a-z):
 
<pre>
 
<pre>
:redir @*
+
:redir @+
 
:history
 
:history
 
:g/fred/
 
:g/fred/
 
" any other commands
etc
 
 
:redir END
 
:redir END
 
</pre>
 
</pre>
   
  +
The output from the commands used is now in register <code>+</code> which might pasted into a new buffer, for example by entering <code>:new</code> then typing <code>"+p</code> to paste.
Redirection to a file.
 
   
  +
If wanted, you can temporarily turn off Vim's paging so there will be no "More" prompts, as shown in the following example which outputs to register <code>A</code> (so output is appended to register <code>a</code>):
 
<pre>
 
<pre>
:redir &gt;&gt; out.txt
+
:redir @A
  +
:set nomore
any commands
 
  +
:echo 'History'
  +
:history
  +
:echo 'Scripts loaded'
  +
:scriptnames
  +
:set nomore
 
:redir END
 
:redir END
 
</pre>
 
</pre>
   
 
Redirection to a file:
Storing glob results in register a (must use ''append'').
 
  +
<pre>
  +
:redir > out.txt
  +
:registers
  +
" any other commands
  +
:redir END
  +
</pre>
  +
  +
The above writes to the new file <code>out.txt</code>. The command does nothing if that file already exists. To overwrite the file if it exists, use <code>:redir! > out.txt</code>. To create a new file or append to an existing file, use <code>:redir >> out.txt</code>.
   
 
Store glob results in register <code>a</code>:
 
<pre>
 
<pre>
  +
" Clear @a (register a) because need to use A to append.
  +
:let @a = ''
 
" Append all lines containing 'fred' to register a.
 
" Append all lines containing 'fred' to register a.
 
:g/fred/y A
 
:g/fred/y A
# Append to a file (must use &gt;&gt;)
+
" Append to a file (must use >>).
:'a,'b g/^Error/ . w &gt;&gt; errors.txt
+
:'a,'b g/^Error/ .w >> errors.txt
 
</pre>
 
</pre>
   
  +
The last command uses the <code>:.w</code> command which writes the current line (<code>.</code>) by appending it to file <code>errors.txt</code>.
Get output from external commands.
 
   
  +
==Processing input==
 
Get output from external commands:
 
<pre>
 
<pre>
:r!ls.exe : reads in output of ls
+
:r !ls " read output from running ls, after current line
  +
:0r !ls " after line 0 (before first line)
!!date : same thing
 
  +
:-r !ls " before current line ("-" is ".-1")
  +
:r !dir " use 'dir' on Windows
 
</pre>
 
</pre>
   
Filtering current file using an external command.
+
Filter current file using an external command (these examples use <code>sort</code>, but Vim has a built-in {{help|:sort}} command which should be used to sort lines):
  +
<pre>
  +
:%!sort -u " use an external program to sort all lines
  +
:'a,'b!sort -u " same, for lines from mark a to mark b inclusive
  +
</pre>
   
  +
The term "filter" means to replace lines with the result from running a program. The original lines are sent as stdin to the program, and are replaced with stdout from the program. You can also filter using motion commands or visual selection:
 
<pre>
 
<pre>
:%!sort -u : use an external program to sort current file
+
!}sort " sort from cursor to end of paragraph
:'a,'b!sort -u : use an external program to sort current file
+
3!}sort " same, 3 paragraphs
  +
3!!sort " sort 3 lines
  +
V " start visual selection of lines
  +
(move cursor) " select some lines
  +
!sort " sort the visually selected lines
 
</pre>
 
</pre>
   
  +
==Simple filter example==
These tips are "filtered" from [[VimTip305]].
 
  +
Following is a Python program to sort the words on each line of standard input (each line is separately sorted).
  +
<pre>
  +
# File sortwords.py
  +
from sys import stdin
  +
for line in stdin:
  +
print ' '.join(sorted(line.split()))
  +
</pre>
   
  +
A file you are editing in Vim may include the following text:
==Comments==
 
  +
<pre>
To sort the actual paragraph at cursor position from normal mode with external sort program (-u deletes multiple lines):
 
  +
this is a line with some words
!1} sort -u
 
  +
words on each line will be sorted
  +
fried banana and cream
  +
</pre>
   
  +
Use this procedure to filter the text:
----
 
  +
*Press <code>V</code> on the first line, then <code>jj</code> to select three lines.
  +
*Type <code>!python sortwords.py</code> and press Enter.
  +
The lines are replaced with the result from running the program:
  +
<pre>
  +
a is line some this with words
  +
be each line on sorted will words
  +
and banana cream fried
  +
</pre>
  +
  +
==References==
  +
*{{help|filter}}
  +
*{{help|:!}}
  +
 
==Comments==
  +
The section about redirecting Vim commands has nothing to do at all with using filters, and neither does the stuff about :r !cmd. That plus the actual stuff about filters seems redundant with the content in [[Append_output_of_an_external_command]]. --[[User:Fritzophrenic|Fritzophrenic]] 14:25, July 2, 2012 (UTC)

Revision as of 12:31, 15 July 2012

Tip 374 Printable Monobook Previous Next

created 2002 · complexity basic · author zzapper · version 6.0


Here are some examples of filtering which show how to save output from Vim commands, or read text from a file, or send text to an external program while capturing the output from that program.

Capturing output

Redirection can capture the output generated by Vim commands.

Redirection to clipboard register + (or use any other register a-z):

:redir @+
:history
:g/fred/
  " any other commands
:redir END

The output from the commands used is now in register + which might pasted into a new buffer, for example by entering :new then typing "+p to paste.

If wanted, you can temporarily turn off Vim's paging so there will be no "More" prompts, as shown in the following example which outputs to register A (so output is appended to register a):

:redir @A
:set nomore
:echo 'History'
:history
:echo 'Scripts loaded'
:scriptnames
:set nomore
:redir END

Redirection to a file:

:redir > out.txt
:registers
  " any other commands
:redir END

The above writes to the new file out.txt. The command does nothing if that file already exists. To overwrite the file if it exists, use :redir! > out.txt. To create a new file or append to an existing file, use :redir >> out.txt.

Store glob results in register a:

" Clear @a (register a) because need to use A to append.
:let @a = ''
" Append all lines containing 'fred' to register a.
:g/fred/y A
" Append to a file (must use >>).
:'a,'b g/^Error/ .w >> errors.txt

The last command uses the :.w command which writes the current line (.) by appending it to file errors.txt.

Processing input

Get output from external commands:

:r !ls     " read output from running ls, after current line
:0r !ls    " after line 0 (before first line)
:-r !ls    " before current line ("-" is ".-1")
:r !dir    " use 'dir' on Windows

Filter current file using an external command (these examples use sort, but Vim has a built-in :help :sort command which should be used to sort lines):

:%!sort -u      " use an external program to sort all lines
:'a,'b!sort -u  " same, for lines from mark a to mark b inclusive

The term "filter" means to replace lines with the result from running a program. The original lines are sent as stdin to the program, and are replaced with stdout from the program. You can also filter using motion commands or visual selection:

!}sort          " sort from cursor to end of paragraph
3!}sort         " same, 3 paragraphs
3!!sort         " sort 3 lines
V               " start visual selection of lines
(move cursor)   " select some lines
!sort           " sort the visually selected lines

Simple filter example

Following is a Python program to sort the words on each line of standard input (each line is separately sorted).

# File sortwords.py
from sys import stdin
for line in stdin:
    print ' '.join(sorted(line.split()))

A file you are editing in Vim may include the following text:

this is a line with some words
words on each line will be sorted
fried banana and cream

Use this procedure to filter the text:

  • Press V on the first line, then jj to select three lines.
  • Type !python sortwords.py and press Enter.

The lines are replaced with the result from running the program:

a is line some this with words
be each line on sorted will words
and banana cream fried

References

Comments

The section about redirecting Vim commands has nothing to do at all with using filters, and neither does the stuff about :r !cmd. That plus the actual stuff about filters seems redundant with the content in Append_output_of_an_external_command. --Fritzophrenic 14:25, July 2, 2012 (UTC)