Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Vim to "colourize" files or input streams

Tags:

vim

This may be an odd question, but still. I use cat to display a file in bash (KDE Konsole),

cat foobar.rb

Now, I would like to use Vim to colourize that foobar.rb file according to what you would get when you start foobar.rb in Vim. Edit: But only for display purpose, on the terminal.

I am not sure this is possible, but I thought it would be neat if I could use Vim for that.

I really just want colourized keywords, and Vim has the perfect colour definitions.

So I thought combining this would be great.

Is this possible in Vim out of the box though?

like image 968
shevy Avatar asked Dec 24 '11 20:12

shevy


2 Answers

One approach would be to use a library such as Pygments, which is a general purpose syntax highlighter. You could write a wrapper called ccat or something that would apply syntax highlighting to an input file and write to stdout.

If you want to page up and down in a highlighted file, you can use less with the -R switch, which passes control characters through to the terminal directly, preserving colours. So:

ccat file.rb | less -R

But at that point, you're pretty much at the capabilities of view.

like image 165
Greg Hewgill Avatar answered Sep 20 '22 11:09

Greg Hewgill


I'm not sure if I understand your question correctly, but if you are only looking for a command that will give you a read-only view of the input file (like cat) but with coloured keywords, use view. view is an alternative way to start vim in read-only mode, so you have all syntax highlighting possibilities. From the vim man page:

   view      Start  in read-only mode.  You will be protected from writing
             the files.  Can also be done with the "-R" argument.

   gvim gview
             The GUI version.  Starts a new window.  Can also be done with
             the "-g" argument.

   evim eview
             The GUI version in easy mode.  Starts a new window.  Can also
             be done with the "-y" argument.

   rvim rview rgvim rgview
             Like the above, but with restrictions.  It will not be possi-
             ble  to  start  shell  commands, or suspend Vim.  Can also be
             done with the "-Z" argument.

I have always seen view on systems that have vim installed.

like image 20
jstarek Avatar answered Sep 24 '22 11:09

jstarek