How to Reassign Cursor Keys in vi & vim

While some people might take exception to any change to the classic vi Unix cursor keys, you might be a vim user who wants to change them up. The nice thing about Linux, BSD and Unix as a whole is that you have the final say about everything. Many gamers like to use the WASD keys as cursor keys, and you might want to use these instead of HJKL. There are also a few other interesting combinations you might like to try, and you can try any of these.

The process isn’t too involved, but it does require the command line. If you’re used to using vi and vim, then you’re already familiar with how to bring it up. Click on the K menu in KDE or the Whisker menu in Xfce4 and click on Terminal in System Tools. You can find it in the same place on the Applications menu in GNOME Shell and LXDE. Ubuntu Unity users can search for the word Terminal on the Dash, or you can use Ctrl+Alt+T to bring one up.

Method 1: Assigning the WASD Keys to vi & vim

Since the WASD keys already have something assigned to them in vim, you’ll want to use a modifier key. It would be easy enough to hold down Alt and these keys, but we need to make sure that your terminal emulator doesn’t already assign those shortcuts to something. At the terminal, type cat and push enter. Push Alt+W, Alt+A, Alt+S and Alt+D to see if some weird command codes come up. You might also notice that some menus drop down as we did when we did this with the xfce4-terminal that we were using for taking screenshots.

Type Ctrl+C to exit, which might also cause some odd unprintable Unicode characters to splatter onto your terminal. You can safely ignore these. Instead, type vim ~/.vimrc to load up your configuration file. Hold down the j key until you get all the way at the bottom and then type I to enter insert mode.

Push enter to get an extra line, and then what you want to put in depends on if your terminal emulator acted up at all. If not, then add these four following lines:

noremap <A-a> h

noremap <A-s> j

noremap <A-w> k

noremap <A-d> l

If you did have a problem, then instead try this:

noremap <Esc>a h

noremap <Esc>s j

noremap <Esc>w k

noremap <Esc>d l

In either case, push Esc and then type :wq to save your work. Try loading up vim again and see if you can now use the WASD keys when you’re not in the insert mode like they’re cursor keys. You’ll more than likely have to either hold down Alt or Esc when using them since those keys are already assigned, but other than that they should work just fine. This is the fastest way to do this without any additional playing around, but there are a few other methods you can try if this either didn’t work or if you’d prefer other key combinations.

Method 2: Using Alternate Alt Assignments

If neither of these techniques work, then type vim ~/.vimrc again to load up your rc file again and hold the J key until you’re all the way at the bottom. Type I to edit again and now change the bottom section to read thusly:

noremap ^[a h

noremap ^[s j

noremap ^[w k

noremap ^[d l

Some users report actually physically typing a caret (^) followed by an open bracket ([) while others say that they actually need to either hold down the Alt key combinations or, alternatively, copy and paste the command codes themselves. You can always type cat at a regular command line and push Alt+A, Alt+S, Alt+W and Alt+D to get these, which you can then highlight, copy with the Edit menu and then paste with the Edit menu into vim if at all necessary. Once again, this method is unnecessary for most users and should be avoided. The first method uses far less playing around.

You can push Esc and then type :wq to save it. Open another file in vim and test to see if your preferred keys are working at this point. This should work even with many types of alternative terminal emulator programs.

Method 3: Using Other Key Bindings

You’re not restricted to the default HJKL keys nor the gamer-preferred WASD keys. Some people prefer to use other sets. In your .vimrc file, you can actually replace the section:

noremap <A-a> h

noremap <A-s> j

noremap <A-w> k

noremap <A-d> l

This gives you more choice. Some people prefer ESDF, which allows the little finger to touch other keys while you’re using these as cursor keys. You could use the following in your file if you’d prefer:

noremap <A-s> h

noremap <A-d> j

noremap <A-e> k

noremap <A-f> l

Some people would prefer the IJKL diamond on the other side of the keyboard. You may be interested in this configuration:

noremap <A-j> h

noremap <A-k> j

noremap <A-i> k

noremap <A-l> l

Note that this one is slightly more complicated because it employs some of the keys that the standard HJKL bindings would use, but it should still work the same way. If you’re interested in these alternate bindings, then you might have fun experimenting with some. Gamers who use other combinations will sometimes substitute these and there have even been some Unix hacker types who configure vim to work with USB controllers. Most users will find the HJKL and WASD keys more than sufficient, however.


Kevin Arrows

Kevin is a dynamic and self-motivated information technology professional, with a Thorough knowledge of all facets pertaining to network infrastructure design, implementation and administration. Superior record of delivering simultaneous large-scale mission critical projects on time and under budget.
Back to top button