Tag Archives: vim

vim arrows in MacOSX

I know vim gurus would criticize me for using arrows in vim's insert mode but it's really hard to give them up.

I have this problem when I connect from my linux box to a MacOSX or FreeBSD box over ssh. I find it one of the most annoying things when using vim. When you are in insert mode and hit one of the arrows to move around, instead of the expected action vim will just print A, B, C or D on a new line. This makes vim practically useless.

So either you are very careful and always exit the insert mode before you move or fix the keys.

I think it's hard to always remember to get out of insert mode and it's one extra operation you have to do that I find useless not to mention you will probably have to enter insert again a few seconds after that.

So here's the fix for the arrow keys.  Edit vimrc either the global vimrc ( I'm using vim from macports so my vimrc is /opt/local/share/vim/vimrc ) or ~/.vimrc like this:

$ vim ~/.vimrc
set t_ku= (now type Ctrl-V and press cursor up)
set t_kd= (now type Ctrl-V and press cursor down)
set t_kr= (now type Ctrl-V and press cursor right)
set t_kl= (now type Ctrl-V and press cursor left)

This solution was stolen from vim tips wiki. I posted it here to avoid looking for it again if I need it. It's the second time I am hit by this problem and every time I had to search through a few pages with solutions that didn't work for me

vim windows and tabs

Tabs are great, I don't know what I would do without tabs, they are everywhere, your browser has them, your editor has them, even your webmail has them ( see yahoo mail ). Well I just discovered vim has them too.

Before windows and tabs

I don't know why I never thought of this before. I was always opening each file in it's own vim session, but that's hard to work with, you can't copy/paste from one file to another using vim commands ( my, y`y, P ) , I used the mouse for that ( not the best choice ). And to switch from one session to another I had to always do CTRL-Z ( to put current session in background ) then fg x to put another session in foreground. That was until I discovered screen split.

Multiple windows

With vim you can split the screen vertically or horizontally and open a different, same or new file in each screen.

Pplit the screen horizontally - :sp filename or :new [filename]

Split the screen vertically - :vsp filename or :vne[w] [filename]

If you use :sp/:vsp with no filename specified then the current screen will be split in half and it's content copied to both screens.

If you use :new/:vne with no filename then it will just create a new empty window.

Move to the next with : CTRL-W j or CTRL-W <down arrow>

Move to the previous window with: CTRL-W k or CTRL-W <up arrow>

You can also jump more then one window by using a number after CTRL-W. For example CTRL-W 3 j will take you to the third window from your current position.

This was great for visually comparing sections of files or to do copy/paste , but you're limited by the screen size. If you split your screen in 10 you're not going to see much from any file.

The Tabs

To open a new tab :tabnew [filename]

You can move between tabs with CTRL-PageDown and CTRL-PageUp or type a number before CTRL-PageDown or CTRL-PageUp and you'll jump that number of pages.

You want to close all tabs except the current tab? :tabo

Close current tab - :tabc

Close n'th tab: :tabc n

There are a few other commands for working with tabs but it's likely you'll never remember all of them so I'm not going to write about them. there's always vim built in documentation for that.

Never underestimate the power of vim!

Do you know good vim tips and tricks ? Care to share in the comments?