In short, I'm in an effort to replace less with vim (vimpager). I have settings for scripts to spit out colors (and bold and everything nice) whenever they can. less understands the color codes and displays them nicely. How can I make vim parse the codes and display colors/boldness the way less does?
- If you want a full blown pager based on vim try vimpager or my rewrite for neovim: nvimpager. They use AnsiEsc and a rewrite in lua respectively, and wrap it all up into a script that can be used as a pager without further hacking on your part.Lucas– Lucas2021-05-18 13:50:24 +00:00Commented May 18, 2021 at 13:50
- Same question on SuperUser, Vi.SEuser202729– user2027292024-11-03 05:57:27 +00:00Commented Nov 3, 2024 at 5:57
5 Answers
Two answers:
A short one: you want to use the vim script AnsiEsc.vim. It will conceal the actual ANSI escape sequences in your file, and use syntax highlighting to color the text appropriately. The problem with using this in a pager is that you will have to make vim recognize when to use this. I am not sure if you can simply always load it, or if it will conflict with other syntax files. You will have to experiment with it.
A long answer: The best you can hope for is a partial non-portable solution. Less does not actually understand the terminal escape sequences, since these are largely terminal dependent, but less can recognize (a subset of) these, and will know to pass them through to the terminal, if you use the -r (or -R) option. The terminal will interprets the escape sequences and changes the attributes of the text (color, bold, underline ...). Vim, being an editor rather than a pager, does not simply pass raw control characters to the terminal. It needs to display them in some way, so you can actually edit them. You can use other features of vim, such as concealment and syntax highlighting to hide the sequences and use them for setting colors of the text, however, it will always handle only a subset of the terminal sequences, and will probably not work on some terminals.
This is really just one of many issues you will run into when you try to use a text editor as a pager.
- 2To put it in a nutshell, you have to write part of a terminal emulator in Vim, the part that handles the terminal escape sequences that are present in your input.Gilles 'SO- stop being evil'– Gilles 'SO- stop being evil'2011-02-20 19:02:36 +00:00Commented Feb 20, 2011 at 19:02
- 1Thanks, I think I'm giving up. If it's not easy it doesn't make much sense anymore.phunehehe– phunehehe2011-02-21 02:10:22 +00:00Commented Feb 21, 2011 at 2:10
- 1@JanHlavacek link is broken :(Nitrodist– Nitrodist2013-08-16 20:09:12 +00:00Commented Aug 16, 2013 at 20:09
- 42
- 1It seems Jan's link has moved to linux-tips.com/t/how-to-enable-syntax-highlighting-in-less/208Timothy Zorn– Timothy Zorn2017-02-17 09:24:24 +00:00Commented Feb 17, 2017 at 9:24
Now with vim 8 you can use terminal mode :terminal and then in that terminal do cat myfile and go back to normal mode with Ctrl-w N. This will display ANSI color codes correctly. By automating these steps and reading from standard input instead of a file, it should be possible to use vim to replace less.
For example, you can run ls --color=always >/tmp/colored.txt
or unbuffer ls >/tmp/colored.txt
and then in vim :terminal cat /tmp/colored.txt followed by :only
Then you will have the ls output nicely colored in vim like less would do. As vim supports passing commands as command-line arguments on startup, it is clearly possible to fiddle around to make that solution work for replacing less :)
- 2I took your idea and ran with it! gist.github.com/RichardBronosky/…Bruno Bronosky– Bruno Bronosky2021-03-04 08:48:08 +00:00Commented Mar 4, 2021 at 8:48
- 1Neovim can do the same, but use
<c-\><c-n>to go back to normal mode.Moberg– Moberg2021-09-14 09:10:16 +00:00Commented Sep 14, 2021 at 9:10 - 2Just Wow! This should be the accepted answer!...Except that if a file is too long (11M in my case), :terminal cat will truncate and loose the head :Sntg– ntg2021-09-21 08:49:58 +00:00Commented Sep 21, 2021 at 8:49
- "The commands that would start insert mode, such as 'i' and 'a', return to Terminal-Job mode." Then the terminal can be exited. P.S. I am on Vim 8.2, and it has both
<C-w>Nand<C-\><C-n>listed for entering Terminal-Normal mode.Kevin– Kevin2022-03-01 14:46:24 +00:00Commented Mar 1, 2022 at 14:46 - Meh. You can look, but not touch.Ярослав Рахматуллин– Ярослав Рахматуллин2022-05-01 17:26:48 +00:00Commented May 1, 2022 at 17:26
Install the vim plugin Improved AnsiEsc and put the below on your .profile/bash_profile/zprofile and you are good to go.
export PAGER="vim -R +AnsiEsc" - 1This is a great answer, +1 for export command.AzP– AzP2020-06-11 09:26:01 +00:00Commented Jun 11, 2020 at 9:26
- Does this works on vim 7.x or its vim 8.x solution?Prabhat Kumar Singh– Prabhat Kumar Singh2022-03-10 22:34:36 +00:00Commented Mar 10, 2022 at 22:34
I installed AnsiEsc like the others and then added the following to my _vimrc:
command Log colorscheme elflord | %s/\[39m//g | AnsiEsc Now, when I load a file that contains a bunch of color control characters, I type:
:Log And it looks like the command line, but I'm editing!
You can use vim's json syntax highlighting. Normally comes in most default vim installs. May not be all as pretty as the less -R BUT..
- If you have python, this makes is human readable with the
-mjson.tool
cat filename | python -mjson.tool | vim -c 'set syntax=json' - - If you don't have python but your json is pretty printed
cat filename | vim -c 'set syntax=json' - Also some browsers have json extensions, and you could pipe it into the browser. Hmmm I wonder if w3m might suffice for a pure cli effort.
Hope that helps.
