is there a way to syntax highlight a file I see in Less?

actually I'm using this command to open an xml file (and sometimes a series of them)

less htmleditors/htmleditors_config.xml

or

less [multiple files]

I'd like to stay in Less (to learn that program better and to use my knowledge of :n and :p for next/previous navigation)

But it also want some kind of basic syntax highlighting – at least show the comments differently. Do you know any way to do it?

link|improve this question

feedback

7 Answers

up vote 11 down vote accepted

You can use GNU's source-highlight, as shown here:.

 export LESSOPEN="| /path/to/src-hilite-lesspipe.sh %s"
 export LESS=' -R '
link|improve this answer
feedback

I think that you should use a text editor. I like vim myself. That will give you LOTS of power when viewing files and then when you want to edit them you will already know the basics.

Here are some of the advantages of using a text editor (specifically vim):

  • syntax-highlighting
  • powerful movement commands
  • find
  • jump to specific location in a file (called a mark)
  • folding (useful when you just want to see function stubbs)

To open your file in readonly mode use this:

vim -R <file name>

Here is a basic navigation guide:

j - move down one line
k - move up one line
h - left one char
l -right one char

ctrl-f - forward one page
ctrl-b - back one page

/<something> - search for something
n - next of whatever you searched for
N - next (search backwards) of whatever you searched for

:q - quit
:q! - quit without saving
:w - save

Here is a link for more information:

http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html

Just to recap, if you will use unix vim is pretty fundamental. I have heard that learning vim is like learning to type. It is the next most useful tool you can learn for programming.

(Just to avoid editor wars you could also look into emacs or another editor, however I personally prefer vim)

link|improve this answer
I second this. Vim launches as fast as less, and many of the keyboard shortcuts (e.g., search, next page, previous page) are identical. – Jeremy W. Sherman Feb 10 '11 at 18:20
feedback

Best of both previous answers : you can invoke Vim from within less, by typing "v".

link|improve this answer
feedback
pygmentize somefile.ex | less

or

function cless () {
    pygmentize -f terminal "$1" | less -R
}
link|improve this answer
feedback

I was also searching for this and found another solution using Vim: http://ubuntu-tutorials.com/2008/07/14/use-vim-as-a-syntax-highlighting-pager/

The post is rather old, so now on more recent distros vim 7.2 is shipped and the .bashrc will read: alias vless='vim -u /usr/share/vim/vim72/macros/less.vim'

link|improve this answer
MacVim comes with a shell script that you can use directly, located in: /Applications/MacVim.app/Contents/Resources/vim/runtime/macros/less.sh – Nick Jan 14 at 19:23
feedback

As others have said, you can use the power of vim. But importantly, you can do so without learning how to use vi/vim.

Vim comes with a less.vim script that works pretty well as a replacement for less, with full color syntax highlighting. It uses less keybindings (just hit 'q' to quit).

It had a few problems, so I improved it. I have a screenshot at http://huyz.us/2011/a-less-like-pager-with-color-syntax-highlighting/

link|improve this answer
feedback

If you have GNU Source-highlight installed you can use the following command to highlight the syntax of a single file:

$ src-hilite-lesspipe.sh yourfile.xml | less -R
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.