245

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

Jorge Castro
  • 71,754
Oxwivi
  • 17,849

15 Answers15

238

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, ${XDG_CONFIG_HOME}/nano/nanorc, or ~/.config/nano/nanorc, whichever is encountered first) 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

You can also write include /usr/share/nano/* to the ~/.nanorc file to enable all languages to be highlighted if your nano version supports it.

Hendrikto
  • 103
  • 1
    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
  • 19
    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 – Nitin Venkatesh Dec 23 '11 at 16:34
  • 3
    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
  • 1
    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! – Nitin Venkatesh Dec 23 '11 at 17:11
  • Ah, I did not read it properly. I see now, thanks. So either I have a .nanorc file or I modify .bashrc to add --enable-nanorc flag whenever I type nano... And I was hoping that I would be able to do away with user-specific changes... Anyway, thanks for all the help. – Oxwivi Dec 23 '11 at 17:19
  • How do you do it while inside nano? – trusktr Oct 23 '13 at 20:37
  • 2
    To enable syntax highlighting when editing a file with an unrecognised filename extension (such as ~/.bashrc), you can use the --syntax option. For example: nano --syntax=sh ~/.bashrc – TachyonVortex Jan 27 '15 at 13:57
  • 20
    ls -1 /usr/share/nano/*.nanorc | sed 's/^\//include \//' >> ~/.nanorc append them all to your .nanorc file, or create it if it does not exist. – Thom Porter Feb 08 '16 at 00:10
  • 12
    include /usr/share/nano/* is not working – Jarek Jakubowski May 16 '16 at 08:23
  • 1
    Even I didn't have a ~/.nanorc file, but I just created one using nano itself. I can't believe this stuff isn't enabled by default! – aalaap Jun 06 '16 at 10:48
  • If I write include /usr/share/nano/* to ~/.nanorc, I just get Error reading /usr/share/nano/*: No such file or directory on starting nano. Could I be doing something wrong, or should that bit be removed from the answer? – mwfearnley Jun 10 '16 at 13:11
  • 1
    @mwfearnley It should probably be removed. Fileglob is not supported throughout all versions of nano. – Will Sep 22 '16 at 20:20
  • @JarekJakubowski use include "/usr/share/nano/*.nanorc" I think it needs double quotes – mchid Mar 29 '21 at 01:32
  • In my case there was a directory /usr/share/nano/extra, so nano printed error [ Mistakes in '/home/sunnycove/.config/nano/nanorc' ], if include /usr/share/nano/* set. Fixed with include /usr/share/nano/*.nanorc – Sunny Cove May 01 '23 at 22:33
105

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/scopatz/nanorc.git
cd nanorc
make install

Add these lines to the ~/.nanorc

include ~/.nano/syntax/html.nanorc
include ~/.nano/syntax/css.nanorc
include ~/.nano/syntax/php.nanorc
include ~/.nano/syntax/ALL.nanorc

Specify the ones you want to have colorizing for, and you will have to tune these colors to your preferences. The ALL.nanorc describes features for all yet unclassified files. These are the tools you need to get started, not the end polished product.

Code42
  • 103
  • 4
Craig Barnes
  • 1,067
  • 1
  • 7
  • 2
  • 3
    My *.js files weren't working...edited /etc/nanorc to remove any other syntax includes as per the FAQ https://github.com/nanorc/nanorc#why-does-syntax-highlighting-only-work-for-a-subset-of-supported-files. ♥ Nano ♥ nanorc – Carlton Aug 06 '14 at 12:27
  • 5
    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 '14 at 14:25
  • 5
    Actually there is a problem, some of the colored text is black! If our terminal background is black, these words are invisible! – dukevin Dec 08 '14 at 03:20
  • 2
    thanks for your hard work! you should get this into the upstream repo, IMHO – code_monk Jan 14 '15 at 17:27
  • 1
    +druciferre provides a great command below that adds all syntax files automatically to your .nanorc file. I changed the path to point to your default install location: find $HOME/.nano/syntax/ -iname "*.nanorc" -exec echo include {} \; >> ~/.nanorc – kodybrown Sep 03 '15 at 17:26
  • On FreeBSD with nano 2.4.3, with or without BSDREGEX=1, I get error message make: "/usr/home/freebsd/nanorc/Makefile" line 40: Missing dependency operator make: "/usr/home/freebsd/nanorc/Makefile" line 42: Need an operator ...(skipping similar lines)...make: Fatal errors encountered -- cannot continue make: stopped in /usr/home/freebsd/nanorc. Any ideas? – mareoraft Jun 17 '16 at 17:29
  • Same procedure with all syntax files at once: find $HOME/.nano/syntax/ -iname "*.nanorc" -exec echo include {} \; >> ~/.nanorc. Also, since all syntax files are now on $HOME/.nano/syntax/ is safe to delete the cloned folder. – Pablo Bianchi Nov 21 '17 at 17:43
  • In alpine virtual x86_64.I cloned git and created .nanorc in root directory which has include "./nanorc/*.nanorc" and everything worked perfectly. No need to make install or install.sh if I am not missing something huge ... – Gediz GÜRSU Oct 21 '22 at 08:25
75

I used this command to quickly enable all available languages.

find /usr/share/nano/ -iname "*.nanorc" -exec echo include {} \; >> ~/.nanorc

As mentioned in other answers, /usr/share/nano/ contains the definitions for different languages.

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

Also mentioned, to enable highlighting for a language, you add include and the path to the language definition you want to enable to your ~/.nanorc file. So, for example, to enable C/C++ you would add this line.

include /usr/share/nano/c.nanorc

The find command searches for files or directories within the specified directory.

  • The -iname flag tells it to only look for files with a name that ends with .nanorc.
  • The -exec flags defines a command to execute on each file found.
  • The {} gets replaced with the file name.
  • \; is used to signify the end of the command to execute to the find command.
  • Lastly, >> ~/.nanorc causes the output to be appended to your ~/.nanorc file.
  • 1
    great tip and explanation, thanks a lot man! ;) – daveoncode May 14 '16 at 17:02
  • Thanks and doesn't look like you need the iname flag. The following seems to work as well find /usr/share/nano/*.nanorc – Milind Feb 17 '19 at 00:06
  • 1
    I realize this is an old question, but I just added this to my puppet configuration and would make one small addition, adding sort before appending to the file. find /usr/share/nano/ -iname "*.nanorc" -exec echo include {} ; || sort >> ~/.nanorc this will make it easier to see if you have language support or not when adding a new language. – Steve Robillard Sep 29 '21 at 22:12
8

This should include all the syntax highlighting plugins included by default, and any you add to /usr/share/nano:

find /usr/share/nano -name '*.nanorc' -printf "include %p\n" > ~/.nanorc

Or, edit /etc/nanorc and see if you can uncomment the includes.

This works if you have a version of nano that refuses to accept wildcards in the .nanorc file.

Just run this line every time you add an additional .nanorc colour config.

NotoriousPyro
  • 206
  • 2
  • 3
  • IMHO that's not a good solution, because this eliminates all the settings from the users current .nanorc by overwriting it. – DJCrashdummy Dec 27 '22 at 11:38
6

You can use this to include all: (add this to ~/.nanorc file)

include "/usr/share/nano/*.nanorc"

Happy coding!

lidaobing
  • 103
  • 4
5

I accidentally disabled highlighting using the keyboard shortcut Alt+Y. It can be turned on again with the same keyboard shortcut.

Thomas Weller
  • 4,617
  • 4
  • 24
  • 36
4

I thought I might suggest something more fresh and up-to-date as of almost 2020 :

Scopatz's nanorc on Git has all you need. it is still regularly updated.

and is delivered with a pretty neat install guide & documented installer that allows you to get things rolling fast, clean, & easy.

Note : You might have to drop some of the highlighters from the nanorc file or comment them out (etc-host, html, and html.j2) as they seem to be faulty, but no big deal

muru
  • 197,895
  • 55
  • 485
  • 740
2

I know it is an old thread but may my solution can help

edit the file at /etc/nanorc

From the line 243 you can find lot include files. Simply uncomment the ones you need, save and done.

## Nanorc files
include "/usr/share/nano/nanorc.nanorc"

## C/C++
include "/usr/share/nano/c.nanorc"

## Makefiles
include "/usr/share/nano/makefile.nanorc"

## Cascading Style Sheets
include "/usr/share/nano/css.nanorc"

## Debian files
include "/usr/share/nano/debian.nanorc"

## Gentoo files
# include "/usr/share/nano/gentoo.nanorc"

I am not sure if you update nano will these changes vanished.

2

Edit: outdated, please refer to other answers

The most complete and up to date syntax hilighters for nano are maintained here

To install all highlighters for your user just run:

cd /tmp
git clone https://github.com/tech4david/nano-highlight.git
cd nano-highlight/
make install
echo "include ~/.nano/syntax/ALL.nanorc" >> ~/.nanorc
Raveren
  • 123
  • The solution is good, but I would use an other folder for git clone. The content otf the /tmp folder will be deleted on every system start and therefore you could never run a git pull. – A.B. Jul 30 '15 at 08:00
  • Your language tag is wrong. js isn't bash or shell-script and I have placed the link behind the word here. Where is the problem? – A.B. Jul 30 '15 at 08:19
  • 2
    @A.B. it doesn't matter if that folder is gone since when you make install all the syntax files are copied to the safe place where they belong: ~/.nano/syntax. – Pablo Bianchi Oct 14 '17 at 21:06
  • Project is now archived. Not maintained anymore. – Melroy van den Berg Jan 12 '23 at 23:48
2

For those who are having trouble after following the instructions above, also check if you have duplicate definitions. These will disable all syntax highlighting for all files. I ran into this after adding a nanorc.nanorc file, and not realising that my ~/.nanorc already had an identical section, i.e.:

## Here is an example for nanorc files.
##
syntax "nanorc" "\.?nanorc$"
## Possible errors and parameters
icolor brightwhite "^[[:space:]]*((un)?set|include|syntax|i?color).*$"
## Keywords
icolor brightgreen "^[[:space:]]*(set|unset)[[:space:]]+(autoindent|backup|$
icolor green "^[[:space:]]*(set|unset|include|syntax)\>"
(...)
dmvianna
  • 133
1

nano ~/.nanorc

just write below code i

include "/usr/share/nano/*.nanorc"

set linenumbers

1

To add support for TypeScript JSX (.tsx) files do the following:

  1. Open ~/.nanorc file and find "javascript" slug using ctrl+w in nano editor
  2. You should find something like include /Users/youruser/nanorc/javascript.nanorc, open this .nanorc file
  3. On the first line edit syntax "JavaScript" "\.(js|ts)$" to syntax "JavaScript" "\.(js|ts|jsx|tsx)$" to include .jsx and .tsx file extensions
0

For those who have working and included *.nanorc files but still see a monochrome text (or maybe regular and bold, use Alt+Y while editing to toggle syntax highlighting), it may be your TERM environment variable is not recognized. I had it set to xterm-256color but on that particular host that value didn't use color in nano for whatever reason.. doing $ TERM=screen worked for me.
You can see what it currently is with $ echo $TERM.

MSpreij
  • 101
  • 2
0
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
user170859
  • 25
  • 1
  • 26
    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 03:10
0

I found syntax highlighting for js here.

I appended it into my user file at ~/.nanorc

However to get a file into which I could append, I had to first copy from /usr/share/nano/nanorc.nanorc to my local folder first as ~/.nanorc.

A.B.
  • 90,397