Sunday, March 27, 2011

Re: Filtering text through external commands

On 03/27/2011 02:24 PM, 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.

You can make a visual-mode mapping as well, something like

:vnoremap <leader>x :! external_prog.pl<cr>

The catch is that this is line-wise rather than character-wise or
block-wise. To do those, you'd have to investigate Dr. Chip's
vis.vim script[1] which takes the pain out of working with those
two sub-modes of visual-mode.

> 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?

Because they're mappings that occur in different modes, you'd
need both a normal-mode mapping and a visual-mode mapping.
However, you can create a custom command with ":command" that
would allow you to have a default range[2] so you could do
something like this (untested)

:command! -range=% E <line1>,<line2>!external_prog.pl

Which you can then use (including in mappings) as

:E

to do the whole file, or you can explicitly specify a range:

:'<,'>E

Hope that helps,

-tim

[1]
Stable:
http://www.vim.org/scripts/script.php?script_id=1195

Bleeding-edge:
http://mysite.verizon.net/astronaut/vim/index.html#VIS


[2]
:help :command-range

--
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