Search code examples
macosterminalnano

Bold text and change color of text for MOTD in nano on OSX?


I'm modifying my MOTD in Terminal via "$ sudo nano /etc/motd", and I'm wondering if there's a way to bold text and/or change its colour. I've checked the help documentation within nano, which would usually lead me to believe such formatting to be impossible – but I've found evidence of Linux users being able to change colors in their MOTD's via nano.

Any help is appreciated.


Solution

  • The /etc/motd file is simply cat'd to the terminal, and more than likely the terminal supports ANSI color escape sequences. You can embed that using (not quite any) many text editors. However the essential part is inserting (and keeping track) of the escape character (usually entered as a control[ on the keyboard). According to nano's documentation,

    All keys, with the exception of Control and Meta key sequences, will enter text into the file being edited.

    That exception seems to apply to the escape character. Reading further, it says that any character can be entered by typing escape twice, then entering its decimal value (27 for escape). But that does not work for the machine where I am typing. Perhaps it works for you, perhaps not.

    You could work around that by reserving some relatively useless character (such as #) to act as the escape and then using tr to translate to/from, e.g.,

    tr '\033' '#' /etc/motd >/tmp/motd
    nano /tmp/motd
    tr '#' '\033' /tmp/motd >/etc/motd
    

    Doing that, bold text would be something like

    #[1mBOLD#[0m
    

    (\033 is the escape character, of course - some people prefer \x1b).