(Move categories to tip template) |
JohnBeckett (talk | contribs) (Expand + tweak) |
||
Line 1: | Line 1: | ||
− | {{review}} |
||
{{TipImported |
{{TipImported |
||
|id=374 |
|id=374 |
||
Line 14: | Line 13: | ||
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 Vim's filtering commands, showing how to get text into or out of a file. |
||
− | Redirection to |
+ | Redirection to clipboard register <tt>+</tt> (or use any other register a-z): |
− | |||
<pre> |
<pre> |
||
− | :redir @ |
+ | :redir @+ |
:history |
:history |
||
:g/fred/ |
:g/fred/ |
||
⚫ | |||
− | etc |
||
:redir END |
:redir END |
||
</pre> |
</pre> |
||
− | Redirection to a file |
+ | Redirection to a file: |
− | |||
<pre> |
<pre> |
||
− | :redir |
+ | :redir >> out.txt |
+ | :registers |
||
⚫ | |||
+ | " any other commands |
||
:redir END |
:redir END |
||
</pre> |
</pre> |
||
− | + | Store glob results in register <tt>a</tt> (must use <tt>A</tt> to append): |
|
− | |||
<pre> |
<pre> |
||
" 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 >>). |
|
− | :'a,'b g/^Error/ . w |
+ | :'a,'b g/^Error/ . w >> errors.txt |
</pre> |
</pre> |
||
− | Get output from external commands |
+ | Get output from external commands: |
+ | <pre> |
||
⚫ | |||
+ | :0r!ls " insert at start of buffer |
||
+ | :-r!ls " insert before current line |
||
+ | :r!dir " use 'dir' on Windows |
||
+ | </pre> |
||
+ | Filter current file using an external command (these examples use <tt>sort</tt>, but note that Vim has a built-in {{help|:sort}} command which should be used to sort lines): |
||
<pre> |
<pre> |
||
⚫ | |||
⚫ | |||
+ | :'a,'b!sort -u " same, for lines from mark a to mark b inclusive |
||
− | !!date : same thing |
||
</pre> |
</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: |
||
− | Filtering current file using an external command. |
||
+ | <pre> |
||
+ | !}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 |
||
+ | </pre> |
||
+ | ==Simple filter example== |
||
+ | Following is a Python program to sort the words on each line of standard input (each line is separately sorted). |
||
<pre> |
<pre> |
||
+ | # File sortwords.py |
||
⚫ | |||
+ | from sys import stdin |
||
− | :'a,'b!sort -u : use an external program to sort current file |
||
+ | for line in stdin: |
||
+ | print ' '.join(sorted(line.split())) |
||
</pre> |
</pre> |
||
+ | A file you are editing in Vim may include the following text: |
||
− | These tips are "filtered" from [[VimTip305]]. |
||
+ | <pre> |
||
+ | this is a line with some words |
||
+ | words on each line will be sorted |
||
+ | fried banana and cream |
||
+ | </pre> |
||
+ | Use this procedure to filter the text: |
||
⚫ | |||
+ | *Press <tt>V</tt> on the first line, then <tt>jj</tt> to select three lines. |
||
− | To sort the actual paragraph at cursor position from normal mode with external sort program (-u deletes multiple lines): |
||
+ | *Type <tt>!python sortwords.py</tt> and press Enter. |
||
− | !1} sort -u |
||
+ | 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|:!}} |
||
+ | |||
⚫ |
Revision as of 03:53, 7 June 2008
created November 24, 2002 · complexity basic · author zzapper · version 6.0
Here are some examples of Vim's filtering commands, showing how to get text into or out of a file.
Redirection to clipboard register + (or use any other register a-z):
:redir @+ :history :g/fred/ " any other commands :redir END
Redirection to a file:
:redir >> out.txt :registers " any other commands :redir END
Store glob results in register a (must use A to append):
" 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
Get output from external commands:
:r!ls " read in output of ls (run 'ls' on Unix) :0r!ls " insert at start of buffer :-r!ls " insert before current line :r!dir " use 'dir' on Windows
Filter current file using an external command (these examples use sort, but note that 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