APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed
RSS Feeds RSS Feeds











(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
->
-> Vi primer


Vi Primer




This site is vi powered image



Some things you should know right away:

It's pronounced "vee-eye". That's important because you don't want people to think you are completely illiterate, and they will if you say "veye" or "vee".

There are people who will differ with this, but here's the deal: those people who pronounce differently know that a great number of people say "vee-eye", but a lot of the people who do pronounce it "vee-eye" don't realize that there are other ways. So be safe. Go with "vee-eye".

By the way, if you have any modern version of Unix/Linux, what you probably want is the graphical editor that is (of course) available only within the graphical environment. Somebody shut that off a long time ago? Try "startx". That editor doesn't begin to have the truly awesome power and beauty of vi, but there is no learning curve.

A caveat: it's hard to see leading or trailing spaces with the graphical Edit program, and there are places where spaces in the wrong spot can mysteriously break things. Be careful, and remember this.

Of course, you can't use the graphical editor on a dumb terminal or over a dial-up connection. Nor can you use it if your graphical environment is kaplooey, or never worked at all. So having at least a minimal knowledge of vi is helpful, if not absolutely necessary.

Honestly, there are only a few things you have to know to use vi. There are lots of things you should know, lots of things you could know that could make your life easier now and then, but there really are only a handful of things you need to know to get a job done. It may take you ten times longer than it would if you learned just a little bit more, but you will get it done, and that's better than getting nothing done at all.

So let's get started. First thing to do is type:

vi /ect/frammif/mkil

No, that's not a misprint: I really want you to type "vi /ect/frammif/mkil". Trust me on this one; it's all in a good cause.

OK. If you've done that, and your system isn't the strangest Unix system in the whole world, you should see something that looks a lot like this:


~
~
~
~
~
~
~
~
~
~
~
~
~
~
~
"/ect/frammif/mkil" [New file]


Without doing anything at all, I want you to look at your screen. Notice that the "/ect/frammif/mkil" is at the bottom of the screen and it says "[New file]". Memorize what this looks like, because every time you mistype the name of a file, this is what you will get.

Notice that, at least right now, vi doesn't care a bit that you don't (I'm pretty sure you don't) even have a directory "/ect", or a sub-directory under that called "frammif", never mind a file called "mkil". Right now, vi just doesn't care. All it knows is that there is no such file right now, so it must therefore be a "New" file. Simple minded, yes.

Notice the "~"'s running down your screen? Those are called "tildes" if you prefer accuracy, or "squigglies" by some people. I don't care what you call them, I just want to to remember that in vi, those "~"'s mean "Nothing is there". That's "nothing" as in absolutely nuttin'. Not a bunch of spaces such as separate every word on this page, but nothing at all. Zip. Nothing. Empty.

That makes sense, here. If /ect/frammif/mkil is a new file, there shouldn't be anything in it. Good so far? OK, let's try something. Press ENTER.

Nothing happened, right? Try the arrow keys. Don't hit any letters or anything else, just the arrow keys. Anything happen? Can you move down into those squigglies? No? Why not?

Because vi won't let you move over what is not there. Other editors (like the graphical Edit program we talked about above), would just assume you want to add spaces or empty lines, and would let you move down. Not vi, though. Vi is picky about those things, and you are stuck right where you are.

Well, there has to be a way to add text, right? Of course. There are two ways (actually a whole lot more, but we're only going to learn two here-keep it simple, remember?). The first way is to type a lower case "i". If you can remember that "i" means insert, that will be good. Go ahead, type an "i", but don't type anything else. What happened?

Nothing, right? Actually, if someone else set up your editing environment, you might have seen "INSERT MODE" appear at the bottom of your screen, but probably not. So, that's the second thing you've learned about vi: if you type "i", nothing happens.

But wait: something did actually happen. Try typing something else now, anything at all. "The quick brown fox wasn't quick enough". Wow. Look at that. It's working! Press ENTER. Type some more. Great fun, right?

OK, now I'm going to break it. Sorry, but this is the only way you are going to learn. Press ESC. Go ahead, there is no point in typing more. Press ESC. Press it again. And again. Wait, then ESC-ESC-ESC real fast, pause for a second and then two more. Did your computer beep at you every time you pressed ESC? It might have (it depends on a few things like: does your speaker work?), which is vi's charming way of saying "Just what is your basic problem, dude? You already pressed it once;I did what I'm supposed to, but NOOO, you have to press it again, and again and again.."

OK, now try the arrow keys. If they don't work, use the "-" key to move up, the ENTER to move down, Backspace to move left, and SPACE-BAR to move right. Try to remember those in case you are ever in a situation where your arrow keys don't work. If it's easier for you, you can also use "h", "j", "k" and "l" to move around. Try it.

Notice that you still can't move into those tildes. Nothing's changed; you've added some text that you can move around in, but that's all.

Now let's add a line. Get yourself right on the very first line and then type "o". The "o" stands for "open". Neat, isn't it? A whole line opened up underneath where you were and you can type whatever you want until you want to move around again. When you want that, press ESC, and then you can move again.

If you cheated and used the arrow keys while you were typing, you might have found out that they work, too. But don't count on that: you might not always have arrow keys that work, and some versions of vi don't let you use them when you are typing.

Next lesson: Press ESC if you haven't already, and put yourself (well, the cursor) anywhere on one of the lines you just typed. Type a lower case "d". Nothing happens (you get a lot of "Nothing happens" with vi). Do it again. Whoops! Did you see that sucker disappear? No? Try it again, and pay closer attention. Type "d", and then type it again. Instant line eradicator!

Of course, sometimes you are going to delete a line you didn't mean to delete. Type "u" ("undo"). Magic, isn't it? Type it again. Wow. Again. And again. It's like stuck, isn't it?

Let's try something else. Get on the very first line and press "d" twice. Now move to the very last line and type a "p". Wow, now you can move lines! But wait, there's more: type "p" again. And again. And once more. Now you can duplicate lines, too. "p" is for "put".

Sometimes you don't want to remove lines, just characters. Vi can do that. Put your cursor on top of a character. Pick a mean looking one, a character that doesn't deserve to be in your file. You are now judge, jury, and executioner. Does this character deserve to die? You bet! Type an "x" and the little creep is gone. Changed your mind? Bring it back with "u". Toy with it: "u" and it's gone, "u" and it's back. Gone, back, gone back. Only you can determine this letter's fate. There's another neat trick that can come in handy if you transpose characters while typing. Say you accidentally type "lteters". Put your cursor on the first "t" and hit "x". Then, without moving a muscle, hit "p". You now have "letters". Neat.

One more thing about "x" (actually about almost any command, but we'll use "x" to demonstrate). Put yourself at the beginning of a line and then type "i", followed by "hello". Hit ESC, then move back to the "h" of "hello". Watch carefully now: type "5x". "hello" disappears. Hit "u" and then try "3x". Get the point? You could type "58x" and the next 58 characters would disappear. The reason I mention this is that sometime you will do it accidentally, and if I didn't give you this hint, you wouldn't have a clue. Forewarned and all that.

You've now learned how to move around, how to insert and delete characters and whole lines, and that's enough. There is no editing task that you cannot accomplish with just this. Yes, there are faster and better ways to do all kinds of things that you might have to do, but there is nothing you cannot do just knowing these few commands.

But you do have to learn how to write your changes and get out, and (important) how NOT to write your changes and get out.

Let's try writing this file. To do that, press ESC if you are in insert mode, and then type a ":". The cursor moves to the bottom of your screen and your computer puts on a very patient expression. You probably won't want to try this, but you could sit there for 6 or 7 hours and vi would do ABSOLUTELY NOTHING. Vi is very, very patient.

But you aren't, right? So type "w", which means "write". OK, cool, the file is written, and now we can..

What? What do you mean you got an error? What error? Let me see that stupid thing. What did you do now? You probably broke it for good this time, and people are going to be real mad at you because YOU PRESSED "w" WHEN YOU WEREN'T SUPPOSED TO!

Yeah, I'm kidding. You got "No such file or directory", didn't you? It's OK, nothing bad happened. Vi just can't write this file because of those crazy directory names we used. I stacked the deck to deliberately create this problem for you.

Great. So you've typed 10,000 words of deathless prose that's due on your boss's desk NOW, and you can't write it. Real amusing, right?

Naw. You can write it, you just can't write it to /ect/frammif/mkil. How about we write it to /tmp/myfile.safe instead? To do that, simply hit ":" again so you are back at the bottom, and this time type "w /tmp/myfile.safe". You get back something like

"/tmp/myfile.safe" 3 lines, 64 characters


Are you worried what would have happened if your boss had an important file named "/tmp/myfile.safe"? Did you just overwrite that file with a bunch of stupid "brown fox" gibberish? Can you do ANYTHING right?

Stop sweating. It wouldn't have happened. Try it again. Type ":", then "w /tmp/myfile.safe". See? It won't overwrite an existing file unless you type "w!". You might also want to know that if vi says a file is read-only, but you should be able to write it anyway 'cause you are the superuser, the "w!" trick fixes that, too.

Warning!

That won't save you from complete stupidity. If you had started this session by giving the name of a real file (like "vi /etc/inittab"), and then had deleted a bunch of lines and added a bunch of new ones, and then typed ":w" (with nothing else, no name, just the bare "w"), vi would have happily, efficiently, and mercilessly overwritten /etc/inittab with your changes. The theory here is that you saw what you were doing, so you must know what you were doing. So be it.

But let's say you messed up the file and you don't want to write it, you just want to quit. Let's try it: mess up this file a little more. Delete a line, add a line, it doesn't matter, just do something. Now do the ":" again, and type "q" (for "quit").

Gotcha again. But notice that vi has given you a hint about what to do. It tells you that you need to type ":quit!" to get out. Actually, you just need ":q!", but you can type it out if it makes you feel better.

That's it. You know the basics. I wish you'd learn more, 'cause it's worth it, but if this is all you can take, it is enough. Quick review and we're out of here:

  • "i" for insert
  • "o" for open
  • "dd" delete line
  • "x" remove characters
  • "u" undo
  • "p" put
  • ":w" write file
  • ":w!" write absolutely
  • ":q" quit after saving (combine with ":wq")
  • ":q!" quit without saving

Don't get into the habit of using "ZZ" to leave vi. This writes your changes and exits, and someday you'll have meant to exit without writing. Get used to :q! and :wq and you won't get bitten.

Worse, ZZ has another nasty quirk. Let's say you are editing along and you decide to make a copy of the file with ":w filecopy". If you now tried ":q", it would refuse, warning you that you haven't written your changes. ZZ will not (because you wrote "filecopy") and will just quit WITHOUT WRITING your file. If you don't know that, you might ship your (un-edited) file somewhere else and maybe delete "filecopy"..

It is also all to easy to hit CTRL-Z by mistake, throwing you into background mode - if you don't realize what happened, you might think you lost your edits. You didn't; just tye "fg" to get back to your session.

Searching

You can search for text with a "/". Just type "/" and whatever you want to search for. Some things are a little harder - you can't search for a "(", "[" or "{" that way - use "/[(]" instead (yes, "/[[]" works). If you then want the matching brace or paren, use "%" to move to it.

If you want to learn a lot more about "vi", try these Vi & Vim Tricks and Tips at GeekStuff.com.

Command line editing

By the way, I can tolerate emacs style command line editing, but honestly I would much rather use vi style - I basically detest emacs and am much more accustomed to vi in general, so why should I have to suffer emacs in Bash?

It turns out that it's very simple to do - you just have to unset INPUTRC and then tell Bash "set -o vi". You want those in .bashrc so they stick everywhere, but that's all you need. From that moment on, no more emacs in your command line editing!




If this page was useful to you, please help others find it:  





3 comments




More Articles by - Find me on Google+



Click here to add your comments
- no registration needed!




Thu Aug 11 07:47:02 2005: 961   anonymous




I feel completly tarded in unix based editors and your primer here is an absolute gem, thanx!

Please god can some one point me in the direction of a primer on searching from command line, I mean what is grep and how, why would I use | (pipe) ?

How do I make unix do what all my unix based friends can make it do? I feel like all those years in dos and on windoze have crippled me for the real world... RIH Bill gates, I'll win this battle and never look at windoze again!

Great primer thank you...



Thu Aug 11 08:53:55 2005: 962   TonyLawrence

gravatar


Grep is for searching inside text. It's often used to extract specific info from a file or from the output of another process:

grep Saturday billings_by_day.txt

ps -el | grep runcbl


That kind of thing. Do a search for "grep" here and you'll see all sorts of real examples.



Wed Jun 7 15:22:33 2006: 2077   arvin




Just THANK YOU!

Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar

Kerio Samepage


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

I am a Kerio reseller. Articles here related to Kerio products reflect my honest opinion, but I do have an obvious interest in selling those products also.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.

pavatar.jpg

This post tagged:

       - Basics
       - Editors
       - Shell















My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!


book graphic unix and linux troubleshooting guide



Buy Kerio from a dealer
who knows tech:
I sell and support

Kerio Connect Mail server, Control, Workspace and Operator licenses and subscription renewals



Click and enter your name and phone number to call me about Kerio® products right now (Flash required)