Sunday, March 27, 2011

Re: Filtering text through external commands

On 27/03/11 21:24, Matt Martini wrote:
> How do I set up a mapping to make the currently selected text (visual
> mode) get processed by an external command?
>
> I have a bunch of scripts that do text processing (mostly perl and bash)
> that I want to process the current buffer in vim.
>
> To process the whole file, I can do:
>
> map<leader>x :%! external_prog.pl<CR>
>
> But how do I set it up so that if there is something highlighted via
> visual mode then only that text gets processed.
>
> Is there a way to set this up so that the same mapping will process the
> highlighted text, or if nothing is selected process the whole file?
>
> Alternatively, can it be set up to process either the highlighted text,
> or the current line?
>
> Thanks,
> Matt
>
>

:nmap <Leader>x :%!foobar.pl<CR>
:vmap <Leader>x :!foobar.pl<CR>

Hitting : in Visual mode (as with the second of these mappings) inserts
:'<,'> on the command-line, thus passing as stdin to the ! command only
the lines included in the visual selection. (If the selection is
characterwise or blockwise Vim will still pass the *full* lines from top
to bottom of the selection, however.)

See
:help map-overview
:help v_:


Best regards,
Tony.
--
Rules:
(1) The boss is always right.
(2) When the boss is wrong, refer to rule 1.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

No comments:

Post a Comment