136

When editing an authorised_keys file in Nano, I want to wrap long lines so that I can see the end of the lines (i.e tell whose key it is). Essentially I want it to look like the output of cat authorised_keys

So, I hit Esc + L which is the meta key for enabling long line wrapping on my platform and I see the message to say long line wrapping has been enabled but the lines do not wrap as I expect.

I'm using Terminal on OSX 10.8.5

8 Answers 8

186

To see the word wrapping style you described, use nano's "soft wrapping": Esc+$.

The Esc+L command you (and everyone) tried does "hard wrapping."

Note on keystroke notation - if you are new to Linux, the notation Esc+$ means press and release Esc and then press $. The full key press sequence then is Esc, Shift+4.

(It does not mean hold down escape while pressing $.)

Source: https://www.nano-editor.org/dist/v2.9/nano.html (search for --softwrap)


Note on softwrap and formatting mistakes - If you are new to nano, be a little careful of softwrap. If you are editing a configuration file or something else that is sensitive to newlines or indents, formatting mistakes can be made. Until you get comfortable with softwrap’s behaviors, I suggest doing a quick check with softwrap off (do the key sequence again) before saving.

Note on the goodness provided by others in their answers below - because different operating systems and different versions of nano do things a little differently:

  • If you like softwrap on all of the time, set it in your .nanorc, as described in x0a's answer below, as it is a bit more through than Prashant's.
  • If you have a Raspberry Pi, note chainsawmascara's answer about needing an extra keystroke for softwrap to go into effect.
  • If you have a Mac, like lodeOfCode's answer below, you can always update nano and here, and thus bask in the warm glow of softwrap!

5
  • 3
    This was my biggest pet peeve in nano. Thanks for sorting this out.
    – user208145
    Mar 10, 2017 at 5:36
  • 2
    I would recommend enabling line numbers Alt + Shift + 3 or set linenumbers in ~/.nanorc settings file. Jan 31, 2019 at 20:31
  • 1
    Note that older version of nano do not include this soft wrap feature. On version 2.0.9 it is not included, but on version 2.5.3 it is. If you are on a version that does not have this feature it will give a warning message of "Unknown Command" at the bottom center of the text edit area. Mar 6, 2019 at 13:54
  • 1
    Pressing escape before a key is the same thing as holding alt while typing it (in terminals), so you can just press Alt+Shift+4
    – 12Me21
    Nov 4, 2020 at 17:34
  • 2
    ESC + $ says 'Unknown command' on iTerm on OS X.
    – joachim
    Jul 27, 2021 at 6:29
25

I'm reading the replies here and need to set this straight!

Nano supports two different forms of line wrapping, and it can be essential to know the difference!

First, the Meta key below is often the Alt key, but not necessarily.

  • Soft line wrapping is activated with Meta-$. Wraps lines without inserting line break characters into the file. That is, the effect is purely visual.
  • Hard line wrapping is activated with Meta-L. Wraps lines by inserting line breaks into the file. The file is physically changed.

So if you activate it in a file where wraps are meaningful (configuration files, programming languages, scripts), you basically never want the hard line wraps or it'll change the meaning of the code.

Also, you'd have found these shortcuts with ^G (i.e. Ctrl-G) in Nano.

2
  • 7
    Meta $ = Alt+Shift+4 Apr 25, 2017 at 1:13
  • 1
    Soft wrapping is enabled in nanorc with set softwrap, while hard wrapping is disabled in nanorc with set nowrap.
    – mwfearnley
    Feb 20, 2019 at 13:36
18

Open the nanorc generally at ~/.nanorc or /etc/nanorc, comment out the line

set nowrap

And uncomment line

set softwrap
2
  • 1
    On pfSense (and possibly FreeBSD more generally), the file needed is /usr/local/etc/nanorc. Failing that, I guess find / -type d -name etc will find other possible locations.
    – mwfearnley
    Jun 3, 2019 at 9:57
  • 1
    There was no line set nowrap. Its mention is misleading.
    – Asclepius
    Jul 25, 2020 at 22:15
17

You can set the defaults in ~/.nanorc. But keep in mind the configuration settings for nano 1, nano 2, and nano 3 are different.

On nano 1, you only have the set nowrap setting which disables all wrapping.

On nano 2, set softwrap was added, which enables softwrapping and disables hardwrapping. set nowrap was changed so that it only disables hardwrapping

On nano 3, set softwrap is ignored and nano continues to hardwrap unless you also set nowrap.

So if you want text wrapping for long lines that doesn't break your configuration files (softwrapping only), this works for versions 2 and 3: Add the following 2 lines to ~/.nanorc

set nowrap
set softwrap
1
  • 1
    This is a thoughtful answer as well, and I am referencing it from my answer above.
    – coreyt
    Jun 3, 2019 at 15:31
14

Just searched for nano line wrapping and this came high in results, so I'll post my findings for GNU nano 2.2.6 on the Raspberry Pi, Raspbian GNU/Linux 7: Esc+L gave me the same message; but for the line wrapping to take effect I had to modify the line. As soon as I typed another character on the long line, wrapping kicked in.

7

Setting aliases for your nano editor also work well.
I normally use:

 alias  e='nano -\$cwS'
 alias se='sudo nano -\$cwS'

Add the aliases to the end of either your ~/.bashrc, ~/.bash_aliases, or your favorite shell startup file.

Usage:

e [filename]

or sudo version:

se [filename]

Flags/Options used:

\$ = Enable soft-wrapping of lines (escaped using backslash)
c = Show constant cursor position (at bottom)
w = Disable any 'hard wrapping'
S = Smooth scrolling

And (as already mentioned) once in editor, use Esc, $ to toggle soft-wrapping ON/OFF.

6

Mac users be advised the version of Nano shipped with High Sierra (v2.0.6) or earlier doesn't support soft line wrapping as described here. You'll get an unknown command error. v2.2 is the earliest version with mention of soft wrap in the man pages. So don't spend an hour trying all these solutions like I did.

Esc+L does work as expected.

2

To only do this when you want to, you can use:

nano -$ file.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .