Vim – Jumping around the page

These are my favorite shortcuts for jumping around the page.

z+t = Scroll the current line to the top of the screen

z+b = Scroll the current line to the bottom of the screen

z+z = Scroll the current line to the center of the screen

H = Move the cursor to the top (high) of the screen.

L = Move the cursor to the bottom (low) of the screen.

M = Move the cursor to the center (middle) of the screen.

Plugins for XCode

I’m very happy that there’s a good way to use all the vim stuff within XCode. I’ve been using it for quite a while, and there was nothing I couldn’t do, but I’m also not a super advanced user.

XVim for XCode

I recommend building it from the github repo. As of right now, the current version has a crash bug in it.

Since this post is really about plugins / extensions, I recommend downloading Alcatraz. Alcatraz has quite a few extensions including VVDocumenter, which helps to produce doxygen and appledoc compliant comments. You can change themes, and other useful stuff to make working with XCode suck less.

Learning vim

I found a pretty good tutorial on how to use vim.
It walks through some basics and some other useful stuff.

VsVim for Visual Studio 2012

Since I started using visual studio 2012 in April, I have discovered that the vsVim plugin extension is very very good. I highly recommend it if you want to swing that way. It’s not perfect, but it’s much more functional than it used to be and it’s frequently updated, which is great. I use it at work and I find that using vsVim, and visual studio hotkeys allows me to work without ever having to touch the mouse. These are the commonly used hotkeys that I use to navigate around:

Ctrl+S (I installed the “switch” extension) – navigates between .h and .cpp files
Ctrl+Tab – Navigates between sources files. Just pressing once goes to the last file used. Pressing multiple times allows you to navigate.
Ctrl+; – Searches for the name of a file in the project.
Ctrl+Alt+F – standard file search.
F5, Ctrl+F5, F7, Ctrl+F7 – Build commands.
F12, Ctrl+F12 (go to definition/declaration)

vim – Jumping around in code

Sometimes you want to jump back to an area of code, so it would be nice to have a bookmark or something similar handy. Well, vim has this feature too.

  • m<someletter> – This will “mark” this particular spot for you to return to later.
  • <someletter> - The backtick and the letter you used to mark that spot will jump to that location again.
  • . – Backtick and period will jump back to the last edited spot. Useful if you edit something, go to a different area to look at something and need to jump back quickly.

Don’t forget to use “zz” to make your life easier as you jump around.

vim – Navigating around code blocks quickly

You can use the following to quickly navigate around code blocks in vim.

  • [{ – Jump to nearest inner code block ( { curly brace ).
  • ]} – Jumps to the nearest inner code block ( } curly brace ).
  • [[ and ]] – allows you to quickly jump from function to function within the file.
  • % – Jumps to the opposite bracket or brace you are currently on.

You can keep pressing [{ to keep going out a scope. Then you can jump to the other using % if you want.

I have my space bar mapped to “zz” in my vim config file. zz allows you to center the text on the screen, which is very useful when navigating around code files like this.

vim – Quote something quickly

This is actually two tricks. Surrounding and highlighting something easier.

Step 1: Install the “Surround” vim plugin
Step 2: type the following: viwS”

This means, go into visual mode, and select the inner word, then surround it with quotes.
If you use “W” instead of “w”, it will select the whole chain of words.. Or highlight them.

If your cursor is situation on the word “car2” and you use:
lower case w: car.car2.car3 will select car2
upper case W: car.car2.car3 will select car.car2.car3

vim – Scrolling in vim

zz – centers the current line cursor is on, to the middle of the screen
zt – top
zb – bottom

Since the space bar isn’t use in normal mode, map zz to it. This makes it easy when using “N” or “n”. Then you can quickly press space bar and it will center the found item to the center of the screen.

Put this into your .gvimrc file: nmap <space> zz

You could also do the following:

nmap n nzz
nmap N Nzz

You can also change how it scrolls by setting the amount of lines from the edge of the buffer you can go before it starts scrolling

:set scrolloff=1000 

This scrolls whenever you move the cursor, but you can set it to 10, for example, to scroll if it comes within 10 lines of the edge of the buffer.

vim – Quick vim tip

I’ve always used the “w” key to go forward a word when highlighting stuff. There’s a problem with this though when you want to just copy a single word.

Lets say you have the sentence “you are a car.” Let’s also assume you have placed your cursor on the letter “a” in “are.”

And we want to highlight the word “are.” So we press “vw”. When you press the letter “w”, it highlights the following: “are a”. Note that it includes a space and the letter a.

So the letter “w” is the wrong key to use, so what is the key to go to the end of the word instead?

It’s the letter: e

vim – Vim tip 34534

I’ve been looking for a way to delete up to a character, but not including the character from within vim, and I finally figured out how to do it.
Lets say you have something like this:

If the cursor is at the beginning of the line, and we want to change the name of the array completely, we could type this:

This will delete all the way to the bracket. Unfortunately, it deletes the bracket.
So instead, this works nicely to NOT remove the character you type:

This will delete every character up until the bracket, but leave the bracket. How nice.
So what does this “t” movement key do exactly?
It simply moves the cursor to the character right before the one specified, and it stays in “normal” mode.
If you wanted to immediately type after deleting the characters, you would use the following:

Which will delete the text, leave the “[” character, but change to insert mode right before the “[” character.

Another thing you can do is type:


vim – Monthly vim tips

If you want to search for the word right where your cursor is, you can do the following:

If you want to maximize your vertical or horizontal buffer splits, type one of the following:

If you want to equally resize all of your buffers, type the following:

To make a vertical split a horizontal one, or visa versa, type the following:

To center the cursor on your screen, press:

To bring up the build in “file explorer” window, type one of the following:

vim – Resizing your vim buffer

To resize your buffer up and down type the following:

To resize your buffer left and right, type the following:

You could make a mapping to do these without pressing the ctrl-w key combo first.
Something like this for the vertical stuff:

You have to put that in your .vimrc file.

vim – How to quickly comment out entire sections of code

Go to the first line and type the following:

^ to go to the beginning line. (probably want the top too)
Ctrl + Alt + V (to enter block highlight mode)
#j (where # is the number of lines to be commented out) Or just move the cursor manually.
I (capital i)
Press the esc key.
They should all be commented out now, with c++ style comments.

vim – Remove trailing whitespace

Last blog post, I mentioned you could clean up and reformat a file by using :retab.
This doesn’t remove unnecessary trailing whitespace though. To remove trailing whitespace, use the following:

vim – Quickly convert all tabs to spaces

Make sure, in your .gvimrc file, that the following are set to your liking:

Then type the following within vim:

vim – A few more vim tips

retrieves a yanked/deleted entry before the last yanked/deleted entry

Ex: yank/delete something, then yank/delete something else. Then type “0p or “1p

Count the number of words in the file of <word>

vim – Changing text between brackets, parens, etc. quickly

Vim tip: Move your cursor between any of set the following: (), “”, [], {}, etc.

Then type one of the following:  

Or any combination. it will remove or change or yank all of the text between those braces.

vim – How to reload your .gvimrc file without quitting gVim

At work I modify my .gvimrc file, and it’s a big hassle to have to quit mvim (mac) and reload it. So a neat command you can do is to type this:

This will reload your .gvimrc file without leaving gvim.
This should work with regular vim too.

vim – Vim link dump

I’ve been using Vim for quite a while and I love it. So I have placed a bunch of useful ViM links here. If you’re a programmer, I highly recommend learning Vim. Unfortunately Vim doesn’t have very good intellisense like functionality. So if you’re on linux, use NetBeans and the jvi plugin, and if you’re on windows and you want to stick with Visual Studio, get the viEmu extension (not free).

Read the rest of this entry »

vim – Vim Emulators

NetBeans: jvi
For VS2010, free but sucked last time I checked: VsVIM
For VS 2010/2008/2005: ViEmu

UPDATE: VsVim is now the best one. It no longer sucks. 😀