1

When using vi and long lines, the text are wrap but vi doesn't insert a CR/LF at the end of the line. With nano, if I start typing and the line becomes too long, it will insert CR/LF at the end of the line.

How can I make nano behave like vi when it becomes to this feature?

2

Both nano and (most implementations of) vi have an optional line wrapping feature, where they replace the previous space by a line break¹ when the line becomes longer than a certain length. They behave differently when the cursor isn't at the end of the line, but it's fundamentally the same feature.

To disable this feature in nano, press Alt+L. In nvi, set wraplen and wrapmagin to 0 (:set wl=0 wm=0).

Vim also has a number of more advanced wrapping features which nano doesn't have an equivalent for.

If a line is too wide for the terminal, vi shows it wrapped by the terminal. Nano doesn't, you need to scroll left and right. If that's what you meant by “act like vi”, you're out of luck.

¹ Not a CR/LF. CR/LF is the encoding of line breaks on Windows. On Unix, a line break is encoded as LF.

  • yes, I meant the scrolling left/right to see the contents if the line is too long for the terminal window. On vi, it wraps around (but no line breaks). – mrjayviper Sep 6 '17 at 0:49
1

https://askubuntu.com/questions/13317/how-to-stop-gedit-gvim-vim-nano-from-adding-end-of-file-newline-char

answered by @Marco Ceppi:

Nano Install Nano is a little easier. You can launch nano with the -L flag (or --nonewlines if you're more the visual type). This will disable the automatic addition of the newline to the end of the file (so long as one was not actually entered). Ideally you could setup this alias in your ~/.bashrc: alias nanon=nano -L which would launch Nano with the no newlines flag included in it.

1

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

set nowrap

And uncomment line

set softwrap
  • Note: This only works for nano 2 and not nano 3. Since nano 2, set nowrap no longer disables all wrapping, only hardwrapping, which is what the asker wants. And nano 3 will ignore set softwrap and continue to hardwrap if set nowrap isn't added. So add both set nowrap and set softwrap for textwrapping that doesn't break your files. – x0a Dec 29 '18 at 4:56
0

Defaults can be set in ~/.nanorc. But the configuration of nano 1, nano 2, and nano 3 are all slightly different.

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

On nano 2, set softwrap enables softwrapping and disables hardwrapping. set nowrap no longer disables all wrapping, just hardwrapping.

On nano 3, set softwrap is ignored and nano continues to hardwrap unless you also set 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 2 lines to ~/.nanorc

set nowrap
set softwrap

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.