Patricia Sauer - Authentic Programming

Vi basics - Modifying a file, saving and exiting vi

a blurry editor

It might — and pretty sure will — happen one day, that you will have to log in to a server via SSH. In this case, you will just have your command line available. What will you do if you have to edit a file when there is no text editor with a UI available? You will get out of this situation safely, if you know how to use the text editors vi or nano. Let's talk about how to use the text editor of my choice: vi.

Using vi is not that difficult and you will get used to it, after some time. To open a file, just use the vi command followed by the file name. You can enter the "insert mode", by pressing "i" on your keyboard. To leave the insert mode, press "esc" on your keyboard. You can save your changes, by typing :w. Leaving vi is possible by typing :q. You can also do both at the same time by typing :wq or :x. To close vi without saving your changes, just type :q!.

How to open a file in vi

When you are on your command line, just use the vi command followed by the file name, e.g. vi file/to/open. After that, the command line will show something like the following:

This is some example file.
  
We will learn how to use vi here.
~                                                                               
~                                                                               
~                                                                               
"file.txt" 3L, 62C

At the bottom, the file name is displayed ("file.txt"). The file contains of 3 lines ("3L") and 62 characters ("62C"). By default, you are in "command mode". You can browse through the file by using the arrow keys. Typing something won't have an effect. But how do you change the file?!

How to modify a file using vi

To modify a file with vi, you have to switch to "insert mode". You can do so, by pressing "i" on your keyboard. After that, the command line will look like the following:

This is some example file.
  
We will learn how to use vi here.
~                                                                               
~                                                                               
~                                                                               
-- INSERT --

At the bottom, it tells you, that you are in insert mode now. You still can browse through the file by using the arrow keys while being able to modify the file. But how can you leave the insert mode and save the file?!

How to leave insert mode and save in vi

You can leave the insert mode by pressing "esc" on your keyboard. Your command line will look the same as in the beginning. To save your file, you need to type :w ("write"). In case that you want to save your changes and close the file, you can type :wq ("write + quit") or :x instead.

In case that you don't want to save your changes, you can type :q! to close vi without saving.

CommandResult
:wSave file
:wq or :xSave file and close file
:q!Close vi without saving

Recap

I hope you learned about the basic commands of vi. Just try them out a few times, and you should be good to go. Be prepared to learn some advanced commands in another post!

Liked this article?

Buy Me A Coffee

© 2018 - 2024 Patricia Sauer