How to Edit a File on Ubuntu using the Terminal

In this quick guide, we will explore how to edit a file on Ubuntu while using the terminal.

Ubuntu Edit Files using the terminal

There are various times that you will want to edit a file on Ubuntu while using the terminal. This is especially true if you connect to your device remotely using SSH.

Luckily for us, editing a file in the terminal is relatively straightforward, thanks to the pre-installed text editors on Ubuntu.

Best of all, the provided editors are straightforward to utilize, especially once you are familiar with the terminal.

In the following section, we will show you how to edit files using the nano text editor on Ubuntu. nano is one of the most straightforward text editors to utilize within the command line.

Editing a File on Ubuntu from the Terminal

The Ubuntu operating system comes pre-installed with two terminal-based text editors that you can use to edit files. These two editors are called “nano” and “vi“.

Both of these editors have pros and cons, but we will focus on nano as we feel like it is one of the easiest terminal-based editors to use.

We use the nano editor in almost all of our Ubuntu projects where we need to edit files within the command line.

Syntax for Editing Files using nano

Using nano to edit a file on the Ubuntu operating system is as simple as using “nano” followed by the path to the file you want to edit.

The syntax for this command can be written as simply as what is shown below. All you need to do is replace “PATHTOFILE” with the path to the file you intend to edit.


You can navigate a file that is open for editing with nano by using the arrow keys on your keyboard. To select text, click and drag over it. Any selected text will automatically be copied to the clipboard.

With nano editing a file on your Ubuntu system there are also a couple of keyboard shortcuts that you will want to know.

  • CTRL + O – This shortcut will allow you to save the file. You will be prompted to type in the filename. However, press ENTER to save the new contents to the existing file.
  • CTRL + X – You can also save and quit by using this key bind. With this shortcut, you will be asked if you want to save the file you edited on Ubuntu before it exits. Type “Y” to save or “N” to quit without saving.
  • CTRL + W – Using this key bind will allow you to search the file. Press this shortcut and type in the text you want to search the file for.
  • CTRL + K – Cuts the currently selected line and keeps it for pasting.
  • CTRL + U – Pastes whatever is in the clipboard to the current position.

Example of Using nano to Edit Files on Ubuntu

1. The first thing you will need to do to edit a file using the terminal on Ubuntu is to open the terminal.

If you use Ubuntu desktop, you can open the terminal easily by pressing CTRL + ALT + T on your keyboard.

2. With the terminal open, your next task is identifying the file you want to edit on your system.

For this example, we will edit the NGINX configuration file on our Ubuntu system, located at “/etc/nginx/nginx.conf“.

3. Once you know what file you want to modify, you can begin editing it by using “nano” followed by the path to the file.

Remember that if your user doesn’t have permission to edit this file you may need to use “sudo” at the start of the command.

So, if we were to begin editing our chosen file on Ubuntu, we would use the following command.

nano /etc/nginx/nginx.conf

If you want to use the vi editor, just type in “vi” instead of “nano“. We find that vi can be a bit intimidating for users unfamiliar with editing files within the terminal.

4. The nano text editor will immediately open the file you specified and show its contents.

You can now edit the file and make any changes that you require. We have a separate guide that covers how to use the nano text editor better to navigate and edit a file.

5. Once you are done editing the file on your Ubuntu system using nano you can save and quit.

To save and quit, you will want to press CTRL + X, type in Y, then press the ENTER key.


Hopefully, at this point in the guide, you will now understand how to edit a file on Ubuntu while using the terminal.

Editing a file within the terminal might initially feel daunting, but it is a very simple process. Terminal-based text editors like nano make this process even simpler.

If you have any questions about editing a file on Ubuntu, please feel free to comment below.

We have many other guides for the Ubuntu system, so you want to learn how best to use this operating system.

One Comment

  1. Avatar for Bulukka
    Bulukka on

    Thanks. Works very well.

Leave a Reply

Your email address will not be published. Required fields are marked *