Javatpoint Logo
Javatpoint Logo

Linux Edit file

Linux file system allows us to operate various operations on files like create, edit, rename, remove. We can edit files by different Linux editors like vim, nano, Emacs, Gedit, Gvim, and more.

How to edit files in Linux

Let's understand how to edit files on a Linux server over different text editors.

  • Edit files with VI editor

The VI editor is the most widely used text editor in Linux based systems. The Vi editor has various modes like normal mode, insert mode, command mode, line mode, and more. Each mode allows us to operate its specific operations.

It is most widely used because of its modality. Most of the tools have just one mode; they take input and performs commanded operation, but Vi has several modes.

When we start Vi, it opens with normal mode, which is basically a command mode. In this mode, whatever we type is considered as a command, not as input.

Sometimes Vi seems like a typical editor, but when you are used to it, it will feel you like one of the powerful editing tools.

Before editing files, let's understand how to switch a mode in Vi editor:

  • Press the ESC key for normal mode.
  • Press i Key for insert mode.
  • Press :q! keys to exit from the editor without saving a file.
  • Press :wq! Keys to save the updated file and exit from the editor.
  • Press :w test.txt to save the file as test.txt

Now, let's understand how to edit a file using the Vi editor.

To edit a file, create a file first using the cat command:

The above command allows to enter the content of the file on the terminal, enter the content of the file, then press CTRL+D keys to save the file. Consider the below snap of the terminal:

Linux Edit file

To edit this file, execute the below command to open with Vi editor:


Linux Edit file

The above command will open the file with the Vi editor in the normal mode. Consider the below image of the editor:

This file is open in normal mode, to switch it to insert mode press 'ESC' key followed by 'i' key. Place the cursor on your desired position and enter some text. To save the file and exit from the editor, press the 'ESC' key, followed by :wq! Keys. Consider the below image of the editor:

Linux Edit file

Now, we have successfully edited a file using the Vi editor. The editor will be closed after this and back to the command line. To see the file content, execute the cat command as follows:

Consider the below snap of the terminal.

Linux Edit file
  • Edit files with a nano text editor

Nano is also one of the most used editors in Linux based systems. It is a straightforward and influential editor of Linux. It is a built-in editor for the Linux distributions. We do not need to have any prior knowledge about the nano editor before using it. In nano, no primary command is used to operate on the file. All the basic operations are displayed on the bottom of the editor. We can trigger them with a CTRL key, for example, to save the file press CTRL+O keys, to exit from the editor press CTRL+X key.

To edit a file with the nano editor, open the file from the directory where it is stored with the following command:

The above command will open the Demo.txt file with nano editor. To edit the file, move the cursor and enter the desired text and press the CTRL+O keys to save the file. Consider the below image of the nano text editor:

Linux Edit file

Press CTRL+X keys to exit from the editor. The changes you made on the file have been saved successfully. To verify the changes, execute the cat command as follows:

It will display the file content. Consider the below output:

Linux Edit file
  • Edit file using Emacs editor

Emacs editor also allows us to edit files. Additionally, it has many built-in features such as web browser, calendar, terminal, and more. It works as a normal text editor. In Emacs editor, we can edit files and save them by navigation bar. It is not a built-in editor in Linux based system, but we can install it using the terminal.

To install emacs editor open terminal (By CTRL+ALT+T) and execute the below command:

The above command will prompt for the administrative password, type the password and press ENTER key. Consider the below snap of the terminal:

Linux Edit file

It will verify the installation process, press 'y' key for confirmation. The installation process will begin, it will take a while to complete the installation process. Consider the below output:

Get:1 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 emacs25-common all 25.2+1-6 [13.1 MB]
Get:2 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 liblockfile-bin amd64 1.14-1.1 [11.9 kB]
Get:3 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 liblockfile1 amd64 1.14-1.1 [6,804 B]
Get:4 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 emacs25-bin-common amd64 25.2+1-6 [127 kB]
Get:5 http://in.archive.ubuntu.com/ubuntu bionic-updates/main amd64 libgif7 amd64 5.1.4-2ubuntu0.1 [30.9 kB]
Get:6 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 libotf0 amd64 0.9.13-3build1 [44.6 kB]
Get:7 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 m17n-db all 1.7.0-2 [1,239 kB]
Get:8 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 libm17n-0 amd64 1.7.0-3build1 [243 kB]
Get:9 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 emacs25 amd64 25.2+1-6 [3,468 kB]
Get:10 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 emacs all 47.0 [1,748 B]
Get:11 http://in.archive.ubuntu.com/ubuntu bionic/main amd64 emacs25-el all 25.2+1-6 [15.6 MB]
Fetched 33.9 MB in 3min 20s (170 kB/s)                                         
Selecting previously unselected package emacs25-common.
(Reading database ... 168266 files and directories currently installed.)
Preparing to unpack .../00-emacs25-common_25.2+1-6_all.deb ...
Unpacking emacs25-common (25.2+1-6) ...
Selecting previously unselected package liblockfile-bin.
Preparing to unpack .../01-liblockfile-bin_1.14-1.1_amd64.deb ...
Unpacking liblockfile-bin (1.14-1.1) ...
Selecting previously unselected package liblockfile1:amd64.
Preparing to unpack .../02-liblockfile1_1.14-1.1_amd64.deb ...
Unpacking liblockfile1:amd64 (1.14-1.1) ...
Selecting previously unselected package emacs25-bin-common.
Preparing to unpack .../03-emacs25-bin-common_25.2+1-6_amd64.deb ...
Unpacking emacs25-bin-common (25.2+1-6) ...
Selecting previously unselected package libgif7:amd64.
Preparing to unpack .../04-libgif7_5.1.4-2ubuntu0.1_amd64.deb ...
Unpacking libgif7:amd64 (5.1.4-2ubuntu0.1) ...
Selecting previously unselected package libotf0:amd64.
Preparing to unpack .../05-libotf0_0.9.13-3build1_amd64.deb ...
Unpacking libotf0:amd64 (0.9.13-3build1) ...
Selecting previously unselected package m17n-db.
Preparing to unpack .../06-m17n-db_1.7.0-2_all.deb ...
Unpacking m17n-db (1.7.0-2) ...
Selecting previously unselected package libm17n-0:amd64.
Preparing to unpack .../07-libm17n-0_1.7.0-3build1_amd64.deb ...
Unpacking libm17n-0:amd64 (1.7.0-3build1) ...
Selecting previously unselected package emacs25.
Preparing to unpack .../08-emacs25_25.2+1-6_amd64.deb ...
Unpacking emacs25 (25.2+1-6) ...
Selecting previously unselected package emacs.
Preparing to unpack .../09-emacs_47.0_all.deb ...
Unpacking emacs (47.0) ...
Selecting previously unselected package emacs25-el.
Preparing to unpack .../10-emacs25-el_25.2+1-6_all.deb ...
Unpacking emacs25-el (25.2+1-6) ...
Setting up liblockfile-bin (1.14-1.1) ...
Processing triggers for mime-support (3.60ubuntu1) ...
Processing triggers for desktop-file-utils (0.23-1ubuntu3.18.04.2) ...
Setting up libotf0:amd64 (0.9.13-3build1) ...
Processing triggers for install-info (6.5.0.dfsg.1-2) ...
Setting up liblockfile1:amd64 (1.14-1.1) ...
Setting up libgif7:amd64 (5.1.4-2ubuntu0.1) ...
Setting up m17n-db (1.7.0-2) ...
Setting up emacs25-common (25.2+1-6) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
Setting up libm17n-0:amd64 (1.7.0-3build1) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for gnome-menus (3.13.3-11ubuntu1.1) ...
Processing triggers for hicolor-icon-theme (0.17-2) ...
Setting up emacs25-bin-common (25.2+1-6) ...
update-alternatives: using /usr/bin/ctags.emacs25 to provide /usr/bin/ctags (ctags) in auto mode
update-alternatives: using /usr/bin/ebrowse.emacs25 to provide /usr/bin/ebrowse (ebrowse) in auto mode
update-alternatives: using /usr/bin/emacsclient.emacs25 to provide /usr/bin/emacsclient (emacsclient) in auto mode
update-alternatives: using /usr/bin/etags.emacs25 to provide /usr/bin/etags (etags) in auto mode
Setting up emacs25-el (25.2+1-6) ...
Setting up emacs25 (25.2+1-6) ...
update-alternatives: using /usr/bin/emacs25-x to provide /usr/bin/emacs (emacs) in auto mode
Install emacsen-common for emacs25
emacsen-common: Handling install of emacsen flavor emacs25
Install dictionaries-common for emacs25
install/dictionaries-common: Byte-compiling for emacsen flavour emacs25
Setting up emacs (47.0) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...

The above daemon process has installed the emacs editor on our system. Now we can explore the emacs editor in our Linux system.

To open a file with emacs editor change directory to the path where our file exists and execute the below command:

The above command will open the file with the emacs editor. We can edit the file like a traditional text editor. Consider the below image of the emacs editor:

Linux Edit file

Edit the file and save it using the save option given on the navigation bar.

To verify the updation of the file, execute the cat command as follows:

Consider the below output:

Linux Edit file




Youtube For Videos Join Our Youtube Channel: Join Now

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA