Editing text based files in Linux can be a bit 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 is included by 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
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.
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 a older text editor and the most widely installed. It is a bit less intuitive than nano, however once you learn a few of the common commands you won’t find it 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 which is 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 that 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
- For read only files, you will need to enter wq! and press ENTER on your keyboard to forcefully overwrite the file.
This will place the editor into INSERT mode so you can make changes to the text.
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 quite without saving any changes you’ve made then use :q! instead of :q
Save and Close: Hold the SHIFTkey 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.