Vim: Making those Arrow Keys Work With You (or Why the Anti-Arrow-Key Propaganda is Wrong ... )

Comments

12 comments posted
You sir, have convinced me.

I came here looking for a 'to arrow key or not to arrow key' answer and you made me realize that the only way to use Vim's awesome navigation even more is to disable the arrow keys. It's actually not that hard (got used to it within two days) and makes a lot of sense. So thanks for this post!

Posted by Martijn (not verified) on Tue, 10/04/2011 - 10:36
A little tip I found out by

A little tip I found out by accident:

If you're on a generic qwerty keyboard you can press 'alt + [movement]' to escape from insert mode and instantly do the movement and escape from insert.

Posted by Anonymous (not verified) on Mon, 08/22/2011 - 15:39
you missed something

Two things:
1) 'O' will, by default, add a new blank line above the current one and 'o' will add one below so no need to remap arrow keys for that.
2) the whole argument for using letter keys for navigation breaks down when you start using other keyboard layouts. Dvorak for example. Way too many macros and existing things depend on the standard keys for navigation, but on alternate keyboard layout those are way awkward. Yes, I could remap them, but then I'd have to translate every single tweak to vm that utilizes them.

Instead I've just learned the various navigation methods that you mentioned that are better than letter or arrow keys and then tweaked my arrow keys so that they actually work as expected on line breaks.

Posted by masukomi (not verified) on Wed, 07/13/2011 - 10:48
Especially True on Kinesis

Hello-

The point you are making regarding the real tradeoff regarding the arrow keys holds double for users of kinesis contour keyboards. Those keyboards are designed so that you can reach everything without moving your hand, so the movement reduction argument for hjkl is even less persuasive. It really is about forcing your brain to give up old bad habits and learn new good ones.

Also, I have a couple other nominations for arrow key repurposing:

Text bubbling:
http://vimcasts.org/episodes/bubbling-text/

Whitespace addition above and below:
http://www.vim.org/scripts/script.php?script_id=1590

Cheers,
Drew

Posted by Drew (not verified) on Mon, 10/25/2010 - 09:36
Great stuff!

Wow!
Thanks for sharing!
Awesome!

Posted by gmarik (not verified) on Fri, 10/08/2010 - 23:52
Hey, I like the arrows

Hey,

I like the arrows deleting and adding whitespace! I mostly use emacs, so here's my implementation. I'm not an emacs lisp expert, but it seems to work, and won't clobber your match-data. I think the left and right arrow keys aren't necessary since I love the way emacs indents (it's one of the reasons stopping me from using vim more often). I'm sure there's a way to get emacs indentation with a vim plugin, but I digress. Here's the code:


(defun delete-blank-line (arg)
(save-excursion
(save-restriction
(save-match-data
(next-line arg)
(move-beginning-of-line 1)
(if (looking-at "[ \t]*$")
(kill-line))))))

(global-set-key (kbd "") #'(lambda ()
(interactive)
(delete-blank-line -1)))

(global-set-key (kbd "") #'(lambda ()
(interactive)
(save-excursion
(move-beginning-of-line 1)
(open-line 1))))
(global-set-key (kbd "M-") #'(lambda ()
(interactive)
(delete-blank-line 1)))

(global-set-key (kbd "M-") #'(lambda ()
(interactive)
(save-excursion
(next-line)
(move-beginning-of-line 1)
(open-line 1))))

Posted by mtucker (not verified) on Tue, 09/21/2010 - 11:10
What about list browsing?

I tried to disable the arrows once, and did not last two days.
The problem for me is not insert mode navigation (I do not do it very much), but list navigation. I have lists in two places: insert-mode completion and FuzzyFinder-like file opening (via my own FuzzyFinder derived plugin). In both examples it's often much easier to hit up/down a couple of times instead of adding enough symbols for the completion to get the exact symbol.

Posted by Arseny Kapoulkine (not verified) on Wed, 09/15/2010 - 13:13
The arrow mappings requires some context-sensitivity

Hi Arseny,

My actual implementation now are not simple mappings like that given here, but a smarter ones (using functions) that are sensitive to context, window and mode. For example, when the buffer is non-modifiable/read-only, all that text-shifting stuff is disabled. The idea can be extended to support other contexts as well.

To be honest, though, now that I so rarely use the arrow keys for movement, I do find myself getting a little flustered or irritated when using plug-ins that do *not* allow the use of h/j/k/l (typically because they have been mapped to other functions) or at least `Ctrl-N` /`Ctrl-P` etc for movement.

For insert mode completion, I use `Ctrl-N` and `Ctrl-P` to move up and down. Works great especially as it takes `Ctrl-X Ctrl-N` to trigger the word-completion, which means that your finger is already on that key.

Here is some other code that I have in my `~/.vimrc` that makes it easier to use the completion menu (I particularly find that `C-Y` seems way out of my way to select a word, and so I like the `ENTER` mapping).

" Change the 'completeopt' option so that Vim's popup menu doesn't select the
" first completion item, but rather just inserts the longest common text of
" all matches; and the menu will come up even if there's only one match. (The
" longest setting is responsible for the former effect and the menuone is
" responsible for the latter.)
:set completeopt=longest,menuone
 
" Enter will simply select the highlighted menu item, just as <C-Y> does.
:inoremap <expr> <CR> pumvisible() ? "\<C-y>" : "\<C-g>u\<CR>"
:inoremap <expr> <C-J> pumvisible() ? "\<C-y>" : "\<C-g>u\<C-J>"
 
" Make <C-N> work the way it normally does; however, when the menu appears,
" the <Down> key will be simulated
"inoremap <expr> <C-n> pumvisible() ? '<C-n>' : '<C-n><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
 
" Simulates <C-X><C-O> to bring up the omni completion menu, then it simulates
" <C-N><C-P> to remove the longest common text, and finally it simulates
" <Down> again to keep a match highlighted
"inoremap <expr> <M-,> pumvisible() ? '<C-n>' : '<C-x><C-o><C-n><C-p><C-r>=pumvisible() ? "\<lt>Down>" : ""<CR>'
Posted by Jeet Sukumaran on Sat, 09/18/2010 - 11:28
`Alt up/down/left/right`

`Alt up/down/left/right` seems to work well as a backup for things like dropdowns

Posted by Matt (not verified) on Tue, 09/21/2010 - 05:45
not 100% I agree, but glad you came around

It took me forever to leave the arrow keys, but I was never one to move around in insert mode. I do experience fewer typos if I keep my hands in position vs moving them around for far flung keys like the arrow keys. That combined with the fact that each keyboard (laptop, work, home) have slightly different arrow key locations/configurations makes it a bit of a mess.

I even map 'jj' to esc so that I can keep my hands on the home row for moving back to command mode from insert mode.

Anyway, glad another one's come to the hjkl side of things. In case you're interested I've got some recent screencasts on some more advanced vim features at http://lococast.net

Posted by Rick Harding (not verified) on Mon, 09/13/2010 - 06:13
I've learned a lot by

I've learned a lot by watching other people use vim. Like, it took me an embarrassingly long time to realize what visual mode was for. I didn't even know how painful I was making things until I saw someone else slicing and dicing large blocks of code with a few keystrokes.

Posted by Mike (not verified) on Mon, 09/13/2010 - 06:09
Need to take it a step further (Vimperator)

Takes a bit of doing, but using the Vimperator in Firefox can make browsing the web more productive on pages full of links and info. But it requires a bit of mind shift from edit mode to make it work with that plug-in as well.

Posted by Anonymous (not verified) on Sun, 09/12/2010 - 19:10

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a biological visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.