List of content you will read in this article:
Linux system allows the users to make the required changes to the files. For this, many text editors are available for carrying out easy changes. Not only this, you can run various commands from the command-line interface for editing the files. In this article, we will discuss how to edit file in Linux on various text editors and other command-line commands for making the desired file changes.
How to edit files in Linux via Vi editor?
You can open the Vi editor in the command-line terminal if you want to change a file. Once the terminal is open, you can type of below command to open the file in the Vi editor.
vi <filename_NEW> or <filename_EXISTING>
If you provide the file name that exists already, then the Vi editor will open this file in editing mode for you. But if you provide a file name that does not exist, it will create a new file for you.
If you are working with the Vi editor, you must understand the VI Editor commands for seamless and efficient working. It will speed up the editing process by entering direct commands. Below are some Vi commands-
i – this command will allow you to Insert wherever your cursor is (goes into insert mode)
a – it will allow you to write just after the cursor (goes into insert mode)
A – it will allow you to write at the end of the line (goes into insert mode)
ESC – this will allow you to exit the insert mode
u – it will allow you to undo the last change
U – it will allow you to undo all changes to the entire line
o – it will allow you to open a new line (goes into insert mode)
dd – it will allow you to delete the specific line
3dd – it will allow you to delete 3 consecutive lines.
D – it will allow you to delete the contents of the line just after the cursor
C – it will allow you to delete the contents of a line just after the cursor and allow you to insert new text. You can press the ESC key if you want to end insertion mode.
dw – it will allow you to delete the specific word.
4dw – it will allow you to delete 4 consecutive words
cw – it will allow you to change the specific word
x – it will allow you to delete the specific character at the cursor
r – it will allow you to replace the specific character
R – it will allow you to overwrite the characters just from the cursor onward
s – it will allow you to substitute one character just under the cursor and then continue to insert
S – it will allow you to substitute the entire line and begin to insert just at the beginning of the line
~ – it will allow you to change the case of a specific character
To run the above commands, you need to be in “command mode.” Vi editor is case-sensitive, and each command has its meaning, so you must ensure that you enter the right command. You can also use a, A, or o to get into the insert mode.
If you want to move within the file using the Vi editor, you can use the following options.
k – it allows you to move the cursor up
j – it will allow you to move the cursor down
h – it will allow you to move the cursor left
l – it will allow you to move the cursor right
To run these commands, you must be in “command mode.” Also, you can use the arrow keys on the keyword to make these movements within the file.
After making the required changes to the file using the Vi editor, it's time to save the changes and close the specific file. You can use the following shortcuts to do so.
- Shift+zz – it will allow you to save the file and quit
- :w – it will allow you to save the file but keep the file open
- :q – it will allow you to quit the file without saving it
- :wq – it will allow you to save the file and quit vim
Again, to make such changes, you need to be in “command mode.”
Linux Edit file with Nano editor
Apart from the Vi editor, you can use the Nano editor to make the desired changes to the file. You can do it by running a simple command-
It will open the nano UI that looks like the image below.
You can also provide the filename and the nano command to launch the editor directly with a file.
In the above image, you can see that the screen is divided into four parts.
- First, the line at the top of the screen will show the version of the editor, opened file, and the editing status of the file.
- Second is the main editing area, where you can see the contents of the opened file.
- Third, the highlighted line at the bottom of the edit area where you can see the important messages.
- Last, the two lines at the bottom display the helpful commands for beginners performing basic nano commands.
Below, we have mentioned some commonly used shortcuts you should be aware of.
- You can use the navigation keys on the keyboard to navigate within the file text.
- To delete the text, you can use the backspace key.
- To save the changes made to the file, you can use the ctrl+o.
Once you try to save the changes, the editor will ask for confirmation, as shown below.
You can now select any option to save the changes in various OS formats. You can press Alt+d to enable the DOS format and Alt+m for the Mac format.
To save the changes, you can press enter.
You can use the ctrl+k and ctrl+u for cutting and pasting the lines of the text. You can also use the keyboard shortcuts for cutting and pasting individual words, but you need to select the specific word for which you need to perform the action. To select the text, you can press the Alt+A and then the arrow key to select the complete word.
Apart from this, you can even perform search operations. You can use the ctrl+w for performing a simple search while the ctrl+\ for searching and replacing the text.
How to edit a File with Emacs Command Line Editor?
Another useful text editor of Linux is Emacs which will help edit the files using the command-line interface. You can run the following command to get started with the file edit.
sudo apt-get install emacs
Also, you can run the following command along with the file name that will open the file directly into the Emacs editor.
emacs -nw [filename]
In the above command, the “-nw” flag is used for launching the Emacs in the bash itself rather than opening in a separate window by default.
emacs -nw test.txt
This command will open the test file in the Emacs UI, as shown below.
Emacs UI is divided into different parts.
- First is the top menu on the screen, which is similar to any graphical application.
- Second is the main edit area, where you can see the file's content. You can see the highlighted bar below the edit area that will show file details like name, editing mode, and status.
- Last, there is an area where you can see the inputted command and output.
Once you make the desired changes to the file, it is time to save those changes using ctrl+x followed by ctrl+s. You can see the changes in the last area, as shown below.
But if you want to discard the changes made to the file and quit the editor, you can press the ctrl+x followed by ctrl+c. The editor will confirm the input command as shown below.
To confirm, you need to press yes, and the editor will quit without saving any changes to the file. If you want to delete the text, you can use the delete or backspace key from the keyboard. If you want to use the shortcuts, then you can use the ctrl+k for deleting a complete line, alt+d for deleting a specific word, and alt+k for deleting a sentence.
If you want to undo the changes, you can use ctrl+x followed by ctrl+u. If you want to re-do the undo changes, you can press ctrl+g followed by ctrl+_. For forward-searching, you can use the ctrl+s; for reverse search, you can press ctrl+r.
For performing a replacement operation, you need to use the alt_shift+%. Then the editor will ask you for the word you want to replace. Enter the word then the editor will ask for the replacement confirmation. In the below screenshot, the user is replacing the word “This."
To launch a replace operation, use the Alt+Shift+% keyboard shortcut. You'll be asked for the word you want to replace. Enter it. Then the editor will ask you for a replacement. For example, the following screenshot shows emacs asking the user about the replacement for the word 'This.'
You must provide the replacement text for “This” and press enter. For carrying out each replacement operation, the editor will ask for your permission first:
After you are asked for permission, press ' y', and the specific word will be replaced.
To use these Linux editors, you need to have a better grip on their specific shortcuts. If you use shortcuts, then you can seamlessly edit the files. For this, you need to practice a lot and remember various shortcuts. You can choose any of the editors of your choice and start practicing for a smooth performance. This is all about how one can edit/modify files in Linux operating system. Also, we have listed different Linux edit file commands that will help you to accomplish your goal.
People also read:
I'm fascinated by the IT world and how the 1's and 0's work. While I venture into the world of Technology, I try to share what I know in the simplest way with you. Not a fan of coffee, a travel addict, and a self-accredited 'master chef'.