How to enable syntax highlighting for Markdown files in GNU Nano

How to enable syntax highlighting for Markdown files in GNU Nano

GNU nano is a simple terminal-based text editor. Though not as powerful as Emacs or Vim, it is easy to learn and use. A lot of developers prefer this editor as it's very simple to use and pretty useful when you only want to edit a single file quickly on your server.

One of those files that you need to change often in this kind of editor are configuration file, like markdown files. Nano offers syntax highlighting for many file types, however not for Markdown files. If you want to highlight this kind of files as well, you will need to follow an extra step. In this article, we'll show you how to highlight Markdown files on nano in Ubuntu.

1. List available Nano Syntax Highlight Files

As first step, discover which languages are available in nano to highlight its syntax with the following command:

ls /usr/share/nano/

This will list all the nano syntax highlighting files in the given directory:

root@server:~$ 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

If you don't find the markdown.nanorc file, then you can install it with the next step.

2. Create Markdown Nano Syntax Highlighting File

In order to provide syntax highlighting to your file, if the default file doesn't exist, you need to create the syntax highlighting file for this language. This file is the markdown.nanorc file and you need to create it in the mentioned directory. Run nano to create the file:

sudo nano /usr/share/nano/markdown.nanorc

and paste the following content:

syntax "markdown" "\.md$" "\.markdown$"

## Quotations
color cyan "^>.*"

## Emphasis
color green "_[^_]*_"
color green "\*[^\*]*\*"

## Strong emphasis
color brightgreen "\*\*[^\*]*\*\*"
color brightgreen "__[\_]*__"

## Underline headers
color brightblue "^====(=*)"
color brightblue "^----(-*)"

## Hash headers
color brightblue "^#.*"

## Linkified URLs (and inline html tags)
color brightmagenta start="<" end=">"

## Links
color brightmagenta "\[.*\](\([^\)]*\))?"

## Link id's:
color brightmagenta "^\[.*\]:( )+.*"

## Code spans
color brightyellow "`[^`]*`"

## Links and inline images
color brightmagenta start="!\[" end="\]"
color brightmagenta start="\[" end="\]"

## Lists
color yellow "^( )*(\*|\+|\-|[0-9]+\.) "

Visit the official repository of Nano Highlight, a spiffy collection of nano syntax highlighting files for more information and languages available for nano. This file will be automatically added into nano and will highlight markdown files. Save changes and proceed with the last step.

3. Create Test Markdown File to see results

As final step, you need to test wheter the highlight works or not. Proceed to create a test file with nano and write some Markdown based content on it, for example:

# h1 Heading 8-)
## h2 Heading
### h3 Heading
#### h4 Heading
##### h5 Heading
###### h6 Heading


## Horizontal Rules

___

---

***


## Typographic replacements

Enable typographer option to see result.

(c) (C) (r) (R) (tm) (TM) (p) (P) +-

test.. test... test..... test?..... test!....

!!!!!! ???? ,,  -- ---

"Smartypants, double quotes" and 'single quotes'


## Emphasis

**This is bold text**

__This is bold text__

*This is italic text*

_This is italic text_

~~Strikethrough~~

Save the file, edit it again and you will now see the markdown syntax highlighted.

Happy coding !

Become a more social person