user658182
user658182

Reputation: 2350

In Vim, how can I switch the functionality of 'i', 'j', 'k' and 'h'

I am 5 or so weeks into learning Vim for development, and while things are starting to sink in, I have one personal issue that is frustratingly still tripping me up.

I am constantly using 'i' to move 'up.' 'k' is down, 'j' and 'l' are left and right respectively.

I know this all comes from playing too many years of PC games where these were direction movements. Honestly, I don't want to fight it anymore, and I would rather do what is extremely natural for me. How can I remap these so the above are the direction movements and 'h' is insert?

2 issues: How can I permanently store these in my local settings for development on my own machine, and how can I also quickly switch these when I am on someone else's machine for the time I am working on it. I mean, is there a set of commands that I can invoke and then they be erased when I shut Vim down?

Upvotes: 1

Views: 1422

Answers (2)

Kent
Kent

Reputation: 195039

DON'T DO THAT MAPPING! It is even worse than using arrow keys

first of all, I would suggest you learning the vim way to move your cursor.

Back to your question, remapping h,j,k,l is easy. However, it makes no sense to do those mapping (and you shouldn't do it), it won't make you work faster, it just drives you crazy when you use your vim. You know, editing with vim doesn't mean

  • in NORMAL mode, just do cursor moving
  • entering INSERT mode, do actual text editing
  • back to NORMAL mode to save the file

There are a lot of vim magic you could do in NORMAL mode (that's why it was called NORMAL). with your mapping, you will really have headache when you want to apply those. Let's take an example. Say you have already set mappings for your needs. i,j,k and h. We have a line:

function foo (String arg)

when your cursor is between (and ), you could type ci( to change the stuff in brackets. However your i means k. What would happen? the current line and the line above it will be removed!! Well you could say, you will remember to type chw, dhw,ch(,dhw... So you have to remap those keys also in your mind. :)

Also when you want to "borrow" someone else's .vimrc, or use some plugins, you will have problem too.

You see the point by the simple example above. So please think it twice before you remap those basic keys.

Upvotes: 10

Neil Forrester
Neil Forrester

Reputation: 5241

Put this in a file called ~/.vimrc:

noremap i k
noremap k j
noremap j h
noremap h i

If you're using vim on someone else's computer, you can type the same four commands at the prompt (type : before each, and enter after each), and the settings will apply for 1 session only.

Also, no is short for noremap, so you can shorten the whole sequence to

:no i k<enter>:no k j<enter>:no j h<enter>:no h i<enter>

when you're using someone else's computer.

Upvotes: 7

Related Questions