Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use vim in a pipeline to colorize text?

I'd like to have a command I can insert into a command pipeline that adds color escapes to its input according to vim's syntax highlighting capabilities.

For example:

cat somefile.js | vim - <???> | less

The resulting text would be that of somefile.js, but colorized according to how the current vim configuration would do it in-editor.

It occurs to me that this must be possible. I agree that the example up there isn't what a sane man might call exactly useful, but that doesn't mean the idea never is.

like image 612
Jon Carter Avatar asked Oct 30 '13 01:10

Jon Carter


People also ask

How do I color text in Vim?

You can change color schemes at anytime in vi by typing colorscheme followed by a space and the name of the color scheme. For more color schemes, you can browse this library on the vim website. You can enable or disable colors by simply typing "syntax on" or "syntax off" in vi.

How do I turn on syntax highlighting in Vim?

After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting. The file will look like the following image if syntax highlighting is on. Press ESC key and type, “syntax off” to disable syntax highlighting.

Does vim support syntax highlighting?

VIM is an alternative and advanced version of VI editor that enables Syntax highlighting feature in VI. Syntax highlighting means it can show some parts of text in another fonts and colors. VIM doesn't show whole file but have some limitations in highlighting particular keywords or text matching a pattern in a file.

How do I highlight in vim editor?

Press 1 to highlight the current visually selected text, or the current word (if nothing is selected). Highlight group hl1 is used. Press 2 for highlight hl2 , 3 for highlight hl3 , etc. Press 0 to remove all highlights from the current visually selected text, or the current word.


1 Answers

There's a Perl module called Text::VimColor that I've heard will do kinda what you're looking for.

http://search.cpan.org/dist/Text-VimColor/

But let me ask this: Why do want it to go through less? Why not use vim as a perfectly good file viewer? view - will read from standard input in read-only mode.

like image 55
Andy Lester Avatar answered Sep 19 '22 11:09

Andy Lester