How to pipe colored output to vim and show it correctly?

591 views
Skip to first unread message

Peng Yu

unread,
Nov 23, 2013, 12:07:12 PM11/23/13
to vim_use
Hi

I have the following alias which works fine when uncolored input.

~/linux/test/gnu/ack-grep/--color$ alias less
alias less='vim -R -'

~/linux/test/gnu/ack-grep/--color$ cat.sh *.txt
==> test1.txt <==
ABC

==> test2.txt <==
AXY
~/linux/test/gnu/ack-grep/--color$ printf "%s\n" *.txt|ack --color -x A
test1.txt
1:ABC

test2.txt
1:AXY

But when the input is colored,
~/linux/test/gnu/ack-grep/--color$ printf "%s\n" *.txt|ack --color -x A|less

I see the following in vim. Does anybody know how to make the correct
alias of less that it works for both colored cases and uncolored
cases? Thanks.

1 ^[[1;32mtest1.txt^[[0m:^[[1;33m1^[[0m:^[[30;43mA^[[0mBC^[[0m^[[K
2 ^[[1;32mtest2.txt^[[0m:^[[1;33m1^[[0m:^[[30;43mA^[[0mXY^[[0m^[[K

--
Regards,
Peng

Gabriele Lana

unread,
Nov 23, 2013, 12:29:10 PM11/23/13
to vim...@googlegroups.com
On Nov 23, 2013, at 6:07 PM, Peng Yu <peng...@gmail.com> wrote:

> Hi
>
> I have the following alias which works fine when uncolored input.
>
> ~/linux/test/gnu/ack-grep/--color$ alias less
> alias less='vim -R -'
>

> […]
>
> 1 ^[[1;32mtest1.txt^[[0m:^[[1;33m1^[[0m:^[[30;43mA^[[0mBC^[[0m^[[K
> 2 ^[[1;32mtest2.txt^[[0m:^[[1;33m1^[[0m:^[[30;43mA^[[0mXY^[[0m^[[K


I'm unable to reproduce your problem, seems like whenever I pipe something in my shell the ansi escapes are stripped… maybe one of the zsh magic tricks? I don't know

Anyway, something like

alias less='sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | vim -R -'

should work (not tested)

--
Gabriele Lana
contact me at gabrielelana at cleancode dot it
http://www.cleancode.it - what we do and what we believe in
http://www.gabrielelana.it - agile methodologies and programming

Peng Yu

unread,
Nov 23, 2013, 12:43:10 PM11/23/13
to vim_use
Something like 'grep --color' (although it produces colored output)
won't cause the problem. You have to use ack. Did you use ack?

~$ ack --version
ack 2.02
Running under Perl 5.14.2 at /usr/local/ActivePerl-5.14/bin/perl

Copyright 2005-2013 Andy Lester.

This program is free software. You may modify or distribute it
under the terms of the Artistic License v2.0.
> --
> --
> 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
>
> ---
> You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/Ba1SfjC7J-M/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.



--
Regards,
Peng

Gabriele Lana

unread,
Nov 23, 2013, 1:02:12 PM11/23/13
to vim...@googlegroups.com

On Nov 23, 2013, at 6:43 PM, Peng Yu <peng...@gmail.com> wrote:

> Something like 'grep --color' (although it produces colored output)
> won't cause the problem. You have to use ack. Did you use ack?


You are right sorry, now I'm able to confirm that

alias less='sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g" | vim -R -'

Works like a charm :-)

Nikolay Pavlov

unread,
Nov 23, 2013, 1:02:21 PM11/23/13
to vim...@googlegroups.com


On Nov 23, 2013 9:43 PM, "Peng Yu" <peng...@gmail.com> wrote:
>
> Something like 'grep --color' (although it produces colored output)
> won't cause the problem. You have to use ack. Did you use ack?

It will. Just make sure you use 'grep --color=always'. By default 'grep --color=auto' is used which will not cause problems.

> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.

Nikolay Pavlov

unread,
Nov 23, 2013, 1:07:09 PM11/23/13
to vim...@googlegroups.com

There is :AnsiEsc command provided by vimscript #302. It will make this output colored in vim, hiding (assuming vim was compiled with +conceal) or removing this sequences. It will disable other highlighting though if you use any with this buffer.

Reply all
Reply to author
Forward
0 new messages