Use Vim to “colorize” files or input streams

This may be a strange 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 foobar.rb this foobar.rb file in accordance with what you would get when running foobar.rb in Vim. Edit: but only for display purposes, on the terminal.

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

I just need color keywords, and Vim has perfect color definitions.

So, I thought it would be great.

Is this possible in Vim out of the box?

+4
source share
3 answers

One approach would be to use a library such as Pygments , which is a general-purpose syntax shortcut. You can write a wrapper called ccat or something that would apply syntax highlighting to the input file and write to standard output.

If you want to move up and down in a selected file, you can use less with the -R switch, which transfers control characters directly to the terminal, preserving the colors. So:

 ccat file.rb | less -R 

But at this point, you are pretty much using view features.

+2
source

I'm not sure if I understood your question correctly, but if you are looking for only a command that will give you read-only access to the input file (for example, cat), but with colored keywords, use view , view - an alternative way to start vim in mode read-only, so you have all the syntax highlighting options. On 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 always saw an idea of ​​the systems where vim is installed.

+2
source

The closest is the less script that comes with vim:

 cat myfile | vim -u /usr/share/vim/vim72/macros/less.vim - 

Note the argument - on vim. You may need to change vim72 to your version (and the whole path if installed elsewhere).

Now this is not quite what you want, because its behavior is less like, since you have to press keys to scroll or end. However, they are more concise than regular vim. For example, space to scroll down; and q to exit (not :q ).

You need a cat version; So do I. But, it seems, not one.

EDIT , as well as the vimpager project that includes vimcat is exactly what you want. But it does not come with vim, and I have not tried it yet.

+1
source

Source: https://habr.com/ru/post/1387897/


All Articles