Skip to main content
Basics

Edit Text Files in Ubuntu Command Line

Stuck in the terminal and need to edit a text file? Here's what you need to know about editing files in the Ubuntu command line.

Pratham Patel

Editing files is easy with a regular GUI text editor like Notepad, VS Code, etc.

But when you are stuck to the Linux terminal and have to edit files in the command line, things are not the same. You don't have the comfort of the GUI here and you have to use the editor with keyboard shortcuts.

Sounds scary, right? Unfortunately, you cannot avoid it. You have to edit files using command line text editors.

There are several such text editors available for Linux. Vim, Emacs, you name it.

Ubuntu being a beginner-friendly distribution comes preinstalled with a beginner-friendly text editor called Nano.

You can create a new file or open an existing one for editing in the Ubuntu command line like this:

nano FILENAME

Nano is a terminal based text editor which is simple and easy to use. Replace 'FILENAME' with the name (or location) of your file.

Using the nano editor

Among all the terminal based text editors I know, nano is the easiest to use for beginners.

The syntax to open a file to edit in nano is as follows:

nano FILENAME

Running the nano command, along with a specified filename will open that particular file in the nano editor.

Opening a file with Nano

Let's assume that I have a file named 'demo-file.txt' in my current directory. To edit it using nano, I would run the following command:

nano demo-file.txt

As you can see in the attached GIF, I opened 'demo-file.txt' in nano.

Editing the opened file with Nano

The interface of nano is quite straightforward.

You can move the cursor left, right, up down using the respective arrow keys. Text can be selected using the normal click-and-drag mouse operation.

To cut a line, press the Ctrl + k key combination, and to paste, press the Ctrl + u key combination.

Here is a graphical demonstration where I edit the previously opened 'demo-file.txt' file in nano.

As you can see, this is quite effortless and easy to use!

Save and close files

The normal convention is to use Ctrl + s key combination to save your work, but in Nano, the keyboard shortcut is Ctrl + o.

Pressing Ctrl + o will ask you the name of the file you wish to save it as. This is essentially the "Save As" operation you see in most text editors. In case you don't want to change the name or path of the file, simply press 'Enter'. That will save your edited file.

In case you want to save and quit, use the Ctrl + x key combination. Pressing it will prompt you "Save modified buffer?" since we want to save it, press 'Y'. The file name will be confirmed, leave it unchanged and press 'Enter'. That will save your changes to the file and close Nano.

Here is a demonstration of how both of these above-discussed functions work in Nano:

💡
Nano shows the useful keyboard shortcuts at the bottom all the time. If it says ^K for Cut, use Ctrl+K (^ means Ctrl key). Similarly, if it says M-A for Append, use Alt + M (M means Alt key).

Bonus tip: Editing system files in Ubuntu

When it comes to editing system files, like /etc/ssh/sshd, it is best not to use the sudo nano command for it.

Using sudo command with an editor runs the editor as root, as expected. But this has a few security issues. Your editor has access to everything now... This isn't a problem on maintainer packaged software, but still makes me (and several others) uneasy.

The solution to this situation is to use the sudoedit command.

Running sudoedit command on the other hand will create a temporary file in /tmp and edit that as a normal user. Once you save it, the original file will be replaced by the temporary file.

Pretty nifty, if you ask me!

All that needs to be done to use the sudoedit command is to have a shell variable EDITOR defined. You may do so with the following command:

export EDITOR=$(which YOUR_FAV_EDITOR)

Once the EDITOR variable (case sensitive) is defined with the editor of your choice, you may start using the sudoedit command.

Below, I show using nvim and nano as editors for sudoedit command:

I hope you now know how to edit files in the Ubuntu command line. You may not realize it at the moment but Nano is a lot easier to use than the likes of Vim and Emacs.

If you want to learn more, here's a free course for Nano beginners.

Not satisfied with Nano? You can try something else. Here are a few recommendations.

9 Best Text Editors for the Linux Command Line
Looking to edit files in the command line? These are the best editors for Linux terminal when you go all command line.
Pratham Patel