Vim Tips Wiki
No edit summary
m (Reverted edits by 124.123.58.157 (talk | block) to last version by Inkarkat)
 
(11 intermediate revisions by 7 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=121
 
|id=121
  +
|previous=120
|title=Using vim as a syntax-highlighting pager
 
  +
|next=122
|created=September 24, 2001 3:51
 
  +
|created=2001
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Mary Ellen Foster
 
|author=Mary Ellen Foster
 
|version=5.7
 
|version=5.7
 
|rating=268/97
 
|rating=268/97
  +
|category1=
|text=
 
  +
|category2=
If you want to use Vim's syntax highlighting in a "more"-style pager, here's
 
one way to set it up:
 
 
First, create a vimrc like the following -- I called mine <tt>~/.vimrc.more</tt>
 
 
" No compatibility -- necessary for mappings to work.
 
set nocompatible
 
 
" Status line
 
set laststatus=0
 
set cmdheight=1
 
set nomodifiable " Only in version 6.0
 
set readonly
 
 
" Syntax colouring -- lines taken from syntax.txt discussion on colour xterms.
 
" See '{{help|color-xterm}}'. Use appropriate lines for your own set-up.
 
if has("terminfo")
 
set t_Co=16
 
set t_Sf=�[3%p1%dm
 
set t_Sb=�[4%p1%dm
 
else
 
set t_Co=16
 
set t_Sf=�[3%dm
 
set t_Sb=�[4%dm
 
endif
 
 
" My xterms have a navy-blue background, so I need this line too.
 
set background=dark
 
 
" Turn syntax on
 
syntax on
 
 
" Key bindings.
 
nmap b <C-B><C-G>
 
nmap q :q<CR>
 
 
" To type the following line, type *two* C-V's followed by two spaces. This
 
" is how you map the spacebar.
 
nmap ^V <C-F><C-G>
 
 
Then, to use this .vimrc, add an alias. If you're using tcsh, the syntax will
 
be something like:
 
 
alias vmore "vim -u ~/.vimrc.more"
 
 
Then you can type '<tt>vmore [filename]</tt>' to view a file in this "pager". Spacebar
 
will move down, 'b' will move back up, and 'q' quits. You can add mappings for
 
other keys if you want to, also.
 
 
}}
 
}}
  +
If you want to use Vim's syntax highlighting in a pager, here's how. Since version 6, Vim comes with a script <code>$VIMRUNTIME/macros/less.sh</code>, and this gives syntax highlighting in a pager. See {{help|less}}.
   
  +
The original tip (below) was for a "more"-style pager. It is kept here because it explains the principles nicely.
== Comments ==
 
If you are using vim 6, there is already something like this - a shell script installed as $VIMRUNTIME/macros/less.sh. It sets up keybindings as if you were using the common pager "less".
 
For more information, "[http://vimplugin.sf.net/cgi-bin/help?tag={{urlencode:less}} :help less]" from vim.
 
 
'''Anonymous'''
 
, September 24, 2001 19:41
 
----
 
I don't have vim 6.0 yet, but I added this to ~/.vimrc.pager:
 
 
" No menu bar and tool bar
 
set guioptions=aiMr
 
 
nmap &lt;Up&gt; &lt;C-Y&gt;
 
nmap &lt;Down&gt; &lt;C-E&gt;
 
 
Cursor key scrolling and no menu/tool bar.
 
 
ivo--AT--mentation.nl
 
, September 29, 2001 10:20
 
----
 
This is a great idea! I adjusted it for "less" since I like to use that. Well, first of all,
 
I made a little script called "vless" so that I can use it as my $PAGER. This means
 
that it has to pass "-" to vim when no command-line arguments are present:
 
 
&#35;!/bin/sh
 
vim -u $HOME/.vim.less "${--AT--:--}"
 
 
And here's my .vim.less file. I had to make all the &lt;C-X&gt; notation lower-case for it to work
 
on vim 5.6.
 
I removed the syntax terminfo stuff, because for me (on RedHat 6.2) it was in the termcap
 
for "ansi" and I didn't need anything more. I just mapped the keys I use most often.
 
 
" No compatibility -- necessary for mappings to work.
 
set nocompatible
 
 
" Status line
 
set laststatus=0
 
set cmdheight=1
 
"set nomodifiable " Only in version 6.0
 
set readonly
 
 
set is ic hls
 
set background=dark
 
" Turn syntax on
 
syntax on
 
:hi comment cterm=bold ctermfg=2
 
:hi PreProc cterm=bold ctermfg=5
 
:hi Constant cterm=bold ctermfg=6
 
:hi Special cterm=bold ctermfg=7
 
:hi Search ctermfg=7 ctermbg=6
 
:hi IncSearch cterm=bold ctermfg=7 ctermbg=6
 
:hi Type cterm=bold ctermfg=7
 
 
" Key bindings.
 
nmap b &lt;C-b&gt;H&lt;C-g&gt;
 
nmap q :q!&lt;CR&gt;
 
nmap g 1G&lt;C-g&gt;
 
nmap j &lt;C-e&gt;
 
nnoremap k &lt;C-y&gt;k
 
nmap e &lt;C-e&gt;
 
nnoremap y &lt;C-y&gt;k
 
nmap d &lt;C-d&gt;z&lt;CR&gt;
 
nmap u &lt;C-u&gt;H
 
nmap &lt;CR&gt; &lt;C-e&gt;
 
nmap = &lt;C-g&gt;
 
nmap &lt;Esc&gt;u :noh&lt;CR&gt;
 
nmap E :e!&lt;Space&gt;
 
" To type the following line, type *two* C-V's followed by two spaces. This
 
" is how you map the spacebar.
 
nmap ^V &lt;C-f&gt;&lt;C-g&gt;
 
 
Sanjay Aiyagari &lt;saiyaga1881--AT--earthlink.net&gt;
 
, October 2, 2001 1:20
 
----
 
Those who like to use vim as a viewer might want to check out
 
[[VimTip167]] "Using vim as a man-page viewer under Unix".
 
 
cec--AT--NgrOyphSon.gPsfAc.nMasa.gov
 
, January 14, 2002 14:16
 
----
 
I have quite a few plugins that get weird with a page, so I added the '--noplugins' option to the vim command in the shell script.
 
 
But it's a wonderful thing . . .
 
 
thehaas--AT--binary.net
 
, June 12, 2002 7:49
 
----
 
Only function of less that I like most is use space to scroll a page at a time.
 
If you are not in Vi's inseart mode, space only move the cursor to the next character.
 
Which is not so important feature, so I map space to page down
 
 
map &lt;Space&gt; &lt;PageDown&gt;
 
map &lt;S-Space&gt; &lt;PageUp&gt;
 
 
While shift-space is not page-up in less, I found it a reasonable mapping.
 
 
dawnsnow--AT--bemail.org
 
, July 19, 2002 0:42
 
----
 
For whatever reason (probably the moron user), the nmap of the space bar in the orginal tip would end up paging down and printing the status line until EOF. But when I added &lt;C-G&gt; to the space mapping from dawnsnow, it worked like a champ.
 
 
matt--AT--mc-c.net
 
, November 20, 2002 5:35
 
----
 
I have done the following to make vim 6.1 work as my PAGER and it works really well. man(1) uses PAGER to display manpages and I had trouble with vim's filetype autodetection picking up that it was indeed a manpage coming in over the pipe. To counter this I've written a scripts.vim to match manpages by regex and set the filetype accordingly. Otherwise, I got no syntax highlighting. The shell I quote is tcsh-specific, but easily portable to another shell. Also, I assume you already have a ~/bin and that it is in your path and that you already have a ~/.vim. If not, you will need to do these things.
 
   
  +
First, create a vimrc like the following &ndash; I called mine <code>~/.vimrc.more</code>
% ln -s /path/to/VIMRUNTIME/macros/less.sh ~/bin/vless
 
% cat &gt;&gt; ~/.vim/scripts.vim
 
if did_filetype()
 
finish
 
endif
 
if getline(1) =~ '^\(.\+\)(\d\{1}).\+\1(\d\{1})$'
 
set filetype=man
 
endif
 
^D
 
% cat &gt;&gt; .cshrc
 
&#35; config pager
 
if (-X vless &amp;&amp; -X col) then
 
setenv PAGER 'col -b | vless'
 
alias less vless
 
alias more vless
 
else if (-X less) then
 
setenv PAGER less
 
setenv LESS "-e"
 
alias more less
 
else if (-X more) then
 
setenv PAGER more
 
else
 
echo "WARNING: No pager found, PAGER not set."
 
endif
 
^D
 
   
  +
<pre>
Brandon D. Valentine &lt;vim_tips--AT--geekpunk.net&gt;
 
  +
" No compatibility &ndash; necessary for mappings to work.
, March 15, 2003 0:51
 
  +
set nocompatible
----
 
Actually, here's a smarter way to do that scripts.vim (the original was taken from the vim manual |new-filetypes-scripts|):
 
   
  +
" Status line
if did_filetype() " filetype already set..
 
  +
set laststatus=0
elseif getline(1) =~ '^\(.\+\)(\d\{1}).\+\1(\d\{1})$'
 
set filetype=man
+
set cmdheight=1
  +
set nomodifiable " Only in version 6.0
endif
 
  +
set readonly
   
  +
" My xterms have a navy-blue background, so I need this line too.
The previous example works but wouldn't allow you to add anything else to your scripts.vim.
 
  +
set background=dark
   
  +
" Turn syntax on
Brandon D. Valentine &lt;vim_tips--AT--geekpunk.net&gt;
 
  +
syntax enable
, March 15, 2003 1:00
 
----
 
Why not use Bram's less.vim that ships with the product?
 
   
  +
" No menu bar and tool bar
'''Anonymous'''
 
  +
set guioptions=aiMr
, November 10, 2003 15:44
 
----
 
Brandon, I think this is even better:
 
   
  +
" Key bindings.
if did_filetype() " filetype already set..
 
  +
nmap b <C-B><C-G>
elseif getline(1) =~ '^\(.\+(\d\{1}\w\{0,1})\).\+\1$'
 
  +
nmap q :q!<CR>
set filetype=man
 
  +
nmap <Up> <C-Y>
endif
 
  +
nmap <Down> <C-E>
   
  +
" To type the following line, type *two* C-V's followed by two spaces. This
Two reasons:
 
  +
" is how you map the spacebar.
- I have "X(7x)" manpage
 
  +
nmap ^V <C-F><C-G>
- The manpage name is the same at both places, regex should reflect that.
 
  +
</pre>
   
  +
Then, to use this .vimrc, add an alias. If you're using tcsh, the syntax will
wejn_at_box_dot_cz
 
  +
be something like:
, May 11, 2006 11:57
 
----
 
I am searching for more general highliting, e.g.
 
   
  +
alias vmore "vim -u ~/.vimrc.more -"
cat filename | highlite_c
 
   
  +
Then you can type <code>''program'' | vmore</code> to view <code>''program''</code>'s output in this "pager". Spacebar will move down, 'b' will move back up, and 'q' quits. You can add mappings for other keys if you want to, also.
echo "text" | highlite_perl
 
   
  +
Those who like to use Vim as a viewer might want to see [[VimTip167|Using Vim as a man-page viewer under Unix]].
etc.
 
is it possible to use vim for it?
 
   
  +
==Related plugins==
nbd_at_x
 
, September 21, 2006 8:19
 
----
 
<!-- parsed by vimtips.py in 0.633844 seconds-->
 
   
  +
* {{script|id=1723|text=vimpager}} is an enhanced version of the built-in script.
   
  +
==Comments==
[[Category:VimTip]]
 

Latest revision as of 16:42, 4 August 2014

Tip 121 Printable Monobook Previous Next

created 2001 · complexity intermediate · author Mary Ellen Foster · version 5.7


If you want to use Vim's syntax highlighting in a pager, here's how. Since version 6, Vim comes with a script $VIMRUNTIME/macros/less.sh, and this gives syntax highlighting in a pager. See :help less.

The original tip (below) was for a "more"-style pager. It is kept here because it explains the principles nicely.

First, create a vimrc like the following – I called mine ~/.vimrc.more

" No compatibility – necessary for mappings to work.
set nocompatible

" Status line
set laststatus=0
set cmdheight=1
set nomodifiable " Only in version 6.0
set readonly

" My xterms have a navy-blue background, so I need this line too.
set background=dark

" Turn syntax on
syntax enable

" No menu bar and tool bar
set guioptions=aiMr

" Key bindings.
nmap b <C-B><C-G>
nmap q :q!<CR>
nmap <Up> <C-Y>
nmap <Down> <C-E>

" To type the following line, type *two* C-V's followed by two spaces. This
" is how you map the spacebar.
nmap ^V <C-F><C-G>

Then, to use this .vimrc, add an alias. If you're using tcsh, the syntax will be something like:

alias vmore "vim -u ~/.vimrc.more -"

Then you can type program | vmore to view program's output in this "pager". Spacebar will move down, 'b' will move back up, and 'q' quits. You can add mappings for other keys if you want to, also.

Those who like to use Vim as a viewer might want to see Using Vim as a man-page viewer under Unix.

Related plugins[]

  • vimpager is an enhanced version of the built-in script.

Comments[]