172

I can select all file by going to the 1st line Alt+\, start marking the text by Alt+A, go to the last line by Alt+/

But there, I don't know which key to remove the selected text. Hit delete doesn't work for me but Ctrl+K to cut the text will destroy my clipboard.

So, what is the hotkey to delete selected text?

user1251007
  • 1,131
Nam G VU
  • 2,378

13 Answers13

205

nano of course can delete blocks, see this article

  1. use CTRL+Shift+6 to mark the beginning of your block
  2. move cursor with arrow keys to end of your block, the text will be highlighted.
  3. use CTRL+K to cut/delete block.

To paste the block to another place, move cursor to the position and the use CTRL+U. You can paste the block as often as you want to.

Videonauth
  • 33,815
Thomas Klein
  • 2,067
52

nano does not support deleting a block of text, only cutting it (to the server's clipboard).

Instead, if you are using Putty, do the following:

  1. Select the text you wish to copy to the clipboard with the mouse first -- this copies it to your local clipboard (i.e. Windows 7 clipboard), which nano can't touch:

    enter image description here

  2. Then, select your block in nano and use Ctrl-K to delete it.

  3. Finally, move your cursor to the position where you want to insert the text you copied in Step 1 (you can close nano, open another file, etc. too as long as you don't select another block of text with the mouse). Right-click to paste the copied text at the cursor position.

ish
  • 141,990
25

If you are trying to empty all of the lines.

A pretty elegant and simplest approach is from the bash-cli:

  • echo "" > filename.txt
  • nano filename.txt
morgz
  • 351
14
CTRL SHIFT K

deletes current line, It can also be useful for quick editing. Thanks

Edit1:

In some of the putty streams, the following works too

CTRL K
th3pirat3
  • 241
11

In Nano

  • Ctrl+6 to set a mark
  • Alt+T will delete all content in a file
  • Ctrl+K will delete the current line at your cursor.
zx485
  • 2,865
Akhil S
  • 211
6

To delete multiple block without overriding your clipboard, first mark the text with Alt+A (You will appear Mark Set then select the text using arrow keys) and then use Ctrl+Shift+K to delete selected text,deleted text won't appear in clipboard.

6

nano >= 3.2

This adds support for the Alt+Del keyboard shortcut, which deletes text without Cutting it.

nano >= 5.0

You can delete the selected text by piping it through a command with no output: using Ctrl+T, and running | true (or |: if you really want to cut down on keystrokes)

nano >= 2.9.8

As with nano 5.0, but you need to use Ctrl+R, Ctrl+X instead of Ctrl+T.

Old answer

Sadly, nano doesn't seem to have any way of bulk-deleting without clobbering the clipboard.

The safest thing to do while staying within the document is probably to paste your clipboard before deleting, then re-cut it again afterwards.

If you have a block of text already selected, then Ctrl+U will paste the clipboard text, including it in your selected block. You can then unmark the pasted text and just cut your originally selected block.

These steps don't preserve your clipboard, exactly, but at least effectively perform a swap between your selection and the clipboard, allowing you to re-cut the lines you had in there before.

mwfearnley
  • 3,497
3

As pointed out by @saagarjha around version 3.2 of nano they introduced a zap command which allows you to delete text without affecting the cutbuffer.

M-Del Throw away the current line (or marked region)

To delete a line by itself use Alt+Delete with the cursor on the line you want to delete.

To delete a block of text, first set the starting mark with Alt+a and select the text to be deleted and finally press Alt+Delete.

To undo a delete, press Alt+u. To redo a delete, press Alt+e. For me redo didn't work until I disabled alt menu access keys in the preferences of my terminal emulator.

Yetoo
  • 35
Lenny
  • 39
2

Recent versions of nano (3.2+) now have this feature if you enable the zap option. Put set zap in your nanorc (or start nano with -Z/--zap), select the text your want to delete, and hit to delete your selection without affecting the cutbuffer.

saagarjha
  • 121
  • 3
2

You could open up a separate buffer to stash your clipboard in:

  • Make sure "reading into separate buffer" is enabled (you can toggle this with Alt+F)

  • Use Ctrl+R and press Enter (without entering a filename), to open a blank file buffer

  • Paste your current clipboard in there with Ctrl+U

  • Use Alt+< or Alt+> to switch between open buffers

mwfearnley
  • 3,497
0

For me (on a mac) Control + K did what I wanted (deleted a full line)

SudoPlz
  • 101
0

Ctrl+6 (Sets the mark position)
Ctrl+- (Enter the lines nos to delete)
Ctrl+K (Delete the selection)

Paul
  • 4,606
0

Ctrl + T (opens Command to execute tab)

Ctrl + V (Cut Till End)

Tested on Ubuntu Server