How to Edit Files on Ubuntu Command Line?

The editing of files is an important process of writing that involves a detailed review of the document. It helps make the content more accurate, eliminating spelling, grammatical, and punctuation mistakes, and increasing its readability. Like other Linux distro, the Debian-based operating system Ubuntu assists the user in editing files through the command line or graphical user interface. 

This post tells the possible methods that are helpful to edit files in Ubuntu using the command line

  • Using “nano” Text Editor
  • Using “vi” Text Editor

How to Edit Files Using “nano” Text Editor?

The “nano” is a commonly used powerful default text editor of Ubuntu that helps to create and edit the text/code and configuration files. It offers a large list of pre-defined shortcut keys for file handling.

Let’s take a sample file in the “nano” editor and edit it in different ways:

Sample File

A “File1.txt” is taken as a sample file opened in the “nano” text editor:

$ nano File1.txt

The “nano” editor shows the shortcut keys that can be easily invoked using the “Ctrl” key:

1. Selection of Particular Text

Place the mouse pointer at the particular line/paragraph that needs to be selected. Use the “Alt+A” shortcut key that indicates the mark of selection:

The “t” mark has been set. 

Start selecting the text with the help of the keyboard “right/left-arrow” key: 

The desired line has been selected.

2. Copy/Cut and Paste the Text

Copy and cut is the most commonly used file operation. The “nano” editor offers the “Alt+6” to copy the text and “Ctrl+U” to paste:

The selected text has been copied and pasted at the end of the file. To cut the selected part, utilize the “Ctrl+K” shortcut key.

3. Search and Replace Word

To search a specific word/character, press the “Ctrl+\” shortcut key. It prompts the pattern or word that you want to search for. Type the word in the search bar: 

For instance, the searched word is “This”. Hit the “Enter” key and enter the word of user choice in the “Replace with” bar, i.e., “it”:

The searched word “This” will be highlighted in the file. Type the “Y” letter for “yes” and “A” for all the instances:

The notification bar shows that the word “This,” having “3” total occurrences, has been replaced with “It.”

4. Save and Close the File

Press “Ctrl+S” to save the code and “Ctrl+X” to exit the editor from the terminal. For a detailed overview of “nano” editor working and usage, read our guide “How to Use nano Editor in Linux?

How to Edit Files Using “vim” Text Editor?

“Vim” is another open-source, well-reputed text editor for editing text files, scripts, and configuration files. in Linux. It refers to the enhanced version of the original “vi” text editor and is also called “Vi Improved.” 

How to Install Vim on Ubuntu?

Vim is an external application that can be easily installed from the default repository of Ubuntu using the “apt” command:

$ sudo apt install vim

The installation of “vim” has been completed. Let’s see if it can edit the files in Ubuntu

Sample File

The existing “File2.txt” is opened in the “vim” editor in this way:

$ vi File2.txt

Vim Editor Mode

The “vim” editor provides three modes to handle the files that are stated below:

  • Command Mode: It supports the execution of commands like find, undo, replace, redo, and many others. By default, the command mode is enabled; press “Esc” to move back from it.
  • Insert Mode: It allows text insertion into a file at the cursor point. Type “i” to enter in the insert mode and the “Esc” key to turn it off.
  • Visual Mode: It selects text to cut, delete, copy, and many other operations. Press “Ctrl+V” to enable the visual mode.

1. Selection of Particular Text

Enter into the “VISUAL” mode by pressing the “Shift+V” key. Type the lowercase letter “v” to select the specific text and uppercase “V” for the whole line:

The whole line has been selected.

2. Copy/Cut and Paste Content

To copy the text, press the small letter “y,” and for the whole line of text, use the capital “Y” letter. Use the small “d” to cut the specific text and the capital “D” for the whole line/paragraph:

The highlighted line has been copied.

Paste

Same as the copy and cut, the paste operation requires lowercase “p” to paste the text at a particular position and uppercase “P” to paste at the cursor point.

The copy-and-paste operation has been done successfully.

3. Search and Replace Word

Vim editor supports the “%s” parameter for searching the desired word/character and uses “g” to replace the entered word globally:

:%s/freedom/privilege/g

In this scenario, the word “freedom” is replaced with “privilege” in the whole file:

The “4” substitution shows the replacement of “privilege” in place of “freedom” in the whole file.

Note: The “vim” editor supports a large variety of commands for editing operations. For more editing commands, read this guide, “Top 25 Vim commands for Linux

4. Save and Close File

When the editing of the file is completed, enter into the “COMMAND” mode and press the “:wq” to exit and save the file with changes:

:wq

Note: For more ways of closing and exiting the vim editor follow this detailed guide on “10 Quick Ways to Exit Vim Editor

Conclusion

The Ubuntu operating system supports a large list of command line text editors from which most commonly “nano” and “vim” are used to edit files. The “nano” editor comes pre-installed in Ubuntu, whereas the “vim” corresponds to an external tool that can be easily installed from the default repository. The “nano” editor is the most recommended editor for beginners because of its shortcut keys as compared to vim commands.

This guide has provided the command line utilities to edit files in ubuntu.