How to edit files on Linux?

Share on:

Windows and Mac based systems provide some easy and simple text editors to do the job. Some Debian and Ubuntu based Linux systems do provide visual text editors which you can use, but since Linux is all about commands, one should essentially know how to edit files using command line. It is useful for the long run.

Here, In this article, I am going to explain how can you create, or edit a file on Linux based systems. We will discuss two generally used methods on a Linux system- Using command line editors like Vim and using a third party text editor like ‘Emacs’.

I will be using a Linux Mint System to explain the steps, however, the commands and interfaces are almost similar on other Linux systems like Ubuntu, and Fedora.

The steps will follow the following sequence:

  • Open the Terminal
  • Select a Text editor program
  • Navigate to the directory where we want to create or edit a file
  • Create or edit the file

Step 1: Open the Terminal:

In Linux Mint, you will find the terminal in All applications>>Accessories section of the Menu.

Open the Terminal in Linux

Some fundamental Command Line Navigation lessons: 

When you open the terminal, you will be in your home directory. The home directory is represented by ‘~’ symbol.

  • To list all the files in your current directory use the command ‘ls’.
  • To Navigate to any other directory use the command ‘cd dir_name’ where dir_name is the name of the directory where you want to go. For example, to move to ‘Documents’ folder, I will use the command ‘cd Documents’
  • Do not forget to include the directory path in the command before the folder name. For example, to move to a folder named “softwares” in my home directory I will use the command “cd /home/velocity-1297/softwares”, where velocity-1297 is my system name.
  • Use the command “pwd” to see the full path of your current directory at any time.
  • Use the command “cd ~” to navigate to the home directory directly from any directory.
  • To navigate to a directory just above your current directory use “cd ..”

Step 2: Select a Text editor program:

There various text editor programs that can be used to edit or create a text file in Linux. ‘Vim’ and ‘Emacs’ are the two popular options that are used by most of the Linux users. Vim generally comes built-in with the default Linux system. In case if it is not there you can install using the command

sudo apt-get install Vim

Similarly, you can install Emacs:

sudo apt-get install emacs

Step 3: Navigate to the directory where we want to create or edit a file:

To create a new file to a specific category or to edit a file in a directory navigate to the directory where you want to create or edit the file.

For example, If I want to create a new file in ‘softwares’ folder, I will use the command “cd /home/velocity-1297/softwares” to go to the software folder.

Navigate to the directory

Step 4: Create or edit the file:

So, now when we are already in softwares folder, we will create a new file named ‘sample’ here.

  •  To create a new file using Emacs use the following command line:

             emacs sample.txt 

create a new file using Emacs

It will open the emacs editor where you can do the editing and save the file.

emacs editor

  • To create a new file using Vim use the following command line:

            vim sample.txt 

create a new file using Vim

It will open the Vim editor where you can edit your file and save it. You can read more about various Vim editor operations and commands from here.

Vim editor

Please note:

If a file is already there in the folder, the above commands will open the file using the chosen editor. So, the above commands can be used to both create a new file or edit an old file.

For example, if ‘sample.txt’ is not there in the folder already, the above commands will create a new empty text file named ‘sample’. However, if sample.txt is already there in the folder, the above commands will open the file using the chosen editor. You can perform the editing and save the file.

For more command line operations such as Zipping and unzipping files on Linux, How to install phpMyAdmin, or to know about Apache web server configuration files, you can take a look at our guides.

Share on: