Vim

From XOmB wiki

Jump to: navigation, search

vim is one of the 'big two' text editors. The other is emacs. The argument over which is better is one of the great *nix holy wars. We don't take a side, we have developers using both.

Contents

Why vim?

I'll stay away from the "Why vim instead of emacs" part of this question, and ask a slightly different one: "Why vim rather than Eclipse, or Netbeans, or Visual Studio?" There are a few reasons, actually:

  • Text Editing is fundamental
  • You should always use the best tool for the job

As a programmer, we wear a lot of hats. But in the simplest sense, most of what we do is editing text. Yeah, there's quite a bit of thinking involved, but most of the time, we're typing things. When we code, we're typing things. When we compile things, we're typing things. When we use our programs, we're typing things, and when we need to communicate with others via the Internet, we're typing things. Typing, typing, typing. It's what we do.

We're all (or at least should be) familiar with Amdahl's law. It has a lot of different implications, but here's the one we care about:

We can achieve the greatest amount of speedup by focusing on the largest part of the problem.

I'll just leave this here: Image:Amdahl.png

In this case, imagine that the red portion represents our text editing abilities, and the blue part represents our knowledge of algorithms. If we can achieve just a little speedup on our text editing, it helps us more than learning dramatically more about algorithms.

This analogy is admittedly a bit simplistic. Programmers have more than two skills, but the general point stands. If most of what we do is editing text, than we need to become as efficient as possible at it. To become as efficient as possible, it makes sense to use the best tool for the job. This is why we don't code in Word. We're not worried about formatting (for the most part), we're worried about content.

Using Vim

Background Information

This tutorial is intended to be used by practicing. Start off by using The Bare Neccesities, and practice using that at first. Once this becomes second nature, move on to the next section, and add that to your repertoire.

Meta

This tutorial is aimed at the vim novice. Now, everyone is a vim novice, so you may be able to skip ahead a little. However, don't read this tutorial too fast. Google Tech talks has a great presentation on vim, and I'll be emphasizing a similar method to the one that he advocates in the talk. It's based around 3 easy steps:

  • detect inefficiency
  • find quicker way
  • make it a habit

Repeating these three steps over and over again will let you remember them. If you try to learn more than just a few features of vim, you will fail. You have to learn scales before you can run. Or something like that. The point is, without a solid foundation, you have nowhere to go. So practice, practice, practice!

The Bare Necessities

To open a file with vim, just type

vim filename

And now we're in vim! To quit, type this:

:q

You should see the ':q' appear at the bottom of the screen, and when you hit enter, you're returned to the prompt.

Now, vim is what we call a 'modal' text editor. It has several different modes, and they all do different things. We start off in 'command mode.' The colon indicates to vim that we want to tell it a command, and the q command quits.

Another mode is called 'insert mode.' This is where we do our typing. To go into insert mode from command mode, type 'i'. At this point, vim works the same way that most editors that you have used in the past have. Just type text as normal, and the text is inserted into the document.

To return to command mode from insert mode, press the Esc key. To save our work, we type this:

:w

This 'writes' our file. If we're also done editing, we can combine the two commands:

:wq

If you have made changes to the file, but don't want to save it, type this:

:q!

If we just type ':q', vim will complain that you have unedited changes, and you may not have wanted to quit. The exclamation mark tells vim that we're sure, discard the changes.

One last thing: Arrow keys will let you move the cursor around. Though this is a bad habit to get into.

Congratulations! You now know the absolute bare minimum needed to edit text files with vim. Your journey has started.

Here's a review of the workflow:

vim filename

opens a file for editing

i

enters into insert mode

Esc

goes back into command mode

:wq

saves and quits

:q!

quits without saving changes.

Advanced Movement

The first step to vim enlightenment is to not touch the arrow keys. Vim has a better way to move around your text than the arrow keys: 'h', 'j', 'k', and 'l'. These correspond to the left, up, down, and right arrow keys. Try it out: open a file, enter command mode if you're currently in insert mode, and go! When you're at the place that you wish to enter text, hit 'i' to go back into insert mode, type, and then hit escape to go back into command mode. Use 'hjkl' to move around.

If you're typing new text, you should spend most of your time in insert mode, but when you're editing text, you should spend more time in command mode. This is basically how it works. When you start a new file, you spend quite a bit of time adding new text, hence insert mode. When the file already exists, you're doing things to manipulate existing text, you're running more commands, hence command mode.

There are also other ways of moving from command mode into insert mode. 'I' will move the cursor to the beginning of the line and move into insert mode. 'a' will move the cursor one position forward and move into insert mode. This may seem weird, but it'll be useful in conjunction with some other key strokes, so remember it! 'A' will move the cursor to the end of the line and move into insert mode. 'o' will insert a new line below the current one, move to it, and go into insert mode, while 'O' will insert one above, move to it, and go into insert mode. See a pattern?

Some other quick movement keys: 'e' moves to the end of a word, while 'b' moves to the beginning. '^' moves to the beginning of a line, while '$' moves to the end. Just like with regular expressions!

Using these movement keys is really awesome! Moving around efficiently is the cornerstone, a strong foundation for all other vim skills.

Search

So you're starting to get a hold of this whole movement thing by now, I'm sure. That's great! You're already more proficient at some things than the casual vim user ever is. However, we've barely even scratched the surface! This lesson will increase our power quite a bit... we're going to cover searching!

Before I forget, I'm going to mention this next thing: if I don't explicitly say so, when I talk about a command, you should be using it in command mode. Remember how we spend most of our time there? That's the basic assumption, so if I give you something to do in insert mode, I'll say so explicitly.

To search for some text in vim, just type a '/' followed by your query:

/hello

and then hit enter. Vim will take you to the first character of the first instance of what you're searching for. Type 'n' to go to the next instance, or 'N' to go to the last instance.

You can use regular expressions in your searches, but they're a bit different than the PCRE regexes you're used to, so I won't cover them just yet. I never thought I'd say this, but regular expressions are a bit overkill in this context. Sometimes, you really need the power, but most of the time, you don't, so exercise your Google-ing skills to learn more.

I'll take this opportunity to segue into something completely different: settings.

We turn settings on in vim by using the ':set' command. Try this out:

:set incsearch
:set hlsearch

Now try the same thing, typing '/function' or some other phrase that is in your code. Now vim highlights your matches, and also searches as you type, incrementally. The results will be left highlighted until you type

:noh

to say 'no highlight, please.'

You have to type these options in every time you start vim, unless you make a .vimrc file. Just type in

vim ~/.vimrc

and add the text

set incsearch
set hlsearch

and bam! Every time you use vim, these options are set. Note that they don't have a colon in front of them.

I'll mention interesting settings from now on, and you can feel free to put them in your own .vimrc as you'd like. Make vim do what you want it to!

Oh yeah, one more option:

:set syntax

This turns on syntax highlighting. You're welcome.


Anyway...

There's another movement command that's similar to the search operator, and it's even more useful a lot of the time. It's the 'f' command, also known as the 'from' command.

Try this out (note the lack of a colon):

fs

This moves the cursor to the first instance of 's' that it can find to the right of the cursor. 'F' goes to the left instead.

This command combines with 'a' to produce our first interesting chord. A chord is a short sequence of keystrokes that combine for an interesting effect.

Find a line in your program that looks like this:

    int f() {

and type this:

f(a

See where the cursor went? See that you're now in insert mode? You've quickly moved from anywhere in the line to the argument list, and you're ready to type things in.

Chords are where vim efficiency comes from. Think of how long it'd take you to move your mouse over to where you're typing, clicking in the right place, moving your hand back to the keyboard, and then starting to type again. The vim alternative is to press three keys, two of which are on our home row. Not bad at all. Try this one out, too:

    int fofex(int x) {
f)i

There you are, moved to the end of the argument list and placed into insert mode! Isn't that sweet? Sooner than you think, typing these will be like second nature, and you'll be inserting text like a champ in no time at all!

Copy, Paste, Ranges, and (un|re)do

We've talked a lot about text, but not much about editing it. And one of the most useful abilities is copying and pasting.

To do a simple cut operation, do this:

dd

This deletes one line and saves it into a buffer. If we wanted to copy instead of cut, we use

yy

to 'yank' the text. Move the cursor elsewhere and type this:

p

to paste it. Pretty easy, huh? So why do you have to hit 'd' twice? Well, you can combine 'd' with other keys to delete things other than lines. For instance, the 'w' command moves forward one word, so to delete a word, we have this chord:

daw

Pretty easy, huh?

dk

deletes the next character.

dab
deletes a block of ( ... ) characters, while
daB
deletes a block of { ... } characters. Very useful for C family languages.

Change those 'a's to 'i's to delete an 'inner' block. Try it out!

You can prefex most vim commands with a number to repeat them. So

2daw
would delete two words, and
25dd
deletes 25 lines.

Ranges are also really useful! Try setting this option:

:set number

This turns on line numbers on the left hand side of your viewport. Now that we know what number certain lines are on, we can copy and paste really efficiently. Let's try out this scenario:

I want to cut the if block that's one lines 10 through 15 and paste it below line 5.

How do we do this in vim? Like this:

:10,15d
:5
p

It's that simple! Line ranges are really powerful! Let's say that in those lines of code we moved, there's a variable name that clashes with the function we moved it to. So we want to change all mentions of 'i' to 'j'. We can do that via this command:

:6,11s/i /j /

Now, when doing a substitution like this, we have to be careful. I've placed the space after the variables so that we don't change 'this' to 'thjs'. You can only do something like this when you see the code individually, and can determine which expression will do exactly what you want. However, by prefixing the substitution (s/search/replacement/) command with some line numbers, at least we've contained any possible damage. But what if you get that expression wrong, and it messes up your text. What can you do about it? Well, this is what:

u

This undoes the last change you've made. Redo is slightly different. It looks like this:

<C-r>

Don't type that one literally! Hold the 'control' key and press 'r'. That's common vim notation for that keystroke. This will re-do a change you un-did.

Macros!!!

The XOmB vimrc

Using Vim-style bindings with other editors

  • Visual Studio with VsVim: here
  • Having Vim understand Visual Studio: here
  • XCode (and most Cocoa apps): here
Personal tools
Panske parfemy štětec na makeup on lambreshop.cz. | best essay help | buy essay