5

I am using GNU nano version 4.8 on Ubuntu 20.04 LTS.

I have X11 set up to use white as the background colour of the xterm window. I don't want to change this setting. It looks like nano inherits this, and use white as its global background colour.

The default syntax highlighting for PHP (and other languages) seems to assume that the background colour in the nano editor is some dark colour. For instance these line from php.nanorc:

# Function names.
color white "\<[A-Za-z_][A-Za-z_0-9]*\("

makes function names white, which of course is unreadable on a white backround.

Changing it to this;

# Function names.
color white,black "\<[A-Za-z_][A-Za-z_0-9]*\("

improves things a bit. It now makes the function name, but only the function name, display with a white font on a black background. But it also looks ugly, as everything else has a white background.

Is there a way to change the background in the nano edit to not re-use the terminal default colour, but black instead?

2

Here is a solution I came up with while looking around. We can use escape sequences to change terminal colors dynamically.

enter image description here

To do that we can create a function named nnano for example and store it somewhere like ~/.bashrc so it get sourced automatically when we open a new instance of bash.

Then to use it:

nnano filename

It first changes the background color of terminal to black then when we close the nano it changes it to white.

Here is the function which can be improved a lot more:

nnano () {

    # Change the terminal foreground to #FFFFFF
    echo -ne "\033]10;#FFFFFF\007"

    # Change the terminal background to #000000
    echo -ne "\033]11;#000000\007"

    nano "$1"

    # Reset the colors
    echo -ne "\033]10;#000\007"
    echo -ne "\033]11;#FFF\007"

}

If you don't flip the foreground color:

enter image description here

Idea comes from here.

5
  • This one works. It is slow as molasses, 'tho'. Firing up nano used to be instantaneous. Now it takes about 6 seconds before the editor window is populated, and after pressing ^X, it also takes 6 seconds before I see the shell prompt again. Since it works, I've upvoted it. And I'll accept in a couple of days if nothing better comes along. May 3 '20 at 10:31
  • I'm not sure what might be the cause of problem because it's working without any lag for me... check out the updated demonstration.
    – Ravexina
    May 3 '20 at 11:59
  • The lag must come from my terminal emulator: starnet.com/xwin32 It must for some reason be slow to change colour. Btw, I don't think there is any need to flip the foreground colour, since foreground is controlled by nano syntax highlighting colours. May 3 '20 at 12:14
  • I guess we should flip that too... See the update.
    – Ravexina
    May 3 '20 at 12:17
  • I guess the need to flip the foreground depends on the configuration. I use the nano default, and it looks like it never uses a foreground inherited from the terminal.I understand you've rolled your own, and that it may be different in this respect. May 3 '20 at 12:29
1

It's not what exactly you want (A dark/black background). I'm not even sure if that thing is possible to do. Because as you said it yourself nano inherits its colors from xterm. Although you might be able to run xterm using different configuration when you are planing to run nano.

However I believe if you create a .Xresources file containing a complete set of colors, then you shouldn't have any problem.

Here is mine. The first part is for urxvt to make it look a little bit better. You can just ignore these lines. Pay attention to the last part of the file which is specifying the colors.

I just changed the background and foreground colors to create something visually similar to what you want.

enter image description here

keep in mind that my colors have been configured to use a dark background. You can change them to get a really better output. For example after changing yellow color to black:

enter image description here

Don't forget to run xrdb -merge ~/.Xresources after editing the '~/Xresources` file.

URxvt*termName:                 rxvt-256color
URxvt*font:                     xft:Menlo:pixelsize=12,xft:Inconsolata\ for\ Powerline:pixelsize=12
URxvt.depth:                    0
URxvt*lineSpace:                1
URxvt.letterSpace:              -1
URxvt.iso14755:                 false
URxvt.iso14755_52:              false
URxvt*geometry:                 90x20
URxvt.scrollBar:                false
URxvt*loginShell:               true
URxvt.internalBorder:           20
URxvt.perl-ext-common:          default,clipboard
URxvt.keysym.Shift-Control-C:   perl:clipboard:copy
URxvt.keysym.Shift-Control-V:   perl:clipboard:paste
URxvt.clipboard.autocopy:       true


! Colors
background:   #FFF
foreground:   #000
*cursorColor:  #9aedfe

*color0:      #33303b
*color8:      #4f4b58
*color1:      #ff5c57
*color9:      #ff5c57
*color2:      #5af78e
*color10:     #5af78e
*color3:      #f3f99d
*color11:     #f3f99d
*color4:      #57c7ff
*color12:     #57c7ff
*color5:      #ff6ac1
*color13:     #ff6ac1
*color6:      #9aedfe
*color14:     #9aedfe
*color7:      #eff0eb
*color15:     #eff0eb

Here is a shot from xterm:

enter image description here

1
  • 1
    As noted, this not what I am looking for. I understand that I can change the colours around to better fit a white background. However, it looks like the nano syntax highlighting defaults are well designed, provided the background is dark, so I want to go with that. To make clear: The question is how to get a dark background for nano in this situation. Not how to adapt nano syntax highlighting to a white background. May 3 '20 at 10:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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