How can I keep the color when I run the external command inside vim?

12

2

For example, for the command ls --color displays colorful results when being executed in the shell. However, when I call :!ls --color inside vim, all color information is gone.

What should I do to keep the color when calling external command inside vim?

EDIT: To clarify my question, This is a question about VIM itself, I'm NOT actually asking how to enable "ls"'s color. What I want to know is, generally how to enable the display of colorful results of ALL shell commands(as long as they have colorful results).

CMinus

Posted 2012-07-16T19:59:11.700

Reputation: 223

2Works fine here. – Ignacio Vazquez-Abrams – 2012-07-16T20:01:22.057

For people who voted to close the question, would you please leave a comment on why you doing this? I read the FAQ and this question should be about "software tools commonly used by programmers". Maybe I misunderstood something, but I don't really think this question is off topic. – None – 2012-07-17T00:44:13.330

2"This is a question about VIM itself ..." – Ignacio Vazquez-Abrams – 2012-07-17T01:10:35.427

2and vim is one of the "software tools commonly used by programmers"... – None – 2012-07-17T01:18:25.483

1So's email. Doesn't necessarily mean "how do I Thunderbird" is on topic here. – Ignacio Vazquez-Abrams – 2012-07-17T02:14:09.313

2Vim has ctags, indent and much more direct support for programming. Email does what directly for programming? – Jens – 2012-07-17T10:02:50.673

Answers

5

CLI Vim or GUI Vim?

You can't show the output of :!ls or :!ls --color or any other external command in CLI Vim itself because it's single threaded: Vim is suspended and $ ls --color is executed in your shell. There's no way around that. Well, you can place the output of external commands in the current buffer but that's not the same thing.

In GUI Vim, you can only get a pseudo-shell that is totally unable to show any colors. There's no way around that either.

If you desperately need colored output from external commands you have to launch an external terminal emulator with something like :!xterm & or use a plugin like Conque Shell.

romainl

Posted 2012-07-16T19:59:11.700

Reputation: 19 227

1I would suggest something like tmux or screen too. – lucapette – 2012-07-17T08:56:14.580

Yes, indeed. But the asker insisted that it's a question about Vim itself. Tmux is great. Logged in on a remote server via SSH and using a well configured Vim within tmux/screen is a surprisingly enjoyable experience. Perfect for getting in the zone and staying there. – romainl – 2012-07-17T09:13:28.520

@romainl thank you for your awesome answer. I used gVim and I falsely assumed CLI vim also cannot display color... With some trials I found color is correctly displayed in my CLI vim. – CMinus – 2012-07-17T17:17:31.463

1

You can :set shellcmdflag=-ic to make the shell behave interactively, like your command prompt.

Conner

Posted 2012-07-16T19:59:11.700

Reputation: 406

1

You can make a function in .bashrc:

function ls { /bin/ls --color=always; }
export -f ls

kev

Posted 2012-07-16T19:59:11.700

Reputation: 9 972

Sorry for the typo, but I'm not asking how to display color with "ls" command -- I'm curious about how to retain the color when executing the external commands with vim. I just used "ls" as an example. – None – 2012-07-17T00:41:08.667