How do I enable syntax highlighting in nano?
Solution 1
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)
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.
Solution 2
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.
Solution 3
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 thefind
command. - Lastly,
>> ~/.nanorc
causes the output to be appended to your~/.nanorc
file.
Solution 4
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.
Solution 5
I accidentally disabled highlighting using the keyboard shortcut Alt+Y. It can be turned on again with the same keyboard shortcut.
Related videos on Youtube
Oxwivi
Updated on September 18, 2022Comments
-
Oxwivi over 1 year
Can nano do syntax highlighting like gedit and vim? How can I enable it? I need at least bash and python syntax highlights.
-
Oxwivi over 12 yearsThose lines are uncommented in
/etc/nanorc
- are the global settings being overwritten by~/.nanorc
? How do I disable.nanorc
? -
Oxwivi over 12 yearsOh wait, I don't even have a
~/.nanorc
file. -
Nitin Venkatesh over 12 yearsThe 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 bytouch ~/.nanorc
-
Oxwivi over 12 yearsWait, wait, how come the global setting with all the
include
lines not enabling syntax highlights even though I have not.nanorc
? -
Nitin Venkatesh over 12 yearsThis is an excerpt from the
/etc/nanorc
-## Please note that you must have configured nano with --enable-nanorc for this file to be read!
-
Oxwivi over 12 yearsAh, 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. -
Nitin Venkatesh over 12 yearsMost welcome :)
-
Kevin Bowen almost 11 yearsIt 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.
-
trusktr over 10 yearsHow do you do it while inside nano?
-
Carlton over 9 yearsMy *.js files weren't working...edited /etc/nanorc to remove any other syntax includes as per the FAQ github.com/nanorc/…. ♥ Nano ♥ nanorc
-
Tom Busby over 9 yearsI 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
-
dukevin over 9 yearsActually there is a problem, some of the colored text is black! If our terminal background is black, these words are invisible!
-
code_monk over 9 yearsthanks for your hard work! you should get this into the upstream repo, IMHO
-
TachyonVortex about 9 yearsTo 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
-
A.B. over 8 yearsThe 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 agit pull
. -
A.B. over 8 yearsYour language tag is wrong.
js
isn'tbash
orshell-script
and I have placed the link behind the wordhere
. Where is the problem? -
kodybrown over 8 years+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
-
Abdull over 8 yearsweird, XML syntax highlighting does not work for me.
-
Thom Porter about 8 years
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. -
daveoncode almost 8 yearsgreat tip and explanation, thanks a lot man! ;)
-
Jarek Jakubowski almost 8 years
include /usr/share/nano/*
is not working -
aalaap almost 8 yearsEven 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!
-
user3757405 almost 8 yearsIf I write
include /usr/share/nano/*
to ~/.nanorc, I just getError 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? -
mareoraft almost 8 yearsOn 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? -
Will over 7 years@mwfearnley It should probably be removed. Fileglob is not supported throughout all versions of nano.
-
Pablo Bianchi over 6 years@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 over 6 yearsSame 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. -
Liker777 about 6 yearsthis didn't work for me...
-
muru about 6 yearsPretty much the same as askubuntu.com/a/865425/158442 and askubuntu.com/a/538674/158442 do
-
not2qubit over 5 yearsYou have a dead link
-
Milind about 5 yearsThanks and doesn't look like you need the iname flag. The following seems to work as well find /usr/share/nano/*.nanorc
-
IronEagle almost 5 yearsgithub.com/scopatz/nanorc same idea as dead link
-
VIX over 4 yearsThansk very much!
-
Artanis Zeratul about 4 yearsthis doesn't work :(
-
mchid about 3 years@JarekJakubowski use
include "/usr/share/nano/*.nanorc"
I think it needs double quotes -
Steve Robillard over 2 yearsI 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.