Editing text-based files in Linux can be confusing if you are unfamiliar with using the command-line as there is no graphical user interface to help guide you. But fear not, we will cover two of the most popular console-based text editors, nano and vi, in this article.
How to Create or Edit a File
How to Save a File
You'll need to be logged into your server via SSH before beginning. If you're not sure how to do this, you may refer to the following guide, which covers Connecting to Your Server via SSH.
This text editor is a growing favorite among Linux users due to its overall ease of use and default on most modern Linux distributions. nano's most commonly used shortcut keys are shown at the bottom of the page with the ^ symbol representing the CTRL key followed by a lowercase letter.
Create or Edit a File in nano
On your server's console, type the nano command followed by the name of the text file, then press ENTER
You'll see an interface similar to the following screenshot, and you can begin editing the file now.
Saving a File in nano
Press the CTRL key and the O key
You'll be prompted for the name of the file to Save As
When you're satisfied with the filename, press the ENTER key
Then press the CTRL key and the X key to exit
Save: CTRL-O This will save the contents to the file you opened. Just press ENTER if this is still the file name you wish to edit or CTRL-C to cancel or CTRL-X to exit.
Close: CTRL-X This will close the nano program. If you've made any unsaved changes, it will ask you if you'd like to continue without saving.
vi is an older text editor and the most widely installed. It is a bit less intuitive than nano. However, once you learn a few common commands, you won't find them too difficult to use.
Create or Edit a File in vi
Editing a file in vi is a fairly straightforward process.
On your server's console, type the vi command followed by the name of the text file, then press ENTER
You should be greeted with an interface similar to the following.
Before you can begin editing the text, you'll need to press the i key to switch to INSERT mode, displayed at the bottom of the screen, as shown below.
You can now edit the text in the file.
Saving a File in vi
Once you're done editing a file, you'll need to save the changes you have made.
You will need to switch out of INSERT mode by pressing the ESC key before you can close or save the file; it should now no longer show — INSERT —
Now issue the write and quit commands.
Press the colon (: ) key on your keyboard (you may need to hold the shift key)
Type wq and press ENTER on your keyboard so that the command looks like :wq
Alternatively, you can press SHIFT and hit ZZ to quickly save the file and exit.
You will need to enter wq! and press ENTER on your keyboard to forcefully overwrite the file for read-only files.
Edit: i This will place the editor into INSERT mode so you can make changes to the text.
Save: :wq This will save the contents to the file you opened. (write and quit)
Close: :q or :q! This will close the vi program. If you would like to quit without saving any changes you've made, then use :q! instead of :q
Save and Close: Hold the shift key and press ZZ
You should now be able to successfully create, edit, and save a file in two of the primarily used text-based editors in Linux.