Getting shit done on Windows, part 2.1: being awesome with gVim

Getting shit done on Windows, part 2.1: being awesome with gVim

Aug 01
Getting shit done on Windows, part 2.1: being awesome with gVim

Right, so you’ve heard about Vim. Those who use Vim refuse to use anything else. And, to be perfectly honest, anything else decidedly lacks awesome. Awesome people use Vim. Even on Windows.

Reading Vim tutorials can be scary; they usually start out by telling you that you need to know a bunch of commands before you’re going to get anywhere… and that’s pretty much true. It’s surprising though how quickly you pick it up if you pay attention so I’m going to give you what you need to be awesome quickly. Ok, more awesome than not at all awesome. Which you are now. Not at all awesome. It’s what all people who don’t use Vim are. But we have the technology, we can change that.

There are four things you need to be awesome with Vim on Windows: gVim (obviously), NERDTree (more on that later), an awesome colour scheme and some commands in your head.

Being awesome: gVim and Windows 7

Being awesome: gVim and Windows 7

gVim: Getting it

You can download if from here: http://www.vim.org/download.php or you can download it from me here: gVim 7.3

Once you’ve downloaded gVim, you obviously install it. Right? Right.

It installs to Program Files/Vim (or Program Files (x86)/Vim) by default and the executable should be in Program Files/Vim/vim73 (or exchange the number for whichever version you’ve chosen to install).

gVim: The 2 minute intro

Vim works with ‘modes’ which is different from most editors, certainly any editor you generally come across in Windows.

The modes are:

  • Command mode: you enter commands to do things (like save and quit)
  • Insert mode: type text into text editor
  • Visual mode: visually select things, like a sub mode of Insert mode

Right, you’ve run Vim but now what? You can’t type anything!?! That’s because you’re in command mode and you don’t know any commands. To type some text press either ‘i’ or the ‘Insert’ key. If you have a Mac keyboard on your Windows machine and you haven’t re-mapped some keys, use ‘i’. If you do want to re-map some keys (and let me assure you, on a Mac keyboard on Windows, you do), read this: http://shawn.hamman.co.nz/2011/07/getting-shit-done-on-windows-1-make-a-mac-keyboard-useful/

gVim: Typing some stuff

So you’ve pressed ‘i’ or ‘Insert’, now you’re in insert mode. To get out of insert mode, press escape. Press it a couple of times just to be sure. Hammer it. Makes you feel better. Makes you feel good. You’ll find yourself pressing the escape button a lot. In fact, before you do anything in gVim you’re going to find yourself beating the escape button two or three times. This is normal. Learn to live with it. Most people who use Vim beat the escape key repeatedly while thinking. Other awesome Vim users can gauge your level of awesome by the way you obsessively stab at the escape key.

So as you’ve probably guessed by now, the standard process in Vim is: press ‘i’, then type stuff, then press escape (repeatedly). That is the process. Always remember to beat the escape key after typing anything and you’ll do fine.

gVim: Commands and making Vim do your bidding in 5 minutes

Commands in Vim usually start with a colon ‘:’ followed by a letter or a word. Commands are how you do things like save and quit (and a bunch of other stuff but we’ll get there). Pay attention to the case of the letters, they matter. ‘U’ and ‘u’ are not the same thing (‘U’ being Shift+U and ‘u’ being just plain vanilla ‘u’).

To type something and then save it, you would do this:

  • Press ‘i’ or Insert
  • Type some stuff
  • Repeatedly hammer the escape key until you’ve satisfied your OCD that you’re no longer in insert mode
  • Press ‘:’
  • Press ‘w’ (w is for write)
  • Press <space>
  • Type a file name, ‘c:\sandbox\test.txt’ for example. Assuming of course you have a ‘c:\sanbox\ directory that happens to be writeable by your user anyway…
  • Press <enter>

The command looks something like this:

<esc>:w c:\sandbox\test.txt<enter>

If everything went well, you should have a text file called text.txt in your ‘c:\sandbox’ directory.

Now, if you want to save it again, you just have to type the command below and it will save to the same file:

<esc>:w<enter>

If you want to quit out of Vim:

  • Repeatedly hammer the escape key until you’ve satisfied your OCD that you’re no longer in insert mode
  • Press ‘:’
  • Press ‘q’ (q is for quit)
  • Press enter

Vim should be gone. So a quit looks like this:

<esc>:q<enter>

Lets say you want to quit without saving. Vim prefers you to not lose your stuff so Vim will complain that you have un-saved work and Vim wants to be damn sure that you are damn sure that you are willing to throw it all away. To force Vim to do your bidding and quit without saving:

  • Repeatedly hammer the escape key until you’ve satisfied your OCD that you’re no longer in insert mode (see where I’m going with this?)
  • Press ‘:’
  • Press ‘q’ (q is for quit)
  • Press ‘!’ (! for force)
  • Press enter

Vim, and your work, should be gone. A force quit looks like this:

<esc>:q!<enter>

Of course, you might want to save and quit at the save time, you can combine the Write and Quit commands like so:

<esc>:wq<enter>

To force a write/quit:

 <esc>:wq!<enter>

gVim: A shortcut to more awesome settings and a decent color scheme

Download this zip file: vim_files.zip

It contains two files:  ‘_vimrc’ (Vim settings file) and ‘pastafarian.vim’ (a Vim color scheme file). Extract them and copy as follows:

Copy ‘_vimrc’ into your home directory, on Windows 7 default it’s:

c:\Users\<username>\

Copy ‘pastafarian.vim’ to the ‘colors’ directory in your Vim install directory:

c:\Program Files\Vim\vim73\colors\

If you’re in Vim, restart it by exiting and starting it again.

The screen should now be a beautiful dark blue and Ctrl+c and Ctrl+v should be mapped to work as expected on Windows. If it isn’t… come on dude, it’s just copying a couple of files. Seriously.

If you edit the _vimrc file and you scroll to the bottom, you’ll notice I’ve mapped some function keys to NERDTree functions. Worry not, we’ll get to that eventually.

To edit the _vimrc file from Vim:

<esc>:e ~\_vimrc<enter>

gVim: The basic commands you need to be more awesome than not at all awesome

If you’ve installed my settings file and color scheme, copy and paste are Ctrl+c and Ctrl+v respectively, as you would expect on Windows. Also, technically, the escape button isn’t part of the actual command sequence but I put it in because you need to be in command mode to type a command. Beginners forget so you might as well get it right from the beginning.

How to undo (note – no colon and no enter and you can hit ‘u’ as many times as you want after then first <esc>):

<esc>u

How to redo (same thing, one <esc> and as many <ctrl>+r as you want):

<esc><ctrl> + r

Create a new tab:

<esc>:tabnew<enter>

Cycle between tabs (no colon and no enter after command):

<esc>gt

Save (or write) the current file:

<esc>:w<enter>

To quit (and mean it):

<esc>q!<enter>

Go to the end of a word:

<esc>e

Go to the end of a whitespace-delimited word:

<esc>E

Go to the beginning of a word:

<esc>b

Go to the beginning of a whitespace-delimited word:

<esc>B

Go to the beginning of a line:

<esc>0

Go to the first non-whitespace character of a line:

<esc>^

Go to the end of a line:

<esc>$

Go to the first line of the screen:

<esc>H

Go to the middle line of the screen:

<esc>M

Go to the last line of the screen:

<esc>L

Go to a specific line number: <esc>:NN<enter> where NN is a place holder for a number. Go to line 99:

<esc>:99<enter>

Go to line 5:

<esc>:5<enter>

Delete a line:

<esc>dd

Delete from the cursor to the end of a line:

<esc>D

(which is, <esc><shift>+d, no <enter> required)

Delete a bunch of lines: <esc>:dNN<enter> where NN is a place holder for a number.  Delete 5 lines:

<esc>:d5<enter>

Delete 15 lines:

<esc>:d15<enter>

Set line numbers on:

<esc>:set nu<enter>

Set no wrap on:

<esc>:set nowrap<enter>

Search for something: <esc>/PATTERN (where PATTERN is a string or a regular expression). To search for the word ‘root’:

<esc>/root<enter>

To go to the next found instance:

<esc>n

Search and replace can be complicated. This example will assume you want to search for a string, replace with a string in the entire file in one go. Go big or go home. Limits in a search and replace are for small minds. Maybe. Anyway, search for ‘foo’ in a file and replace all occurrences with  ‘bar’:

<esc>%s/foo/bar/gc<enter>

a<enter>

That second ‘a’ is for ‘all’. Be a man. Replace the lot… you can always undo with ‘u’ right? Right.

gVim: …and in conclusion…

That’s all for now. In the next post I will cover NERDTree, it is the final key to attaining unimaginable awesome. Unsurprisingly, NERDTree is called ‘plugin 42’ for good reason! It is the answer to… actually, I just made that shit up. NERDTree, it completes you. Seriously.

 

 

 

15 comments

  1. If you want the latest and greatest gvim with all the latest patches, you can get it from here:

    https://sourceforge.net/projects/cream/files/Vim/

  2. shawn

    Cheers cheers, thanks for that.

    I wonder if they’ve fixed the OSX version of gVim that stopped working with Lion…

  3. Hello, nice article. I look forward to see your take on the NERDtree plugin. I have just installed it but I haven’t found much use for it yet.

  4. shawn

    Thanks for the comment; perhaps I’ll get around to the NERDTree bit tomorrow.

    I’ve mapped F2 to toggle NERDTree on/off and F4 to open my Sandbox and F5 to open my Dropbox. I find browsing for files and swapping between them much easier with NERDTree.

    That you can create and delete files and directories with it is also pretty awesome.

  5. semaj

    Holy cow, did you type that much?!

  6. semaj

    Just in case if anyone uses Mac, there is a MacVim which is similar to gVim

  7. Scott

    “Visual mode: visually select things, like a sub mode of Insert mode”

    Actually, more a submode of normal (command) mode, since normal mode commands are used on the visual selection.

    “Press ‘!’ (! for force)”

    ! is the damnit command ;^} For instance, :q! means “quit, damnit.”

    “If you’ve installed my settings file and color scheme, copy and paste are Ctrl+c and Ctrl+v respectively, as you would expect on Windows.”

    Argh! Dude, this is a modal editor; it doesn’t need no stinkin’ chorded commands, !. They just hinder learning the Vim single-key commands, y and p, by encouraging modeless editor habits. They force you to take your hands away from the home row and your eyes from the text on screen to ensure you are hitting the right keys. This interrupts your flow.

    A better substitution is to swap the caps lock and escape keys so your pinky can easily invoke escape without a long reach. Then, switching modes for issuing commands is much easier, and there is no need for key maps that use multiple keys simultaneously. You should also be able to map escape to something easier to type, like leader-[, or even leader leader, which on my system becomes ,,.

    Then, a better approach is to use leader-c and leader-v or leader-y and leader-p to either match the Windows keys or the Vim keys without chords. I use the latter to map “+y and “+p for copying to and from the system clipboard. I also use: let mapleader = “,” so I don’t have to use \ for leader. The commands become ,y and ,p. This works very well in visual mode. In normal mode you can do things like ,yas to yank a sentence to the system clipboard, or ,yap to yank a paragraph. This is all without moving your hands from the home row or your eyes from the text.

    “[esc][ctrl] + r”

    One of Vim’s few chorded normal mode commands. I remap it to leader-r.

    Vim’s commands can seem elementary and somewhat limiting until it is understood that they are a mini-language of nouns and verbs designed to be combined. So you can not only move the cursor with the w, W, e, E, b, and B noun commands. But you can also combine them with the verbs d, c and y, among others, to delete, change and yank. Include numbers to say how many, such as d3w to delete three words. 3das to delete around three sentences and so forth. Verbs can work with arbitrary nouns. Nouns can work with arbitrary verbs. And both can be multiplied an arbitrary number of times: 3d2w to delete two words three times, which is the same as either 6dw or d6w.

    :wq

Trackbacks/Pingbacks

  1. Getting shit done on Windows, part 2.2: Using NERDTree to level up your awesome | Shawn Hamman - [...] since you’ve obviously read my previous post on being awesome with gVim, taken it all to heart and have…
  2. My Vim Key Maps | James Lin's Blog - [...] to vim, if you would like to do the same, please spend 10 minutes of your life to read…

Leave a Reply