vi is a powerful, lightweight text editor that comes preinstalled on almost every Unix and Linux system. Now, learning how to use vi to edit a file is an essential skill for system administrators, developers, and anyone who works in a terminal. This mode-based design may feel unusual at first, but once you understand the flow, you can edit files quickly and efficiently without ever touching a mouse. Unlike modern graphical editors, vi operates entirely from the keyboard and uses distinct modes for different tasks. This guide walks you through the entire process—from opening a file to saving your changes—with clear steps and practical examples.
What Is vi and Why Should You Learn It?
vi (short for visual editor) was created in the 1970s and remains a standard tool on Unix-like systems. Its successor, Vim (Vi Improved), is also widely available and adds features like syntax highlighting and multiple undo levels. Many systems alias vi to vim, so the commands in this article work for both.
Why bother learning vi when there are friendlier editors? If you SSH into a minimal server, a container, or a rescue environment, vi is often the only editor installed. Because vi is ubiquitous. Knowing how to use vi to edit a file means you can fix configuration files, write scripts, and troubleshoot systems without installing anything extra. It is also extremely fast: once you master the modes, you can work through and modify text with just a few keystrokes Most people skip this — try not to..
Understanding vi Modes
The key to mastering vi is understanding its modes. Because of that, unlike most editors, vi starts in Command mode, where every keypress is interpreted as a command rather than inserted text. You switch to Insert mode to type text, and back to Command mode to run commands like save, quit, or delete.
Here are the main modes you need to know:
- Command mode – The default mode. Use it for navigation, deleting, copying, pasting, and saving.
- Insert mode – Used for typing new text. You enter it by pressing
i,a,o, and other keys. - Visual mode – Used for selecting blocks of text. Press
vfor character selection orVfor line selection. - Ex mode (last-line mode) – Used for saving, quitting, and running extended commands. Press
:from Command mode.
A common beginner mistake is typing text while still in Command mode. If you hear beeps or see strange characters appear, press Esc to return to Command mode, then press i to enter Insert mode again No workaround needed..
How to Open a File in vi
Opening a file is straightforward. From your terminal, type:
vi filename.txt
Replace filename.txt with the path to your file. If the file does not exist, vi will create a new, empty buffer with that name when you save.
vi +25 filename.txt
This places the cursor on line 25. To open a file in read-only mode, use vi -R filename.txt. This is useful when you want to inspect a file without accidentally modifying it That's the part that actually makes a difference..
Navigating Through a File in vi
Once the file is open, you are in Command mode. Use these keys to move around:
h,j,k,l– Move left, down, up, and right (like arrow keys).w– Jump to the beginning of the next word.b– Jump to the beginning of the previous word.0(zero) – Move to the start of the current line.$– Move to the end of the current line.gg– Go to the first line of the file.G– Go to the last line of the file.:n– Go to line numbern(for example,:42).Ctrl + f– Page forward.Ctrl + b– Page backward.
You can also use the arrow keys in most modern vi implementations, but the traditional hjkl keys keep your hands on the home row and are faster once you get used to them That alone is useful..
How to Edit Text in vi
Editing text requires switching to Insert mode. From Command mode, press one of the following keys:
i– Insert before the cursor.a– Append after the cursor.I– Insert at the beginning of the line.A– Append at the end of the line.o– Open a new line below the current line and enter Insert mode.O– Open a new line above the current line and enter Insert mode.
Once in Insert mode, you can type normally. When you are finished, press Esc to return to Command mode.
To delete text, stay in Command mode and use:
x– Delete the character under the cursor.dd– Delete the entire current line.dw– Delete from the cursor to the end of the word.d$– Delete from the cursor to the end of the line.D– Same asd$.
To change text, use c commands. As an example, cw changes the current word (deletes it and enters Insert mode), and cc changes the entire line.
To copy and paste, vi uses the yank and put commands:
yy– Yank (copy) the current line.yw– Yank the current word.p– Paste after the cursor or below the current line.P– Paste before the cursor or above the current line.
To undo a change, press u. To redo, press Ctrl + r (in Vim) or U (in traditional vi). Worth adding: to repeat the last command, press . (period) And it works..
Saving and Exiting vi
Saving and quitting are done from Command mode using Ex commands. Press : to open the command line at the bottom of the screen, then type:
:w– Save the file.:w newname.txt– Save as a new file.:q– Quit (fails if there are unsaved changes).:q!– Quit without saving.:wq– Save and quit.:x– Save and quit (only writes if changes were made).ZZ– Save and quit (no colon needed).
If you try to quit with :q and have unsaved changes, vi will warn you. Use :q! to discard changes or :wq to save them.
Practical Example: Editing a Configuration File
Suppose you need to edit /etc/ssh/sshd_config to change the default SSH port. Follow these steps:
- Open the file: `sudo
vi /etc/ssh/sshd_config` Worth knowing..
-
Press
/, typePort, and pressEnterto search for the port directive. If the first match is commented out, pressnto jump to the next match until you find the activePortline And that's really what it comes down to.. -
Move the cursor to the port number, press
cwto change the word, type the new port number, and pressEscSmall thing, real impact. Nothing fancy.. -
If the line begins with
#, position the cursor over the#and pressxto remove it. The line should now read something likePort 2222. -
Save and exit by typing
:wqand pressingEnter. -
Restart the SSH service so the change takes effect. On most systemd-based distributions, use
sudo systemctl restart ssh; on some systems the service is namedsshd, so usesudo systemctl restart sshdinstead.
Before disconnecting, test the new configuration from a second terminal with ssh -p 2222 user@host. Plus, if the connection works, you can close the original session. If it does not, return to the file, correct the mistake, and restart the service again. Also remember to update your firewall rules to allow the new port; otherwise, SSH may be running correctly but still unreachable Not complicated — just consistent..
A Few Habits That Make vi Less Painful
The most common source of frustration in vi is being in the wrong mode. If keystrokes appear as text instead of commands, press Esc and try again. If commands seem to do nothing, check whether you are still in Insert mode. When in doubt, press Esc twice.
It also helps to save often. A quick :w costs little and protects you from mistakes. If you make a change you regret, u will undo it; if you undo too far, Ctrl + r will redo it in Vim. And if you get completely lost, :q! will abandon the file without saving, letting you start over.
Finally, do not try to memorize every command at once. On top of that, learn the mode switch, basic movement, insertion, deletion, saving, and quitting first. Those few commands are enough for most everyday edits. As you become more comfortable, add search, yank and put, and repeat commands to your toolkit Simple as that..
Conclusion
vi rewards a small amount of practice with speed and reliability. Plus, its modal design may feel unusual at first, but once the distinction between Command mode and Insert mode becomes automatic, editing files directly from a terminal becomes quick and predictable. Start with the essentials covered here—moving around, entering text, deleting lines, saving, and quitting—and you will have everything you need to edit configuration files, scripts, and notes on almost any Unix-like system Still holds up..