9

How to delete the complete word where cursor is positioned in nano text editor? Or if cursor is on white space, I assume it should delete the next word?

Nano help shows these two functions but they are not bound to any shortcuts:

Cut backward from cursor to word start
Cut forward from cursor to next word start

Those don't appear to be what I'm looking for, but if nothing else is available, I'd like to know how to use them (especially with a shortcut key).

3 Answers 3

8

Save this file to ~/.nanorc and ctrl+] cuts the word to the left, and ctrl+\ cuts right

This works for me in nano version 2.5

bind    ^]          cutwordleft         main
bind    ^\\         cutwordright        main

This works for me in nano version 2.9.3

bind    ^]          cutwordleft         main
bind    ^\          cutwordright        main
5
  • I realize the right square bracket cutting left is silly, but the keybinding for control left square bracket was broken, in some way. Commented Feb 1, 2019 at 15:49
  • Isn't that because of the [ being an escape character? Commented Jan 2, 2020 at 15:20
  • if you can get [ to cut left and ] to cut right, please edit my answer or let me know what works for you and i'll change my answer to include it. thanks @MichałKapracki Commented Jan 3, 2020 at 17:30
  • 1
    Just a note, but in nano 6.0+ you have to use chopwordleft and chopwordright
    – Phil Cohen
    Commented Jan 17, 2022 at 20:53
  • 1
    What is nano's definition of a "word"? Now it see that it is not anything between spaces. Example: __xxx_yyy __ after ^] becomes __xxx_ (unexpected), instead of __xxx_yyy (expected). How to change the definition to be "anything between spaces"?
    – pmor
    Commented May 19, 2022 at 13:13
7

When it comes to editing text in Nano, I find these keyboard shortcuts are of great benefit:

  • Alt+M - Enable/Disable mouse support mode.
  • Ctrl+6- Switch on marking (mark set/unset)
  • Ctrl+A- Move to beginning of current line.
  • Ctrl+K- This shortcut has magical dual functionalities, it cuts (deletes) the whole line of selection when mouse support mode is disabled, and it cuts (deletes) the whole selected word/text when mouse support mode is enabled.
  • Ctrl+D- Delete the character under the cursor
  • Ctrl+H- Delete the character to the left of the cursor
  • Ctrl+U- Uncut from the cutbuffer into the current line.
  • Ctrl+space- Move forward one word. When mose support mode is enabled, it will select the words one by one with every hit on the keys.

If you would like to make edits at/to a specific word in the text, then you need to do the following:

  • Hit Alt+M to enable "mouse support mode".
  • Double click on the word of choice and this will switch on marking (mark set/unset). When you click on the word of choice and marking is set on, then select the word or the text of choice using the mouse (exactly like how you usually select text in any word editor e.g. gedit). Or, alternatively you can hit Ctrl+6 to enable marking, then Ctrl+space to select the word/text that you would like to edit.
  • Use Ctrl+H to delete the characters of that selected word, or alternatively use Ctrl+K to cut (delete) the selected word/text when mouse support mode is enabled.

In order to undo/redo actions, nano has experimental undo/redo feature. In nano manual:

   $man nano
. . . .

   -u (--undo)
          Enable experimental generic-purpose undo code. By default, the undo and redo shortcuts are Meta-U and Meta-E, respectively.
. . . .

To activate "undo/redo" mode, you’ll need to start nano with the -u option:

$ nano -u file.txt

… and then you can use Alt+U to undo and Alt+E to redo.

More details about the Nano's keyboard shortcuts

3
  • 1
    undo is alt+u while ctrl+u is for uncut
    – elig
    Commented Sep 17, 2018 at 22:42
  • 6
    What is the commend for deleting a whole word? That's my question.
    – MountainX
    Commented Sep 19, 2018 at 3:21
  • 2
    You didn't even answer the question Commented Oct 2, 2018 at 13:24
1

In later versions such as 7.2 onward, it is:

bind ^H chopwordleft main

If you'd like to also use ctrl+backspace as your whole word deletion shortcut (in addition to alt+backspace) (as it is in non-terminal text editors in linux)

You must log in to answer this question.

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