Can I/how to specify colors in hex or RGB in nano syntax highlight config?

I enabled syntax highlight in nano (PHP), but not happy with the default, I would like for example to have the comments displayed in very light grey.

However, the documentation I found seems to suggest I can only write colors like “yellow”, “red” etc.

Is there a way to specify a color by its hex/RGB code?
Is there a limitation in the number of colors bash/nano and so on can display? Obviously I am not very experienced with the Linux world.

Same question for VIM, I might switch to VIM if that is not possible.

Asked By: bg17aw

||

nano is small. In this case, it limits the choices to the 8 predefined ANSI colors (plus bright/bold) so that it can use the predefined symbols from curses.h (such as COLOR_BLUE) as a guide to naming.

Many terminals support 256 predefined colors; nano can’t take advantage of them, but Vim can.

Terminals which allow directly specifying the R/G/B content of a color are an exception rather than a rule—unlike GUIs.

Some terminals (including Xterm, which I maintain) support the escape sequence e]4;N;#RRGGBBa to change palette color N to the specified RGB value, and e[38;2;R;G;Bm to set the foreground color to the closest approximation in the palette of the specified RGB value (use 48 instead of 38 for the background color). However, changing a palette color is not useful for nano, because it is taking advantage of the existing palette, in contrast with (the much larger) Vim, which can do this with an add-on.

On writing the above in December 2015, the most recent release of nano was version 2.4.2 (July 2015). At that point, nano was 23336 lines (7657 statements) in C, which was a small fraction of the 131621 lines of text files (counting the ".po" message files). At the moment (October 2021, six years later), the program size is about the same size (fewer lines, more statements), but the other text files have roughly doubled the size of its source-tree (253036 lines). It’s not exactly "small" any longer (but nowhere near the size of vim). A couple of weeks before releasing nano 5.0 in July 2020, the developer added eight names for entries in xterm’s 256-color palette, in src/rcfile.c:

const char hues[COLORCOUNT][8] = { "red", "green", "blue",
                                   "yellow", "cyan", "magenta",
                                   "white", "black", "normal",
                                   "pink", "purple", "mauve",
                                   "lagoon", "mint", "lime",
                                   "peach", "orange", "latte",
                                   "grey", "gray" };

short indices[COLORCOUNT] = { COLOR_RED, COLOR_GREEN, COLOR_BLUE,
                              COLOR_YELLOW, COLOR_CYAN, COLOR_MAGENTA,
                              COLOR_WHITE, COLOR_BLACK, THE_DEFAULT,
                              204, 163, 134, 38, 48, 148, 215, 208, 137,
                              COLOR_BLACK + 8, COLOR_BLACK + 8 };

That doesn’t appear to be extensible (but at least it uses ncurses). However, it does not address OP’s question because it does not provide a hex or RGB method for configuring nano. In developing ncurses, I created an example which reads the X11 rgb.txt file, as part of making the program display X pixmap files in color. But it also reads and displays using a data file for xterm’s 256-color palette. For screenshots, see the discussion of the picsmap program.

Answered By: Thomas Dickey

Some six years after this question was asked, a version of nano with this feature was released:

https://9to5linux.com/gnu-nano-6-0-text-editor-is-out-with-new-color-names-suspension-enabled-by-default

Answered By: John Horner
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.