Vim Tips Wiki
(Move categories to tip template)
Tags: Visual edit apiedit
(21 intermediate revisions by 12 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=767
 
|id=767
 
|previous=766
 
|previous=766
 
|next=768
 
|next=768
|created=August 11, 2004
+
|created=2004
 
|complexity=basic
 
|complexity=basic
 
|author=pim
 
|author=pim
 
|version=6.0
 
|version=6.0
 
|rating=10/9
 
|rating=10/9
|category1=
+
|category1=Getting started
 
|category2=
 
|category2=
 
}}
 
}}
You can easily execute more then one command by placing a | between two commands. For example:
+
You can execute more than one command by placing a <code>|</code> between two commands.
   
  +
For example:
  +
<pre>
  +
%s/htm/html/c | %s/JPEG/jpg/c | %s/GIF/gif/c
  +
</pre>
  +
  +
This example substitutes for htm, then moves on to JPEG, then GIF.
  +
  +
The second command (and subsequent commands) are only executed if the prior command succeeds.
  +
  +
This works for ''most'' commands, but some commands like <code>:argdo</code> or <code>:autocmd</code> see the '|' as one of their arguments. This allows commands such as <code>:argdo</code>, which execute a different Vim command, to execute a series of commands. See {{help|:\bar}} for the full list of such commands.
  +
  +
For example:
 
<pre>
 
<pre>
 
argdo %s/foo/bar/gc | w
 
argdo %s/foo/bar/gc | w
 
</pre>
 
</pre>
   
  +
Normally, Vim will complain if you haven't saved changes to a buffer before abandoning it, but by adding <code>| w</code> in the above example, you can actually write out each buffer after processing. Note that this command will, for each item in the arg list, do a <code>:substitute</code> ''and'' a <code>:write</code>. If <code>:argdo</code> didn't take '|' as an argument, it would instead run the <code>:substitute</code> on each item, then write the last item.
By adding <tt>| w</tt> in the above example the multiple find and replace command doesn't get interupted because the last edited file isn't saved.
 
  +
  +
If you want to make a command chain including one of the commands listed at {{help|:\bar}}, you can still do so using the <code>:execute</code> command. For example, in order to chain a <code>:normal</code> command, you would need to do something like this:
  +
  +
<pre>
  +
:execute 'normal "ayiw' | echo @a
  +
</pre>
  +
  +
If you want to chain commands form the vimrc file, then you need to user <bar> instead of | like this:
  +
map <F6> <ESC>:echo "test" <bar> :echo "test2"
  +
  +
==References==
  +
* {{help|:\bar}}
  +
* {{help|:execute}}
   
 
==Comments==
 
==Comments==
  +
what about the range? how can you specify one range, and then have a list of commands executed on that one range?
  +
:Normally, you'd pass the range to each command, like the "%" in the first example. Theoretically, you could put the commands in a function, then :call that function, and it would be invoked once for each line of the range, but I doubt that's what you're after. You could use my {{script|id=3270|text=CommandWithMutableRange plugin}}, though: <code>:[range]ExecuteWithMutableRange command1 | command2 | command3</code> -- [[User:Inkarkat|Inkarkat]] 10:12, October 9, 2010 (UTC)
   
  +
How do I do multiple commands if the previous one failed?
----
 
  +
:"The 'e' flag tells ":substitute" that not finding a match is not an error." &mdash;<code>usr_12.txt</code>, example: <code>%s/from1/to1/ge | %s/from2/to2/ge | %s/from3/to3/ge</code> --[[User:User000name|User000name]] ([[User talk:User000name|talk]]) 05:10, January 13, 2016 (UTC)
  +
::This will work for the <code>:s</code> command. For other commands, see below. [[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 16:08, January 13, 2016 (UTC)
  +
:Try using <code>silent!</code> to suppress errors. Example: <code>:exe "norm! yaw" | exe "silent! norm! b" | exe "norm! P"</code> --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]])

Revision as of 21:29, 21 January 2016

Tip 767 Printable Monobook Previous Next

created 2004 · complexity basic · author pim · version 6.0


You can execute more than one command by placing a | between two commands.

For example:

%s/htm/html/c | %s/JPEG/jpg/c | %s/GIF/gif/c

This example substitutes for htm, then moves on to JPEG, then GIF.

The second command (and subsequent commands) are only executed if the prior command succeeds.

This works for most commands, but some commands like :argdo or :autocmd see the '|' as one of their arguments. This allows commands such as :argdo, which execute a different Vim command, to execute a series of commands. See :help :\bar for the full list of such commands.

For example:

argdo %s/foo/bar/gc | w

Normally, Vim will complain if you haven't saved changes to a buffer before abandoning it, but by adding | w in the above example, you can actually write out each buffer after processing. Note that this command will, for each item in the arg list, do a :substitute and a :write. If :argdo didn't take '|' as an argument, it would instead run the :substitute on each item, then write the last item.

If you want to make a command chain including one of the commands listed at :help :\bar, you can still do so using the :execute command. For example, in order to chain a :normal command, you would need to do something like this:

:execute 'normal "ayiw' | echo @a

If you want to chain commands form the vimrc file, then you need to user <bar> instead of | like this:

map <F6> <ESC>:echo "test" <bar> :echo "test2"

References

Comments

what about the range? how can you specify one range, and then have a list of commands executed on that one range?

Normally, you'd pass the range to each command, like the "%" in the first example. Theoretically, you could put the commands in a function, then :call that function, and it would be invoked once for each line of the range, but I doubt that's what you're after. You could use my CommandWithMutableRange plugin, though: :[range]ExecuteWithMutableRange command1 | command2 | command3 -- Inkarkat 10:12, October 9, 2010 (UTC)

How do I do multiple commands if the previous one failed?

"The 'e' flag tells ":substitute" that not finding a match is not an error." —usr_12.txt, example: %s/from1/to1/ge | %s/from2/to2/ge | %s/from3/to3/ge --User000name (talk) 05:10, January 13, 2016 (UTC)
This will work for the :s command. For other commands, see below. Fritzophrenic (talk) 16:08, January 13, 2016 (UTC)
Try using silent! to suppress errors. Example: :exe "norm! yaw" | exe "silent! norm! b" | exe "norm! P" --Fritzophrenic (talk)