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.

Learning vim

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

http://tom.scogland.com/blog/2012/05/25/vim-basic/

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 – 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:

de

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. 😀

Vim & mc

When I worked at Valve Software, I had a chance to watch a physics simulations engineer work on some stuff. I did notice some of the tools he used: midnight commander and vim. I decided to give them a go and really learn them. It’s been about a week since I started learning vim and mc. So far they are great and I never have to take my hand off the keyboard to work. The learning curve is a little high, but there are tons of resources out there. Also, the fact that you can customize vim a lot is really useful. I didn’t really like the color themes that came with the standard vim install, so someone recommended I check this site out: color scheme test

One cool thing that I haven’t tried yet is the viemu. Basically it allows you to use vim controls from within visual studio. The only thing I would use visual studio for is intellisense to get to know an API better. But I also found out that someone created an intellisense type of plugin for vim anyways. I’m not ready for it yet, but it may prove useful in the future.

On a side note, although you can use vim, I recommend gVim instead, simply for the wider array of color scheme support. Currently I work from within mc and I have mc execute my windows binary of gVim. So far it seems to work pretty well. Periodically when I exit out of gVim, cygwin isn’t the top window, which is annoying. Alt-tab fixes that issue though.