- How to edit a text file in my terminal
- 6 Answers 6
- Modify Files in Linux using vi, nano or emacs
- Approach 1: Using the Vi Editor to Modify Files
- Everything you Must Know About How to Edit File in Linux? [Linux Edit File]
- How to edit files in Linux via Vi editor?
- Linux Edit file with Nano editor
- How to edit a File with Emacs Command Line Editor?
- Conclusion
How to edit a text file in my terminal
I’m using Linux mint and using the vi command to create text files, now that I created a text file and saved it. How do I get back into to edit the text file again?
6 Answers 6
it, will open up a text editor to edit your file.
Here, you can edit your file in the terminal window.
Open the file again using vi. and then press » i » or press insert key ,
and write the following command
Open the file again using vi. and then press the insert button to begin editing it.
could also run ‘vimtutor’ to learn how to use vi/vim. This of course assumes you have vim (vi’s big brother) if you don’t run ‘sudo apt-get install vim’
If you are still inside the vi editor, you might be in a different mode from the one you want. Hit ESC a couple of times (until it rings or flashes) and then «i» to enter INSERT mode or «a» to enter APPEND mode (they are the same, just start before or after current character).
If you are back at the command prompt, make sure you can locate the file, then navigate to that directory and perform the mentioned «vi helloWorld.txt». Once you are in the editor, you’ll need to check the vi reference to know how to perform the editions you want (you may want to google «vi reference» or «vi cheat sheet»).
Once the edition is done, hit ESC again, then type :wq to save your work or :q! to quit without saving.
For quick reference, here you have a text-based cheat sheet.
Modify Files in Linux using vi, nano or emacs
Linux users frequently utilize Vi, Nano, and Emacs as their preferred text editors. Vi is a standard editor that is revered for its advanced functionalities and efficiency. Conversely, Nano is a straightforward editor that boasts a user-friendly interface and is easily learned by novices. Emacs is a powerful and customizable editor with a complex user interface that is ideal for advanced users.
In this article, we are going to learn how to modify files in Linux using “vi, nano, and emacs”. Our discussion will encompass the installation process of each editor, their respective functions, as well as examples demonstrating how to employ them for the purpose of modifying files. With this knowledge, users can choose the editor that best suits their needs and master its features to perform text editing tasks efficiently in Linux.
Approach 1: Using the Vi Editor to Modify Files
Vi is a standard text editor in Unix and Linux operating systems. It is a powerful editor with many advanced features, such as multiple buffers, macros, and command line editing.
It is pre-installed on most Linux distributions. If the editor is not currently present on your system, you may install it by executing the following command −
Now, type «vi newFile.txt » to open the file in Vim editor.
After executing the Vim command this will open “newFile.txt” file. After we hit ENTER key, the screen will display a similar interface −
To begin inserting new text, switch to insert mode by pressing the «i» key and commence typing.
This is a sample text. ~ ~ ~ ~ ~ -- INSERT -- 3,1 All
To save the modifications made to the file and exit, enter the command ‘:wq’. This will write the changes to disk and quit the editor.
This is a sample text. ~ ~ ~ ~ ~ :wq
Vi has different modes that you can use to perform different tasks. There are two primary modes in the editor, namely the insert mode and the command mode. In command mode, you can navigate through the document, delete text, copy and paste text, search for text, and perform other editing functions.
Everything you Must Know About How to Edit File in Linux? [Linux Edit File]
Are you new to working with Linux OS? This post will teach you on how to edit file in Linux or how to edit file in Linux without VI.
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.
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.
nano [filename]
nano test.txt
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.
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.
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.
Conclusion
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: