Delete word before and after the cursor in nano editor

7,050

Solution 1

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

bind    ^]          cutwordleft         main
bind    ^\\         cutwordright        main

This works for me in nano version 2.5

Solution 2

There's no direct way to do this in nano, but there are some key combinations that can accomplish what you want.

One useful tip to start is to open nano using the -W option, to make word boundaries work in a more sensible manner.

-W, --wordbounds
Detect word boundaries more accurately by treating punctuation characters as parts of words.

Delete the word before the current position of the cursor

  1. Switch on marking - Ctrl+6
  2. Move forward one word - Ctrl+Space
  3. Cut (delete) the selection - Ctrl+K

Delete the word after the current position of the cursor

  1. Switch on marking - Ctrl+6

  2. Move backward one word - Alt+Space

  3. Cut (delete) the selection - Ctrl+K

Delete from the current cursor position to the beginning of the line

  1. Switch on marking - Ctrl+6

  2. Move to beginning of current line - Ctrl+A

  3. Cut (delete) the selection - Ctrl+K

These command will work as described in a normal terminal, though the Alt key is swallowed by some editors (if I've connected through Windows cmd by ssh). You can use Esc to replace Alt.

Share:
7,050

Related videos on Youtube

Hendrik
Author by

Hendrik

Updated on September 18, 2022

Comments

  • Hendrik
    Hendrik over 1 year

    How can I delete the word before and the word after the current position of the cursor in nano editor? (For the sake of clarity: I mean two distinct editions under this description, like the Ctrl-Backspace and Ctrl-Delete in Windows).

    And how can I delete the text between the beginning of the current line and the position of the cursor?

  • KingKaitoKid
    KingKaitoKid over 5 years
    later version of nano allow users to bind the cutwordleft and cutwordright functions to keyboard shortcuts using ~/.nanorc
  • KingKaitoKid
    KingKaitoKid over 4 years
    the most recent version of nano broke because now ctrl-\ is used to search and replace. thanks nano.