Monday, August 1, 2016

Information Dump: Vi/Vim Quickstarter Guide

During my internship, I was tasked at one point with configuring an openSUSE server with Novell. The reason I was given the task was because I was the only one familiar with Linux systems. I won't go too far into the details or reason for this, let's just say it was a non-profit organization and leave it there.

While attempting to set it up, I needed to modify the fstab because for some reason it would not play nice with the drive and I was too inexperienced with Linux at the time to work out a cleaner way to do it. The problem I ran into was there was no graphical text editor, I didn't know how to get one and there was no nano or pico. There was only vi. For just starting out and my only familiarity being Ubuntu, it was terrible. I was lost. So being handed the book that the system came with opened to the vi section, I attempted and failed miserably. In the end, I did a lot of cat and echo because I was also unfamiliar with sed.

I failed to learn anything because it just seemed too daunting. Later down the road I would try it from time to time with little success. Then one day at the job I am currently working, I decided to allow myself only to use vi until I figured it out. To my surprise, it didn't take that long and is actually rather simple to use... just different.

Now most systems use vim, even when you run vi. Vim is much easier to use, however I have found vi on some devices that I use and it was easiest just to keep with working like I'm always on vi for habit and consistency. So here's some quick controls to get you started with vi so you can use it at the very least for what it was intended, editing text files. This should give you a simple base to get going.

So the first thing that happened when opening a file tends to be that most people try typing and it seems like random stuff just happens. This is because vi has different modes. You start out in command mode. To type, we need insert mode. There are two ways to get to insert mode and they do two different things, so let's start with a blank file and you can join in the fun. Start vi!

So once it is started up, press the i key to go to insert mode (makes sense, right?). Now type in (and don't hit enter at the end):


Now press Esc. Easy enough, and the cursor should now be on 3. Now let's say we want to add a 4 to the end. So let's press i again and hit 4. Now you may notice the 4 is before the 3. What gives? Well, we did insert and insert will insert before the cursor. Well, darn, let's delete that pesky 4 that is cutting in line. Press your Esc then delete key.

Okay, so how do we insert something after the cursor? Well, we use a fancier word, append! Can you guess what we hit for that? If you guessed a, then you're right! So press that a key and hit 4.

Okay, now let's try making a second line by pressing enter and let's type...

xzy 789

Okay, so now we have two lines. There are a few different ways we can navigate this (press Esc so we're in command mode). You can use the arrow keys, which is straight forward. The other us using h, j, k, and l keys. Up is k, down is j, left is h, and right is l. Easy enough, except for the counter-intuitive l for right, but at least it's on the far right.

So now the next big question is how do we exit? There are a few different ways and we need to be in command mode. First, the scenarios and how to exit. You press enter after these commands.

If you opened up a file and it remains unchanged, you can do


If you edited a file, edited it, and now need to save and quit, you can do either of these
:wq <optional filename>
:x <optional filename>

If you changed something but want to quit without saving, you can do this


Since we have no filename, the optional filename to save is well... mandatory. Now if we just want to save the file... say as test.txt, you can do this

:w test.txt

This will save it in your pwd. So far, so simple. With these memorized, it should be pretty easy to at the very least use vi or vim when you need to. So we can insert, append, delete, navigate, save and quit. Let's look at a feature I find very useful when a file is cluttered with way too much commenting that looks like more configuration. Let's delete the xyz line entirely. Arrow onto that line and double tap your d key. That removes the line entirely. If you just want to delete the text, you could tap shift+d at the beginning of the line (use home and end to get to the beginning and end of lines). Shift+d will remove all of the line from the cursor on.

What more could you ask for? Copy pasta! Copying, cutting, and pasting is part of today's culture. Not having this ability in a text editor is horribly inconvenient. Fear not, for we have that ability in the form of yanking! Sounds weird, but bear with me. Before we can get there though, we need to learn to select text. We do that with v. When you use v, you start selecting on the cursor and move the cursor around to where you want to select to. There's also a way to select whole lines with shift+v and arrow down or up or just stay right there for one line. Keep in mind that this will include the newlines at the end.

With this, we can now yank (copy) or delete (cut) text. To yank the text, you can simply press y with the text highlighted. To delete, you press d. Now to paste it, you arrow to the point you wish to insert the text and press p.

Now you may remember to delete a line, I mentioned you can use shift+d, well this is the equivalent of cutting that single line. So after you do that, you can press p to paste it. The same works for double tapping d. Copying a whole line for pasting is also as simple as double tapping y.

With that, you can now easily begin some basic file editing. Hopefully this will help a poor wayward soul who shivers at the mere mention of vi. It really isn't that scary, it's just so different than what people are used to nowadays. There are plenty of more advanced features, like regex searching and replacing that are some powerful features worth looking into as you get more comfortable with it. There are also even more numerous features than just these, but I try to stick to what I need rather than finding a feature that I need to make a need for using it.

Tag Cloud

.NET (1) A+ (2) addon (6) Android (4) anonymous functions (5) application (10) arduino (1) artificial intelligence (2) bash (4) c (7) camera (1) certifications (4) cobol (1) comptia (4) computing (2) css (2) customize (16) encryption (2) error (19) exploit (17) ftp (3) funny (2) gadget (3) games (2) Gtk (1) GUI (5) hardware (7) haskell (15) help (8) HTML (6) irc (2) java (5) javascript (21) Linux (20) Mac (5) malware (2) math (8) network (9) objects (2) OCaml (1) perl (4) php (9) plugin (7) programming (42) python (24) radio (1) regex (3) security (25) sound (1) speakers (1) ssh (3) story (1) Techs from the Crypt (2) telnet (2) tools (15) troubleshooting (5) Ubuntu (4) Unix (4) virtualization (1) web design (14) Windows (8) wx (2)