Updated: 4 November 2026 · Applies to: GNU nano 5.6.1 to 8.7.1; tested on Debian 13 (nano 8.4), Ubuntu 24.04 (7.2) and 26.04 (8.7.1), AlmaLinux 9 (5.6.1) and 10 (8.1), Fedora 44 (8.7.1)

Nano is a small, easy text editor that runs in the terminal, so you can edit files on a server over SSH. To delete a line in nano, put the cursor on it and press Ctrl+K (the line goes to the cut buffer, so Ctrl+U brings it back). Save with Ctrl+O and Enter (or Ctrl+S), and exit with Ctrl+X.

Install and open nano

Nano is the default editor on Debian and Ubuntu. Minimal images and containers often leave it out (our Debian 13 and AlmaLinux 9 test images had no nano), so install it if the command is missing:

sudo apt install nano        # Debian, Ubuntu
sudo dnf install nano        # AlmaLinux, Rocky Linux, RHEL, Fedora
nano file.txt                # open, or create if it does not exist
nano +25 file.txt            # open with the cursor on line 25
nano -l file.txt             # show line numbers
sudo nano /etc/hosts         # system files need root

The two bottom lines list the most used shortcuts. ^ means Ctrl and M- means Alt. According to nano’s built-in help, an M- key can also be typed with Esc (press Esc, then the letter), which helps on a Mac or in a browser console where Alt does not reach the server.

Save and exit

  • Ctrl+O: write the file. Nano shows the file name (“File Name to Write:” in 7.2, “Write to File:” in 8.7.1); press Enter to keep it.
  • Ctrl+S: save without the prompt.
  • Ctrl+X: exit. With unsaved changes nano asks “Save modified buffer?”: Y saves (then Enter), N discards, Ctrl+C goes back to editing.
  • Ctrl+G: the full help, including keys not shown at the bottom.

How to delete lines in nano

  1. One line: move to the line and press Ctrl+K.
  2. Several lines in a row: press Ctrl+K once per line. Lines cut one after another are kept together, so a single Ctrl+U pastes them all back.
  3. A block: press Alt+A to set the mark at the start, move with the arrow keys to select, then Ctrl+K.
  4. Delete without keeping a copy: Alt+Delete throws away the current line (or the marked text) and leaves the cut buffer as it was.
  5. Everything from the cursor to the end of the file: Alt+T.
  6. All text in the file: Alt+\ (first line), Alt+A (mark), Alt+/ (last line), then Ctrl+K.
  7. Undo a mistake: Alt+U (redo with Alt+E).

Smaller deletes: Backspace and Delete remove one character; Ctrl+Delete deletes the next word and Alt+Backspace the previous one.

Lab test on Ubuntu 24.04 (nano 7.2): a five-line file, cursor on line 2, Ctrl+K, then Ctrl+S. The status bar showed the line count and the file was one line shorter:

$ printf 'line 1\nline 2\nline 3\nline 4\nline 5\n' > t.txt
$ nano +2 t.txt              # Ctrl+K, Ctrl+S, Ctrl+X
[ Wrote 4 lines ]
$ cat t.txt
line 1
line 3
line 4
line 5

The same steps gave the same results in nano 5.6.1, 8.1, 8.4 and 8.7.1: three Ctrl+K presses removed lines 2 to 4 (and one Ctrl+U at the end of the file put all three back), Alt+T on line 2 left only line 1, the select-all sequence saved an empty file (“Wrote 0 lines”), and after Ctrl+K on line 1 and Alt+Delete on the next line, Ctrl+U still pasted line 1.

Cut, copy, paste and move a line

  • Ctrl+K: cut the line or the marked text.
  • Alt+6: copy the line or the marked text (the cursor moves to the next line).
  • Ctrl+U: paste at the cursor.
  • To move a line: Ctrl+K, go to the new place, Ctrl+U. The pasted line lands above the cursor line.

Search, replace and go to a line

  • Ctrl+W: search forward. It works in every version we tested. In nano 8.0 and later the bottom bar shows ^F Where Is instead, and Ctrl+W still works as the alternative key.
  • Alt+W: find the next match; Alt+Q finds the previous one. Nano 8.x also accepts Alt+F and Alt+B for these (in 7.2 those two keys start the formatter and the linter instead).
  • Ctrl+\ (or Alt+R): replace. Nano asks “Search (to replace)”, then “Replace with”, then “Replace this instance?”: press Y for this one or A for all. Replacing “line” with “row” in our test file reported “Replaced 5 occurrences”.
  • Ctrl+/ (shown as ^_ in older versions) or Alt+G: go to a line. Type the number at “Enter line number, column number:” and press Enter.
  • Alt+N: show or hide line numbers (nano 4.1 and later; very old guides say Alt+#).

Version differences to know

According to nano’s release notes, nano 8.0 changed two default keys: Ctrl+F now starts a forward search and Ctrl+B a backward search (in 7.2 and older they move the cursor one character). Nano 8.0 also added the option --modernbindings (-/), which makes Ctrl+Q quit, Ctrl+X cut, Ctrl+C copy, Ctrl+V paste and Ctrl+Z undo, for people used to desktop editors. Check your version with nano --version.

Settings in ~/.nanorc

Put your preferences in ~/.nanorc, for example:

set linenumbers
set autoindent
set tabsize 4
set tabstospaces
set mouse

Make nano the editor for commands such as crontab -e and visudo: sudo update-alternatives --config editor on Debian and Ubuntu (on our Debian 13 and Ubuntu 24.04 tests /bin/nano was already the selected editor), or select-editor on Ubuntu for your own user, or add export EDITOR=nano to ~/.bashrc.

Tip: before editing important configuration files, make a copy with cp -a file file.bak. Nano can also keep a backup automatically with set backup in ~/.nanorc.

Related: how to delete lines and text in Vim, connect to your server or VPS with PuTTY and basic Linux commands over SSH.

Official documentation: GNU nano manual.

Related services

KVM VPS hosting

NVMe SSD and full root access, with Free Basic Managed Support.

Unmanaged dedicated servers

full root access and free IPMI KVM, with Free Basic Managed Support.

Managed dedicated servers

our engineers look after the server for you, from $135 a month.

Was this answer helpful?

Still stuck? Open a ticket