The vi text editor is found on nearly every Unix or Linux systems in existence, usually by default. I first used it a freshman at Purdue, in 1981. My second programming class, ran Unix on one of the big-iron university mainframes. Those were the pre-GUI (graphical user interface) days and everything was done via the command line, while basking in the comforting (eerie?) green-text glow of the old-school CRT (cathode ray tube) terminals. Purdue was phasing out punch cards and I had one class with them. Number your cards, is all I can say.
It was during those dark, sub-basement computer room late-nights that I passed into the ancient mystic netherworld of vi on Unix. Happily, here we are 39 years later and it is still my weapon of choice for text editing. Other popular, readily available Linux text editors exist, such as nano or emacs and do a right fine job. Not as easily as vi though, in my humble opinion.
Users growing up on strictly GUI or mouse-based editors might find command terminal-based vi intimidating. Don’t fret. We’re at the beginning of the roaring 2020s and there’s no better time to try something different. Give vi a fair try and I think you’ll see the value of a fast, no-drama text editor that works great across all your Linux servers, notebooks and microcontroller-based machines even including over remote (network or serial) connections. (Note, most distributions use an updated vi called VIM).
After a little while, vi might become an old friend to you too.
Buddy up to the ESC Key
Think of vi as a powerful, character or keyboard-based, minimalist text editor. By-the-way, vi can handle multi-megabyte text files, so hand-editing text-based, large data sets is possible without much fuss. Comma-separated value (.csv), HTML and json files come to mind.
In a Linux terminal, invoke vi with a filename. If the file already exists, you’ll simply be dropped into the text and can start editing from there. If the file doesn’t exist, it will create a new empty file. Here’s the command line for a new rob.txt file.
rob% vi rob.txt
Adding text is straightforward, keeping in mind that vi‘s methodology is character/key-based. Type the “i” character, for “insert” and add your desired text to the file. When finished hit the ESC key once to tell vi that you want to leave the “insert” function.
The escape key is an important vi mechanism used to conclude a command sequence. By default, vi accepts keyboard character/key combinations to do various functions, such as inserting text (with the “i” command), moving around in the file, replacing or deleting text and switching to command mode (with the “:” command).
vi‘s methodology is character/key based. Type the “i” character, for “insert” and add the your desired text to the file. When finished hit the ESC key once to tell vi that you want to leave the “insert” function.
That pretty much covers inserting text anywhere in a file. Move the cursor where you want to type. Use the “i” key to insert text, use the ESC key when finished. Go on to the next thing you want to add or change.
What if we already have some text and want to move down a few lines and insert a character or some text? Open an existing rob.txt file the same way.
rob% vi rob.txt
vi uses four basic navigation keys to move around in a file.
h – moves the cursor back one space
l – moves the cursor forward one space
j – moves to the next line
k – moves to the previous line
Say you want to add the letter “s” to the word “computer” down on line three in this rob.txt file. Use the “j” key to move down to line three. Now use the ‘l’ key to move over to the end of the word “computer”. Next, type the “i” (insert) key to insert a character, enter the letter “s” and follow up with the ESC key. After typing the ESC key, we are no longer inserting text and can freely move around again using the navigation h, l, j and k keys.
Notice that moving with the h, l, j and k keys minimizes your finger movement and lets you navigate around a file with one hand. I’ve been able to touch type since the ’70s and keeping your hand in one spot speeds things up a lot.
I habitually hit ESC after just about everything I do in vi. In fact, I occasionally catch myself punching the ESC key when editing my article files, up on WordPress and in LibreOffice.
Standard Method, Different Character
Deleting a text character works the same way as inserting. Move to the character you want to delete and type an “x”. Poof, the character will disappear from your screen. You can repeatedly use a string of x’s to delete a string of characters. Don’t forget to type the ESC key to end the delete function.
To replace a character use the “r” key. Move to the character you want to replace, type an “r”, then type your replacement character. Hit ESC to end the replace character function.
Suppose you want to search for a string of characters in your text file?
Use the “/” key. This one works slightly differently. Make sure you are in edit mode, by hitting the ESC key. Upon typing the “/” key you will be teleported to the bottom of the terminal edit window. Type in your search string. You might use “computer” in the rob.txt file. Hit Enter and vi will find the next instance of “computer” in the file. Type another “/”, followed by Enter and it will remember it’s looking for “computer” and go find the next instance. vi shows a prompt at the bottom, when it reaches the end of the file. Type “/” again to continue searching starting back up at the top.
Before we wrap up today’s tutorial, it might be nice to save our work.
Type a “:” to again teleport down to the vi command line at the bottom of the terminal window. Here you can use a whole host of additional key characters and combinations to really delve into the power of vi.
In our case, we just want to save our work in the file. Type ESC, then a “:”, then the “w” key. If you are editing an existing file, the file on the disk will be replace by your current edits. If it is a brand new blank file that’s never been saved, you can also give it a file name. I might start a new file and type “:” followed by “w”, then a space and then “rob.txt”. When editing a file, especially a large one, I use “w” fairly frequently.
Likewise, getting out of vi completely is as easy as typing the “:” key, then the “q” key. If you haven’t saved your work, you’ll probably get a “file not saved” prompt. I usually end my editing sessions with a “wq” combination. If you don’t want to save anything and just exit use the “q!” combination. Of course, if you start a blank file and for some reason change your mind, use “q” without doing any edits to just end your vi session.
There you have it. A crash course in using vi on a Linux machine. I’d rather open a terminal, type vi and a file name and get right into editing, instead of going up to the desktop menu, finding a GUI/mouse-based editor and doing it that way.
Of course, I regularly use LibreOffice to write stories and bluefish to not very occasionally edit HTML files. My online posts are now done on exclusively WordPress, as well. We all have to be somewhat open-minded and flexible.
For quick text edits across my whole spectrum of Linux-based machines vi is my go-to app. Old programs that work for me might just be a new program that works great for you. Jump in and be a part of the Unix/Linux legacy.
Look for future Linux tutorials that delve deeper into using the vi text editor.