202

I use the commmand line text editor nano. How do I undo an action while editing text?

For example: I typed some text and then changed my mind. Now I want to return to previous state. How?

1
  • You may want to consider changing the accepted answer here, as there is a newer, more accurate one.
    – Scimonster
    Jul 6, 2017 at 9:09

3 Answers 3

242
+50

Starting with nano version 2.3.5 in July 2014, undo/redo became standard:

  • Alt-U or Option-U or EscU - undo
  • Alt-E or Option-E or EscE - redo

It's in the help:

enter image description here

And if you stretch the screen wider than about 1400 pixels, there's a hint at bottom right:

enter image description here

If these keys don't work, check nano --version.

  • nano versions 2.1.10 - 2.3.4 should use the command line option -u (thanks @sil). Tip: add alias nano="nano -u" to your .bashrc (thanks @Benia). The implementation of undo during this period appears to have been buggy or incomplete.

  • nano versions 2.1.3 - 2.1.9 had some kind of undo feature. The -u option was not necessary.

M stands for meta.

21
  • 4
    Clicking alt+u doesn't work for me to undo.
    – user423047
    Oct 5, 2016 at 8:07
  • 7
    @Benia try using Esc key instead of Alt. Let me know if that helps Oct 5, 2016 at 8:13
  • 7
    I came here to know what does that M stand for
    – Amr Saber
    Oct 26, 2018 at 21:30
  • 5
    But I wonder why it's shown as M-U or M-E on nano. What's that M?
    – Zolbayar
    Oct 5, 2019 at 23:26
  • 6
    @Zolbayar also added to answer: M stands for meta.
    – Bob Stein
    Oct 5, 2019 at 23:44
39

Nano's undo code is experimental. As you'll see from the nano manual (type "man nano" in a Terminal to read that), you'll need to start nano with the -u option (so "nano -u somefile.txt"), and then you can use Alt-U to undo.

2
  • 10
    Thanks. There is also redo functionality and the shortcut for that is Alt+E.
    – user44
    Aug 15, 2013 at 14:25
  • 3
    You can edit /etc/nanorc to set it permanently (uncomment set undo). Note that under Mac you need to use esc|then|u to undo.
    – ishahak
    Jul 16, 2019 at 7:10
8

You can also add the line

set undo

in the .nanorc file. This enables the experimental undo feature on startup in versions below 2.4, such as 2.2.6 which seems to be the standard installation on many Linux distros.

0

You must log in to answer this question.

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