
How to Open and Edit a File in Linux/Ubuntu?
Editing is an essential process in file handling. System files are automatically edited by Linux/Ubuntu processes in the backend to add log information or other system-related data. On the other hand, a user can edit files to append text, improve the quality and organization of text, remove grammatical and punctuation mistakes, etc. Files can be edited manually via CLI (Command Line Interface) and GUI (Graphical User Interface) tools.
This article will demonstrate how to open and edit a file in Linux/Ubuntu by the following methods:
- How to Open and Edit a File in Linux/Ubuntu via CLI Tools?
- How to Open/Edit a File Through GUI Tools?
- Bonus Tip: How to Open and Append a File via cat and echo CLI Tools?
How to Open and Edit a File in Linux/Ubuntu via CLI Tools?
Linux/Ubuntu supports a lot of command-line editors. In this section, the file handling and editing will discussed by using the the below command line editors:
- Method 1: How to Open and Edit a File via Nano Text Editor?
- Method 2: How to Open/Edit a File via Vi Editor?
These methods are discussed in the following sections
Method 1: How to Open and Edit a File via Nano Text Editor?
Nano is a command line-based text editor. It is pre-installed in Linux/Ubuntu. The salient feature of Nano is that it includes pre-defined shortcut keys that enable effective file handling. In the following sections, a text file is opened via the Nano text editor and edited in different ways:
Open a File via Nano Text Editor
To access a file in the Nano text editor, enter the nano keyword followed by the name of the file as shown below:
$ nano File1.txt

From the above image, it can be observed that the Nano editor includes short-cut keys which are very useful while file processing. These short-cut keys are displayed at the bottom of the screen. These keys are invoked by the “Ctrl” key.
Select Text in Nano Editor
To select a text in the Nano Text Editor, navigate the cursor to the starting character and press “Alt +A” to set a mark which is indicated at the bottom of the screen:
After, the mark is set, navigate right and left via the arrow keys on the keyboard to select text. For example, in the image below, the text “it achieves this highest efficiency” is selected:
Cut, Copy, and Paste Text via Nano Editor
Copy, Cut, and paste are frequently used commands while editing a text. To copy and paste a text, first navigate to the text and select the text by “Alt + A” followed by pressing “Ctrl + 6” to copy. Then, navigate to the area, where text is required to be pasted and paste text by pressing “Ctrl +U”. For example, the text “7z is one of the popular archive formats” is copied and pasted in the image below:
Similarly, to cut and paste a text, first navigate to the text and select the text by “Alt + A” followed by pressing “Ctrl + K” to cut the desired text. Then, navigate to the area, where text is required to be pasted and paste text by pressing “Ctrl +U”. For example, the text “archive format” is cut and pasted in the image below:
Find and Replace Text via Nano Editor
Finding and replacing text is quite common while editing a text. To find a string pattern “popular” and replace it with “frequently used” in the Nano text editor, use the shortcut “Ctrl+\” to find a string pattern and then enter “popular” in the “search (to replace)” field:
Then, enter the text to be replaced, i.e., “frequently used” in the “Replace with: “ field and press enter key:
All the occurrences of “popular” will be highlighted after pressing enter and the user will be prompted for validation as shown below:
Press “Y” to replace all occurrences of “popular” with “frequently used”:
From the image above, it can be verified that the word “popular” is replaced by “frequently used”. Additionally, a message indicating the number of instances replaced is displayed at the bottom of the screen.
Save Text and Exit Nano Editor
To save the changes in the file after editing, press Ctrl+O:
The user is prompted to save the file. If required, the user can change the file name else a user can press Enter to save the file. Finally, press the keyboard combination of Ctrl+X to exit the Nano editor.
Method 2: How to Open/Edit a File via Vi Text Editor?
Vi is a free-to-use command line-based text editor pre-installed in Linux/Ubuntu systems. Vi editor has three modes to edit text files:
- Command Mode enables commands like replace, undo, redo, etc. Command mode is the default mode which can be disabled by pressing the “Esc” button.
- Insert Mode enables the insertion text in a file. To enable insert mode, press “i”, while to disable insert mode press the “Esc” key.
- Visual Mode enables operations like selection of text, copy, cut, paste, etc. It is enabled by pressing the “Ctrl+V” and disabled by the “Esc” key.
How to Open a File via Vi Text Editor?
To open a File “File1.txt” via Vi Text, execute the following command:
How to Select Text via Vi Text Editor?
To select text via the Vi Text editor, first, enter the “Visual Mode”. Then, to select text, press either “v” or to select a complete line press “V”. The selection of text “7z is one of the frequently used archive format” is demonstrated below:
Cut, Copy, and Paste Text via Vi Editor
To Copy, Cut, and Paste text in the Vi text editor, first, enter the “Visual Mode” then navigate to the text, and select the text followed by pressing “y” or “Y” to copy a text or complete line respectively. After that, navigate to the area where the text is required to be pasted and paste the text by pressing “p” or “P” to paste text or complete line respectively. For example, the text “7z is one of the popular archive formats” is copied and pasted in the image below:
Similarly, to cut and paste a text, first navigate to the text and select the text followed by pressing “d” or “D” to cut a text or complete line respectively. After that, navigate to the area where the text is required to be pasted and paste the text by pressing “p” and “P” to paste text or complete line respectively. For example, the text “archive format” is cut and pasted in the image below:
Find and Replace Text via Vi Text Editor
To find and replace text in the Vi text editor, first, enter the “Visual Mode” then navigate to the text, and select the text. To find text, Vi uses “%s” while “g” is used to replace txt. For example, to replace the word “popular” with “frequently used”, first select the word “popular” as shown in the image below:
Then, switch the mode to the “Command mode”. To replace “popular” with “frequently used”, run the following command:
%s/popular/frequently used/g

From the above image, it can be seen that the word “popular” is replaced with “frequently used” successfully.
Save Text and Exit Vi Editor
To save the ext and exit the Vi text editor, enter the “Command mode” and run the following command:
:wq

How to Open/Edit a File Through GUI Tools?
Gedit is the default graphical text editor for Ubuntu that can be used to take notes, edit simple documents as well and provide advanced features for software development like syntax highlighting, formatting, etc. In the following sections, a text file is opened via Gedit and edited in different ways:
Open a File via Gedit
To open a file via the Gedit text editor open the Ubuntu File Manager and navigate to the file. Then right-click on the file and select “Open with Text Editor” from the drop-down menu:
This will open the file:
Select Text, Cut, Copy, and Paste Text via Gedit
To Cut/Copy text via Gedit, simply select the text via a mouse or a keyboard, then right-click on the selected text, and select Cut/Paste. For example, in the following example, the text “7z is one of the popular archive format” is cut/copied:
Next, navigate to the area where the text is to be pasted. Finally, right-click and select “Paste” from the drop-down menu as shown below:
As seen from the below image, the text “7z is one of the popular archive format” is copied and pasted.
Find and Replace Text via Gedit
To find and replace the text via Geidt, simply press the keyboard shortcut “Ctrl+H”. This will open the “Find and Replace” pop-up window. For example, to replace “popular” with “frequently used”, type “popular” in the Find tab while “frequently used” in the “Replace with” tab and finally press the “Replace All” button:
From the image below, it can be verified that the word “popular” is replaced by “frequently used”:
Apart from Gedit, other third-party editors such as Visual Studio Code, Sublime Text, GNU Emacs, etc., can be used to open and edit files in Linux/Ubuntu.
Bonus Tip: How to Open and Append a File via cat and echo CLI Tools?
Text files can also be appended using command line tools such as cat and echo.
To append text “Test: append data in File1.txt via cat command.“ in an existing file “File1.txt” via cat command, execute the following command:
$ cat >> File1.txt
Where the “>>” flag is used to append the existing file. Enter the text to append, i.e, “Test: append data in File1.txt via cat command.“ and press a combination of [Ctrl+C] keys to save and exit the file:
The above output indicates that the added text “Test: append data in File1.txt via cat command.“ is appended in File1.txt.
Similarly, to append text “Append via echo command.“ in an existing file “File1.txt” via echo command, execute the following command:
$ echo Append via echo command >> File1.txt
Where the “>>” flag is used to append the existing file.
The above output indicates that the added text “Append via echo command“ is appended in File1.txt.
Conclusion
A file can be opened and edited via CLI as well as by GUI. Nano text editor and Vi text editor are two popular text editors to manipulate text in CLI while Ubuntu’s default text editor gedit and other third-party like Visual Studio Code, Sublime Text, and GNU Emacs are used to manipulate text via GUI. This article demonstrated file manipulation methods in Linux/Ubuntu.