Ubuntu – How to set the global nano background to black

command linenano

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?

Best Answer

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.

Related Question