Nano is the command-line text editor of choice for many Linux users, particularly those on Ubuntu, partially because its basic functions are quite intuitive to use.

Even so, there are a few things that trip new users up. We've already covered how to exit nano and Linux base 64 decoding, but today's topic is deleting a line in nano. This quick guide will cover:

  1. Deleting a line in nano
  2. How to delete multiple lines in nano
  3. How to select all and delete in nano
  4. How to undo deleted text
  5. Troubleshooting and common issues
  6. FAQs

Deleting a line in nano


Though it makes copious use of hotkeys, deleting a line in nano isn't that difficult. Here's the step-by-step process:

Step 1: Open the file


In Terminal, open the file you want to edit with:

nano yourfilename.txt

Step 2: Navigate to the line you want to delete


You can do this using the arrow keys to move line-by-line downwards. Alternatively, you can use the nano go to line function by typing Ctrl + Shift + _ and entering the line number. You don't need to be at the end of the line for this to work.

Step 3: Use the delete shortcut


Press Ctrl + K to cut the text and therefore delete the line in nano

That's it! You can now delete the empty space with a backspace if you wish, or delete other parts of your document.

Nano: Delete multiple lines with delete selection


If you want to delete many lines in a document, going through and pressing Ctrl + K on each is tedious. If you want to use nano to delete multiple lines, use its selection tools first.

Step 1: Open the file


To open the file you'd like to modify, type nano filename.txt in your terminal. You'll see nano's interactive editor.

Step 2: Move to the first line you want to select


Using your arrow keys, navigate to the first line of the block you'd like to select. You don't need to be at the end of the line.

Step 3: Set your mark and select


Press the Meta key (Alt/Command) + A to set your mark. Use the arrow keys to move up or down the document, with each line selected along the way.

Step 4: Delete the text


You can now delete the multiple lines selected in nano by pressing Ctrl + K and save and exit nano.

How to select all and delete in nano


Deleting all contents of a file in nano is much easier than some guides would have you think. Rather than messing around with selections, you can use the nano clear file shortcut.

Step 1: Open the file in nano


In Terminal, open the file you want to edit with:

nano filename.txt

Step 2: Press the "clear file" shortcut


With your cursor in its starting position at the beginning of the document, you can press Alt + T to delete all text in nano.

Note: If you're already midway down the document, you can use Alt + \ to quickly return to the beginning.

How to undo deleted text in nano


It's quite easy to make mistakes when deleting text with nano, but don't worry. While your usual Ctrl + Z won't work, there's another hotkey.

To recover deleted text in nano, press Alt + U to undo.

Troubleshooting and common issues

Ctrl + K isn't working in my terminal


This is usually because your terminal software, such as the VS Code terminal, is intercepting the key combination for its own shortcut. Try with a different terminal if possible, and if that works, check your original terminal's configuration files to rebind the relevant shortcut.

Nano editor disappeared and left a lock file


An easy mistake to make. Ctrl + Z in nano suspends the editor rather than undoing, but does not exit the process. As a result, users see a warning when they try to nano back in. The fix is to type fg to bring your active nano session back to the foreground.

My server/PC crashed, and now my file is stuck being edited


You can usually override this by just typing "Y" when asked if you still want to edit the document. However, if you're using the -G flag or have set locking enabled in your .nanorc file, you need to delete the .lock file associated with your document if you want to continue. To do so:

  • Run ls in your file's directory
  • Look for a file like .yourdocument.ext.swp
  • Run rm -f .yourdocument.ext.swp

You should now be able to edit your document as normal.

Need another Linux machine? Sign up to BitLaunch and launch a VPS in Amsterdam, Bucharest, or the USA within minutes. Launch servers programmatically, or use our control panel to select from various regions and VPS providers.

FAQs

How do I delete a line in Nano using PuTTY?

The same shortcuts apply in PuTTy as in nano — press Ctrl + K to delete a line. That said, PuTTY can sometimes intercept certain key combinations. If Alt + A won't set a mark, try pressing Esc then A instead, since PuTTY treats Esc followed by a key as the Meta/Alt equivalent.

How do I delete a Line in Nano using bash-cli?


If you need to delete certain lines using bash rather than interactively, you can use sed. For example, sed -i '5d' yourfilename.txt would delete line 5 from your file. Alternatively, you could delete a range with something like sed -i '3,7d' yourfilename.txt.

How can I display line numbers in nano?

Use the -l flag when opening a file, for example nano -l yourfilename.txt. Or, if you already have the file open, press Alt + N.

If you don't want to do this every time, you can add set linenumbers to your ~/.nanorc file.

How can I move to the end of the line in nano?

Press Ctrl + E or use the arrow keys to move your cursor. You can use Ctrl + A to jump back to the front of the line.

For more guidance on Linux terminal commands, you can check out these guides:

How to use the chown command recursively and non-recursively
The chown command allows users to change the owner of a file or directory to a specified user or group. Here’s how to use it.
Linux CLI: 5 Ways to Get File Size in Bytes
Knowing how to check file sizes in Linux is a key skill. It’s easy enough to find the info gigabytes or megabytes — this guide will instead cover retrieving file size in bytes.
How to Save in Vim
Vim is a powerful text editor, but its multiple modes can be intimidating. Here’s how to use it to save files.