Vim Tips Wiki
(→‎Comments: Answered question about singe range to multiple commands.)
No edit summary
Line 44: Line 44:
 
what about the range? how can you specify one range, and then have a list of commands executed on that one range?
 
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: <tt>:[range]ExecuteWithMutableRange command1 | command2 | command3</tt> -- [[User:Inkarkat|Inkarkat]] 10:12, October 9, 2010 (UTC)
 
: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: <tt>:[range]ExecuteWithMutableRange command1 | command2 | command3</tt> -- [[User:Inkarkat|Inkarkat]] 10:12, October 9, 2010 (UTC)
  +
<div style="margin-left:-9999px;">[http://www.vipsexshop.com.br Sex Shop][http://www.requintesexshop.com.br Sex Shop][http://www.sexshop.com.vc Sex Shop][http://www.belasexshop.com.br Sex Shop][http://www.belasexshop.com Sex Shop][http://blog.vipsexshop.com.br Dicas de Sexo][http://www.viplingerie.com.br Lingerie][http://www.vipcalcinhas.com.br Calcinhas][http://www.uniformesbr.com Uniformes Profissionais][http://www.uniformesbr.com Uniformes]</div>

Revision as of 14:51, 8 November 2011

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

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)