edit file ubuntu

As you get started with Linux, you’ll inevitably have to edit config files at some point, whether it’s to change your device name, configure networking, or whatever else.

You have numerous options to choose from for this matter. There are powerful text editors like vim or emacs but they have a significant learning curve. Editors like kate or nano are better suited for beginners.

As nano comes preinstalled and is most commonly used in Ubuntu, we recommend nano for new users. We’ll explain how you can edit files with it in this article.

Opening a File with Nano

The basic syntax for opening a file to edit with nano is

nano /path/to/file

If you need root privileges to edit this file, you can simply prefix the command with sudo like so

sudo nano ~/Desktop/intro.txt

In the above example, if the intro.txt file doesn’t exist yet, the command will create a text file by that name.

Editing Files with Nano

As nano emulates the Pico text editor, two of its main selling points are simplicity and ease of use. The most commonly used shortcuts are conveniently listed at the bottom.

Control-key sequences can be entered by pressing Ctrl (e.g., Ctrl + O to save changes) or by entering Esc twice (e.g., Esc + Esc + O).

Meta-key sequences can be entered using Alt or Cmd (e.g., Alt + U to undo the last change).

The two most important shortcuts are to:

  • Write the current buffer, i.e. save the changes (Ctrl + O)
  • Close the current buffer, i.e., exit nano (Ctrl + X)

Ctrl + S works for saving too, but it’ll save the file without a confirmation prompt. And if you try to exit without writing out the changes, you’ll be prompted to save the modified buffers so that the edits you made aren’t accidentally lost.

Useful Nano Shortcuts

You could definitely get by with just the shortcuts mentioned above. But if you’re going to learn how to use nano, you might as well learn to use it efficiently. 

You can press Ctrl + G to read the full help text, but for now, here are some useful shortcuts:

  • Mark (Alt + A)
    Mark text starting from the cursor position. You can also use Shift + Arrow keys or your mouse to make a selection.
  • Copy (Alt + 6)
    Copy the current line or the selected text and store it in the cut buffer.  
  • Cut (Ctrl + K)
    Cut the current line or the selected text and store it in the cut buffer.
  • Paste (Ctrl + U)
    Paste the contents of the cut buffer at the current cursor position.
  • Undo (Alt + U)
    Undo the last operation.
  • Redo (Alt + E)
    Redo the last operation.
  • WhereIs (Ctrl + W)
    Search forward for a string or a regular expression.
  • WhereWas (Ctrl + Q)
    Search backward for a string or a regular expression.
  • Replace (Ctrl + \)
    Replace a string or a regular expression.
  • Read File (Ctrl + R)
    Read another file and insert it into the current buffer (append the contents).
  • Execute (Ctrl + T)
    Execute a function or an external command (e.g., ls).
  • Go To Line (Ctrl + /)
    Go to line and column number.
  • Count (Alt + D)
    Count the number of lines, words, and characters.
Anup Thapa

Senior Writer

Anup Thapa is a Linux enthusiast with an extensive background in computer hardware and networking. His goal is to effectively communicate technical concepts in a simplified form understandable by new Linux users. To this end, he mainly writes beginner-friendly tutorials and troubleshooting guides. Outside of work, he enjoys reading up on a range of topics, traveling, working out, and MOBAs.