Using Vi? Here’s How to Open a File Then Save and Quit \
As a beginner in the world of programming, understanding and using a command-line text editor such as Vi can be daunting. However, Vi is highly customizable and lightweight, which makes it highly efficient for editing and creating files in a variety of computer systems. Here’s how to open a file then save and quit using Vi:
Opening a File in Vi
- Launch your terminal and type “vi ” followed by the name of the file you wish to open.
For example, if you want to open a file called “example.txt,” type the following command into your terminal:
“`
vi example.txt
“`
- Once you’ve opened your file, you can start editing. In Vi, you are in command mode by default to move around the screen or change some of Vi’s preferences. Press “i” to enter insert mode, where you can start typing or pasting in text.
- After you’ve made any changes, you need to save the file to keep your changes.
Saving the File with Vi
- Press the “Esc” button to exit insert mode and return to command mode.
- Type “:w” to save your changes to the file.
If you were to forget to save your file before exiting Vi, your changes would be lost. However, if you prefer to automate the save process, you can use the following command instead:
“`
:wq!
“`
This saves the file changes and exits Vi simultaneously.
Quitting Vi
- Lastly, to quit Vi, you can use the “:q” command.
However, this only works if you haven’t made any changes to the file. If you’ve made any changes and try to Quit, Vi will warn you “E37: No write since last change (add ! to override)”
- If you did make any changes to the file, you need to save them first using the “:w” command, followed by quitting using “:q.”
- In the case of making changes to the file and saving, you can combine the two commands to “:wq” or “:wq!” to save and quit the file all at once.