Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Can nano do syntax highlighting like gedit and vim? How can I enable it? I need at least bash and python syntax highlights.

share|improve this question
    
Thanks very much for the syntax files in the git repo, I use this on all my servers now... keep up the good work matey !!! –  TheGene Sep 12 at 15:23
    
@TheGene Comment and thank on the answer. The repo is credited to him, not me, the one asking the question. –  Oxwivi Sep 15 at 11:45

4 Answers 4

up vote 41 down vote accepted

The nano editor provides syntax highlighting for a few languages and scripts by itself. Check out /usr/share/nano/

nits@nits-excalibur:~$ ls /usr/share/nano/
asm.nanorc     fortran.nanorc   man.nanorc     ocaml.nanorc   ruby.nanorc
awk.nanorc     gentoo.nanorc    mgp.nanorc     patch.nanorc   sh.nanorc
c.nanorc       groff.nanorc     mutt.nanorc    perl.nanorc    tcl.nanorc
cmake.nanorc   html.nanorc      nano-menu.xpm  php.nanorc     tex.nanorc
css.nanorc     java.nanorc      nanorc.nanorc  pov.nanorc     xml.nanorc
debian.nanorc  makefile.nanorc  objc.nanorc    python.nanorc

Link them to your user's nano configuration file present at ~/.nanorc with something similiar to this line:

nits@nits-excalibur:~$ cat ~/.nanorc
include /usr/share/nano/sh.nanorc

Now, syntax highlighting is enabled in nano for whatever file you linked (You could also link multiple files)

screenshot of syntax highlighting

Note: Sometimes you might get a segmentation fault after you have edited your ~/.nanorc file. If such an error occurs, unsetting the LANG environment variable helps. You can unset it with unset LANG in the terminal. (Solution obtained from here)

There are also other solutions if you are not satisfied with your bash highlighting. One example of such can be found here

share|improve this answer
    
Those lines are uncommented in /etc/nanorc - are the global settings being overwritten by ~/.nanorc? How do I disable .nanorc? –  Oxwivi Dec 23 '11 at 16:31
1  
Oh wait, I don't even have a ~/.nanorc file. –  Oxwivi Dec 23 '11 at 16:33
    
The global settings (in /etc) provide settings for all users. The ~/.nanorc provides additional settings for your user-profile. To disable your '~/.nanorc' you can just rename it or delete it. (Edit after the second comment), you can create one by touch ~/.nanorc –  nitstorm Dec 23 '11 at 16:34
    
Wait, wait, how come the global setting with all the include lines not enabling syntax highlights even though I have not .nanorc? –  Oxwivi Dec 23 '11 at 16:53
    
This is an excerpt from the /etc/nanorc - ## Please note that you must have configured nano with --enable-nanorc for this file to be read! –  nitstorm Dec 23 '11 at 17:11

Yes, it does seem possible. Here's a guide that explains how to enable syntax highlighting: http://tuxarena.blogspot.com/2009/04/how-to-enable-syntax-highlighting-in.html.

It took me a few seconds to google "syntax highlighting nano" and that popped right up. I'd recommend trying to search for your question before asking it, as you will probably get a quicker response time.

On a side note, I highly recommend taking the plunge and learning Vim (or emacs, if that suits you better.) It will only take 20 or 30 minutes to learn the basic functionality you get out of nano, and you can learn about additional features and plugins at whatever pace you want to.

share|improve this answer
3  
I'm familiar with the basics of vim, thanks to a Red Hat course. But for general use, I prefer a dead simple editor so that I don't have to press Esc and wq, etc. every time. –  Oxwivi Dec 23 '11 at 16:26
4  
I did Google, but just making sure on AU and ensuring that this knowledge is here as well. –  Oxwivi Dec 23 '11 at 16:29

Yes you can, however the default syntax definitions are quite poor and incomplete. I'm maintaining a more accurate set of definitions here, for anyone who finds them useful.

To install, run:

git clone https://github.com/nanorc/nanorc.git
cd nanorc
make install
echo 'include ~/.nano/syntax/ALL.nanorc' >> ~/.nanorc
share|improve this answer
2  
Thanks for keeping those nice syntax definitions in GitHub. –  Rubens Mariuzzo Nov 13 '12 at 2:43
    
My *.js files weren't working...edited /etc/nanorc to remove any other syntax includes as per the FAQ github.com/nanorc/…. ♥ Nano ♥ nanorc –  Carlton Aug 6 at 12:27
1  
I just installed your syntax defs on Mac OSX. There's only one thing I'd note, which is the black colour of the open-bracket in python gets lost against the black background of my terminal –  Tom Busby Aug 27 at 14:25
wget -r -nH --cut-dirs=3 --no-parent --reject="index.html*" https://nanosyntax.googlecode.com/svn/trunk/syntax-nanorc/ -P ~/.nano-syntax
for i in `ls --color=never -1 ~/.nano-syntax/*.nanorc` ; do echo "include $i" >> ~/.nanorc ; done
share|improve this answer
8  
It would be helpful if you could explain why this is a useful answer for newer users. What is the command doing? What does it change? Randomly suggesting to run a command line is not helpful. –  Kevin Bowen Jun 28 '13 at 3:10

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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