Open braham-snyder opened 6 years ago
I was experimenting with colorizing programs and found this ticket, and felt really sorry for you. So I'll help you out.
Vim actually already has a built-in "vimpager" script, called less.sh
. It's installed in a "macros" folder somewhere on your system. Find it with sudo find -x / -name less.sh
. Although it isn't very good...
Either way, the best pager is not vim. Not by a long shot. Vim is slow as hell as a pager (takes 2 seconds to start up before anything is shown). The best pager is less
. But not by default. It needs some options to make it usable. Then you'll get full pagination, UTF-8, color highlighting, line numbers, text search via /
just like in vim, and you can even edit files with vim directly from less by pressing v
(for "visual editor"). And you press q
to leave less.
Here's how to setup less
properly to make it your pager and get syntax highlighted pagination of man-pages etc:
# COLORIZE THE OUTPUT OF "LESS" TO MAKE MANPAGES MORE READABLE
export LESS_TERMCAP_mb=$'\E[01;31m';
export LESS_TERMCAP_md=$'\E[01;31m';
export LESS_TERMCAP_me=$'\E[0m';
export LESS_TERMCAP_se=$'\E[0m';
export LESS_TERMCAP_so=$'\E[01;44;33m';
export LESS_TERMCAP_ue=$'\E[0m';
export LESS_TERMCAP_us=$'\E[01;32m';
# TWEAKING OTHER "LESS" PARAMETERS
export LESSCHARSET='utf-8';
export LESS='-i -N -w -z-4 -g -M -R -P%t?f%f :stdin .?pb%pb\%:?lbLine %lb:?bbByte %bb:-...';
# SETTING "LESS" AS DEFAULT PAGER AND REPLACING ALL OTHER PAGERS WITH IT
export PAGER=less;
alias more=$PAGER;
alias zless=$PAGER;
# SET UP "VIM" AS DEFAULT EDITOR
export VISUAL=vim;
export EDITOR=$VISUAL;
You need to put that in your ~/.bash_profile
or ~/.bashrc
.
Now all your man git
and stuff will load with less
in beautiful syntax highlighting, colorized mode. And it's super fast. Unlike vim/vimpager which takes 1-2 seconds to start, less
is actually instant.
vimcat
to colorize code, right? Well, it's truly garbage. I'm sorry... I'm really sorry. I understand the work involved in making the vimscript that powers it. But it's garbage due to how bad vim's scripting language/limitations are.For a 1600 line file, I ran vimcat <that file>
and it took about 2 minutes to print all lines, line by line... as if I was running on a 300 baud modem in the 1980s. If I tried Ctrl-C to abort that tedious process, it hung and stopped accepting keyboard input and had to be killed via ps aux | grep vim
and killing the process... (Probably because Ctrl-C just killed the "tail" pipe program but not actually vim? No idea...)
Why was it so slow? Because vimcat uses a temporary file named /tmp/vimcat_####/vimcat.fifo
which is read by the shell 1 line at a time until it is empty.
Tedious...
To install it, do the following:
First install Homebrew.
Then run these commands to install Python 2 and Python 3:
brew install python3
brew install python
brew unlink python && brew link --overwrite python # make sure python links to python2
pip2 install --upgrade pip setuptools # upgrade to latest pip2
pip3 install --upgrade pip setuptools # upgrade to latest pip3
# never use the generic “python” and “pip”.
# always use “python[2/3]” and “pip[2/3]”.
Then, to install the Python3-version of Pygments, I just ran:
pip3 install pygments
# to upgrade later:
pip3 install pygments --upgrade
Lastly, make an alias for running Pygmentize with output as colorized 256-color terminal, etc:
alias pcat='pygmentize -f terminal256 -O style=monokai -g'
Now just run pcat main.cpp
or pcat index.php
or whatever other language.
Enjoy the perfection on your screen! It syntax highlights the 1600 line file in ~1 second (pygmentize) compared to ~120 seconds (vimcat). And supports 300 programming languages.
You can change the style (theme). I use monokai
, but others are listed here: pygmentize -L styles
. (Another good one is native
).
If you need to output to other formats (like BBCode for forums, etc), then just run this command to look at all of its available output formats. :-) pygmentize -L formatters
.
Have fun.
Here's an example of the pcat
output:
I appreciate the thought, but I'm painfully aware of less
, and I'm only incidentally interested in vimcat
's behavior.
I'm looking for something more powerful. More precisely: I want to be able to search and select text in my pager as well as I can in emacs or vim (so -- ideally -- I'd like access to easymotion
, sneak
/snipe
, swiper
, evil-little-word
, etc.).
Meanwhile, less
can't even wrap its searches (which alone kills me) -- nor is its search incremental.
One use-case I'm particularly interested in is better terminal scrollback: I'd like to test https://github.com/kovidgoyal/kitty with my scrollback_pager
set to vimpager
(though I'd surely need to limit the number of lines sent to vimpager
to keep startup times reasonable).
+term
almost covers this use-case, but clips the scrollback upon resizes of the terminal window ( https://github.com/neovim/neovim/pull/5045 )emacs
's shell
misses the underlying shell's completion, so that's a no-go for me.emacs
's ansi-term
's font-locking is too slow (all printing is slowed down -- it also gets significantly slower as the buffer gets larger).
ansi-color-apply-on-region
(or equivalent) simply takes too long -- I suspect it would be fairly difficult to speed up the coloring enough for my tasteeshell
is that it's very nonstandard (it's possible I'll use it down the road, but first I want a setup for a more standard shell that I'm relatively happy with)You need to put that in your ~/.bash_profile or ~/.bashrc
You might want those non-bash-specific exports in ~/.profile
instead (which you can then source from ~/.bash_profile
if the latter also exists): https://unix.stackexchange.com/a/45687/193985
Vim is slow as hell as a pager (takes 2 seconds to start up before anything is shown).
I see sub-one-second startup times with this, which I greatly prefer to less (though I also need to try emacs
's woman
):
export MANPAGER="/usr/local/bin/nvim -c 'set ft=man' -"
Which is what led me to want to try vimpager
as well.
You wanted vimcat to colorize code, right?
No, actually -- I only included the vimcat -x
output above to hopefully help the author (or help anyone) diagnose why I cannot get vimpager
working.
For a 1600 line file, I ran vimcat
and it took about 2 minutes to print all lines, line by line... as if I was running on a 300 baud modem in the 1980s. If I tried Ctrl-C to abort that tedious process, it hung and stopped accepting keyboard input and had to be killed via ps aux | grep vim and killing the process... (Probably because Ctrl-C just killed the "tail" pipe program but not actually vim? No idea...) Why was it so slow? Because vimcat uses a temporary file named /tmp/vimcat_####/vimcat.fifo which is read by the shell 1 line at a time until it is empty.
While I would not at all be surprised if vimcat
were relatively slow, a two-minute runtime for a 1600 line file must surely be a separate bug? Have you tried with a blank ~/.vimrc
? (Though -- again -- I'm primarily interested in vimpager
.)
edit: there's always tmux
for scrollback paging and selection, but its vim-like keybindings are very limited
Haha, alright good luck with everything! :-) By the way, you can search incrementally forwards and backwards in less: /word
, n
, n
, n
, shift-N
, shift-N
.
And with my tweaks you get search result-highlighting etc. Works perfectly for my needs. But you've got other needs and vimpager seems more up your alley. Good luck.
Haha, alright good luck with everything! :-)
Thank you!
By the way, you can search incrementally
I mean a different kind of incremental search (it's unfortunately quite ambiguously-named -- I might start calling it "real-time search" or "as-you-type-matching" or "incrementally-updated search" or something): https://en.wikipedia.org/wiki/Incremental_search
Ohh that kind of search. Sure that's nice. :)
Hmm just as a warning to you:
Remember the vimcat
command I described taking 2 minutes, and which I hit Ctrl-C
to abort, which froze vim and the terminal and required me to kill that process manually?
Uhm... well, that exact failed process has somehow continued running in the background since yesterday and is taking 60% CPU:
I know it's that process, because under the Open Files and Ports
tab of Activity Monitor, it lists the folder of the file I was vimcat
ing... so it belongs to that (long-ago closed) terminal.
I discovered this now because my fans are going nuts, and I wondered why my CPU was so hot... and why my battery drained in like 40 minutes.
Strange stuff...
Time to reboot my computer.
Edit: Rebooted and all is well and my battery estimate is back to 2-3 hours. So yeah turns out vimcat can really badly kill bash on macOS Sierra. I mention it because it may be related to the crashing you mentioned in this ticket.
Did you ever find a solution for your problem? I'm running against the same wall at the moment ;-)
Regarding the vimpager
freeze? Nope, but I haven't toyed with it further.
Or $PAGER
more generally? I decided to settle -- for now at least -- with tmux
's set-window-option -g mode-keys vi
+ set -g status-keys emacs
(the vi
status-keys
are quite odd) + some minor modifications to make it slightly more vim-like (since, e.g., for some reason tmux
only defaults the emacs
-style search to incremental, and yanking does not cancel the visual selection by default).
All of the Emacs solutions print even medium-length outputs too slowly, due to the time it takes Emacs to deal with ANSI-coloring (which I suspect would be difficult to speed up).
https://github.com/neovim/neovim/issues/5054 could be quite relevant (with kitty
for scrollback paging without re-running commands), but I'm not quite sure of the scope (is the idea to add ANSI-coloring capability to nvim
itself?) -- plus it's "unplanned" at the moment.
@braham-snyder that issue is about the "internal pager" of (n)vim that is used for long output from vim commands (see :h more-prompt
).
I'm sorry, I haven't worked on this repo in a while because of various things in life, and am currently deeply engrossed in another project.
I'll try to take a break from the other project soon and take a look at stuff here.
@rkitover Relax and have fun with the other project. ;-) You're not under any obligation to do this. Have fun and come back when you feel like it.
@rkitover Take your time! My question was more out of curiosity since the error I'm getting is not exactly the same as described here and I was in doubt whether to create a separate issue or not ... no real life has caught me again and I'll have to see when I can dive into it a bit more ;-) @braham-snyder thanks for your quick response! I'll see how I can tackle the problem ...
@rkitover Sorry for reporting so late due to real life capture but my issue was fixed with 2.06-347-gf03db2e ... many thanks for that! 👏
On OS X El Capitan, with homebrew
vim
, usingvimpager
also installed through homebrew, with and without--HEAD
, frombash
, in both Terminal.app and iTerm2:At which point output stops, and all keys I press are simply appended to the output. Well, other than
^Z
or^\
-- the latter of which then shows (probably unremarkably):And same for
vimcat
, with this debugging output:Thanks for
vimpager
!