Linux Commands Ubuntu

How to Edit a File in Linux/Ubuntu with sudo?

How to edit file in Linux Ubuntu with sudo

Editing is a crucial process in writing as it improves the organization, quality, readability, and clarity of the text. Editing improves the quality of text by eliminating punctuation errors, spelling mistakes, grammar issues, etc. In this article, we will demonstrate a few file-handling methods that are useful for editing a file in Linux/Ubuntu 22.04 LTS.

Prerequisites

In order to follow this article, we will require

  • System with Ubuntu 22.04 LTS Installed.
  • A user account with sudo privileges.
  • A text editor (We will use Nano and Vim in this article).

What is Sudo?

Sudo (superuser do) is a command line utility that temporarily elevates regular user accounts to have root privileges. When a user runs the sudo command, they are prompted for a password of the user account, after which the user is granted root privilege.

How to Edit a File in Linux/Ubuntu with Sudo?

We will discuss a few file handling methods used for file editing Ubuntu using the below command line editors

  • Using Nano Text Editor
  • Using Vim Text Editor

How to Edit Files in Ubuntu/Linux Using Nano Text Editor?

Nano is a powerful, lightweight text editor for Unix-like systems. Nano comes pre-installed and is the default text editor for Ubuntu 22.04 LTS. Nano has pre-defined shortcut keys for effective file handling. We can access a file in Nano by entering the sudo nano command followed by the path of the file that needs to be edited:

$ sudo nano /etc/apt/sources.list

We will be prompted for a password and after authentication, we will have access to the file.

Nano editor has short-cut keys on the bottom of the screen which can be useful for file editing. These keys are invoked with the “Ctrl” key.

Some ways of text manipulation using Nano Text Editor are discussed below:

How to Select Particular Text in Nano Text Editor?

Text can be selected in the Nano Text Editor by pointing the cursor at the line/word/paragraph/word and by pressing “Alt +A” to fix the point. This will set the mark and will be indicated by “Mark Set” at the bottom screen as shown below:

We can then navigate right and left by using the arrow keys on the keyboard to select text.

How to use Nano Editor to Copy, Cut, or Paste Text?

Cut, copy, and paste commands are used very frequently while editing Text. We can Copy and Cut text by first navigating to the text and then by selecting text using “Alt+A”. After the selection of text, we can press

Alt+6 and Ctrl+K for copy and Cut respectively. After we either Copy or Cut. the text will not be highlighted anymore. We can then navigate to where we would like to Paste the text and Paste text using Ctrl+U.

For example, below is an example of a whole line copied and pasted to line 1 in a file:

An example of Cut and Paste is shown below. In this example, text is Cut using “Ctrl+K” and pasted at the beginning of the file:

How to Find and Replace Text in Nano Text Editor?

Find and replace is also used extensively while editing texts. In the Nano text editor, we can use the shortcut “Ctrl+\” to find a word or string pattern. We can enter the string pattern that we want to replace with and press enter as shown below:

We will then be prompted to enter the word or string pattern that we want to replace the selected string pattern as shown below:

After pressing enter, all the occurrences of the string pattern will be highlighted and we will be prompted for confirmation:

We can Press “y” at the prompt to replace all occurrences of the selected string.

As seen from the above image, the selected string pattern “demo” is replaced by the string pattern “test”. Additionally, we will also get a notification of the number of occurrences replaced.

How to Save Text and Exit Nano Editor?

After editing the file, we can use “Ctrl+O” and Ctrl+X to save and exit the nano respectively.

How to Utilize Vim Text Editor for Editing Files in Ubuntu/Linux?

Vim is a free-to-use and highly adaptive text editor. Vim editor furnishes three modes to manage files:

  • Command Mode: Command mode is enabled by default. It supports commands like redo, undo, replace, etc. The “Esc” key is pressed in order to exit command mode.
  • Insert Mode: is enabled by “i”. It enables text insertion. We can exit command mode by pressing the “Esc” key.
  • Visual Mode: is enabled by “Ctrl+V”. It is used for selecting text for copy, cut, delete, paste, and other operations.

How to Install Vim Tex Editor on Linux/Ubuntu?

Before installing Vim Editor, the apt repository is updated by executing the following sudo command:

$ sudo apt update

After the apt repository is updated, Vim can be installed by running the following command:

$ sudo apt install vim

From the above image, it can be seen that Vim is installed successfully.

We can access a file in the Vim text editor by entering the sudo vi command followed by the path of the file that needs to be edited:

$ sudo vi /Files/samplefile1.txt

We will be prompted for a password and after authentication, we will have access to the file. Some ways of text manipulation using Vim Text Editor are discussed below:

How to Select Particular Text in VimText Editor?

Text can be selected in Vim Text Editor by first entering “Visual Mode”. “V” can be used to select a complete line while “v” is used to select text. The below example shows the selection of the complete line:

How to Cut, Copy, and Paste Text in Vim Text Editor?

In order to Cut, Copy, and Paste text in the Vim text editor, a user is required to switch mode to visual mode and select text. “Y” and “Y” are used to copy text and line respectively. Similarly, “d” is selected to cut specific text while “D” is used to cut the entire line. Finally, “p” is used to paste text while “P” is used to paste the entire line. Below is an example of copying a line:

From the above image, we can see that the complete line “What is Network Congestion?” is copied and pasted. An example of “Cut and Paste” is shown below:

From the above example, we can see that a complete line “Causes of Network Congestion” is cut and then pasted.

How to Find/Replace Text Using Vim Text Editor?

We have to enter Visual mode to first select string/line and then replace text. Vim uses the “%s” parameter for searching while “g” replaces that word. For example, if we want to replace the word “Causes” with “Reasons” in the line “Causes of Network Congestion”, we will first select a complete line by pressing “V”:

Then, for the find and replace operation, we will enter in the “Command mode”. Then, to replace “Causes” with “Reasons”, We will run the following command after entering in command mode:

%s/Causes/Reasons/g

After entering the above command, we will press enter. This will replace the word “Causes” with “Reasons” in the above line only as shown below:

We can see from the image below that the find and replace operation is successfully executed.

How to Save Files and Close Files in Vim Text Editor?

We can save and close the file by entering the “Command mode” and running the following command:

:wq

Conclusion

File handling operations like text selection, cut, copy, paste, find, and replace, etc., can be performed on files in Linux/Ubuntu OS by using a text editor like Nano, Vim, etc. In this article, we have discussed a few file-handling methods that are useful for editing a file in Linux/Ubuntu 22.04 LTS.

Similar Posts