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 be discussing various text editors along with other command-line commands for making the desired file changes.
Editing files using Vi editor
If you want to make the changes to the file, you can open the Vi editor in the command-line terminal. 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 the editing mode for you. But if you provide the file name that does not exist then it will create a new file for you.
If you are working with the Vi editor, then you must understand the VI Editing 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
For running the above-mentioned commands, you need to be in the “command mode”. Vi editor is case-sensitive and each command has its meaning so you need to make sure that you enter the right command. You can also use a, A, o for getting into the insert mode.
If you want to move within the file using the Vi editor, then 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
For running these commands, you need to be in the “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, now it's time to save the changes and then 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
Again, for making such changes, you need to be in the “command mode”.
Editing file with Nano editor
Apart from the Vi editor, you can simply use the Nano editor for making 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 along with the nano command for launching 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 that display the helpful commands that is beneficial for beginners for performing basic nano commands.
Below, we have mentioned some of the commonly used shortcuts that you should be aware of. You can use the navigation keys on the keyboard for navigating within the file text. For deleting the text, you can use the backspace key. For saving 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.
At this time, you can select any option to save the changes in various OS formats. You can press Altd+d for enabling the DOS format and Alt+m for Mac format.
For saving 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 for that, you need to select the specific word for which you need to perform the action. For selecting the text you can press the Alt+A and then the arrow key for selecting the complete word.
Apart from this, you can even perform search operations. You can use the ctrl+w for performing a simple search while you can use the ctrl+\ for searching and replacing the text.
Editing Files with Emacs Command Line Editor
Another useful text editor of Linux is Emacs that will help in editing 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 the 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 available on the top of the screen that is similar to any graphical application.
Second, is the main edit area where you can see the content of the file. Just below the edit area, you can see the highlighted bar that will show file details like name, editing mode, 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 the 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.
Now, to confirm you need to press yes and the editor will quit without saving any changes made to the file. If you want to delete the text, then you can simply use the delete or backspace key from the keyboard. If you want to go for using 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 the ctrl+x followed by 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 and for reverse search, you can press ctrl+r.
For performing a replace operation, you need to use the alt_shift+%. Then the editor will ask you for the specific word that 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".
Moving on, 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 the replacement. For example, the following screenshot shows emacs asking the user about the replacement for the word 'This'.
You need to 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 will be asked for permission, press 'y' and the specific word will be replaced.
Well, for using these Linux editors, you need to have a better grip on their specific shortcuts. If you use the shortcuts then you can seamlessly edit the files. For this, you need to practise a lot and to remember various shortcuts. You can choose any of the editors of your choice and start practising for smooth performance.
People also read: