Hacker News new | comments | show | ask | jobs | submit login
Vim - Putting arrows to use (codingfearlessly.com)
29 points by mmozuras on Aug 21, 2012 | hide | past | web | favorite | 40 comments

I believe many VIM users overestimate the importance of using h,j,k,l in comparison to numeric modifiers. Holding down j to scroll down the page is no more "right" than holding down the down arrow, in my opinion.

The following made me considerably faster:

* Relative Line numbering (For Vim 7.3). G/gg stop being convenient for frequent line movement once your file goes beyond 100 lines. Doing math in your head to figure out if line 589 is 16 lines from 605 takes way too much effort. Append this[0] to your ~/.vimrc, and then CTRL-l will toggle relative/absolute line numbering. So in relative mode, you see a line is 20 up from your cursor, and 20k takes you there with no guessing or holding down keys.

[0]: https://gist.github.com/3418841

* Learn the following: f,F,t,T. Combine them with c and d.

* zz => make current line appear in middle of the screen.

* H,L,M

* w,W,b,B. Combine with numeric modifiers (5dW)

* CTRL-u, CTRL-d, CTRL-f, CTRL-b

* % for block highlighting. Works well with curly braces by default, you may need to pick up extra plugins for ruby/python.

* g; will cycle back to the last places you were in insert mode. g, will cycle forward.

Even better, just learn to use EasyMotion, it's one of the most efficient ways of navigating around a file:


I've tried it and didn't like it at all.

The main problem is how it replaces the letter I'm searching with another unrelated one, making all the matches practically unreadable. To me, at least.

    set incsearch

are more than enough for my needs.

EasyMotion is really only good for getting to something you are staring right at on the screen.... I want to go THERE.. not meant as a general search or anything like that... and doesn't help get off the screen, for sure.

Basically what dedward said before me.

EasyMotion is not for searching, it's for moving the cursor deliberately to a place onscreen where you need to make an edit.

Yes, I know.

Searching is how I move to where I want.

It's a lot less confusing to me than EasyMotion's logic, it's consistent whether the word I want to jump to is visible or not and it's a native feature.

Well, I was curious when it was released so I've used it for a couple of weeks and found it horrible and confusing. Obviously many people like it so let's say it's a matter of taste.

I have been playing with the relative numbering all afternoon. I love having all my lines indexed, but it's hard to hit any number above 5 or 6 with my right hand because it's about to hit j or k. I wish it could be indexed with a combination of numbers between 1 and 5 only, but I guess that would mess with the j and k functionality.

Any ideas?

Also note: I am not a touch typist.

I don't use anything beyond 5(with the exception of 0 because that's fairly easy for me to hit). 6 and 7 are the hardest to hit. 17k for me just becomes 15kkk, for example. If you ever find yourself needing to go 50+ up or down, you should be using one of H, L, or M to take out a good chunk of that.

Yeah that's what I was thinking. Thanks!

zz is nice. but i hardly need it with

set scrolloff=5

with which i always have 5 lines context below and above the cursor.

I had tried scrolloff=1000 for middle scrolling. I liked it for a while, but got a little tired of the jumpy-ness. I'll give 5 a go.

Same experience here. I have set it to 4 since one year: it's perfect.

I use my up/down keys to move between visible lines (rather than real lines, which is what they and j/k do by default). This can be helpful occasionally when you have lines wrapped for something that's text rather than code.

    " up/down move between visual lines
    " instead of actual lines when wrapped
    imap <silent> <Down> <C-o>gj
    imap <silent> <Up> <C-o>gk
    nmap <silent> <Down> gj
    nmap <silent> <Up> gk
(You could also map j/k to just do this if it's something you use often, but I prefer not to.)

I don't remember where I found it but the best remapping I ever did was to map the sequence 'kj' to esc.

kj is rarely used in a word, and when it is it isn't a big deal.

inoremap kj <Esc>

That's useful indeed - I have both kj and jk mapped to Esc in my vimrc.


Interesting, I always did jj, but kj or jk feels speedier. Thanks for the tip!

I use arrow keys for movement all the time. Not always, but I found no value in forcing myself otherwise.

Interesting. How long have you been using Vim?

you did not ask me, but: when i'm busy thinking about the code, when i'm not typing for a long time, i lay back and browse the sourcecode with arrows and (up and down or pg-up and pg-down). not that fast, sure. but can be done with one hand, the other is scratching my head or what helps me think.

Page up and page down are part of my must-learn-asap shortcuts when learning a new editor - once you have hjkl and ctrl+f/ctrl+b ingrained, browsing code while keeping your hands mostly over the home row becomes a lot easier and you don't have to move your hands back and forth. I've found that if I want to browse my code with one hand, the scrollwheel on my mouse does the trick :)

ah yes. mouses. i knew there was another input controller...

(not to be misunderstood, i use mostly hjkl and ctrl-d and ctrl-u (half-screen up and down). and i find the mouse uncomfortable.)

Around 2 years I think.

Often switching mode to move around is just more effort than moving my hand a little, at least on the laptops I've used lately.

Insert is not a mode.

Well, technically it is. But don't think of it as one.

You do use hjkl and the others, right? As long as you do, we're good here. But if not, you need to block the arrow keys and use vim the 'right way'. I assume what you must mean is that you are using these in insert mode. Simply don't. Drop out of insert mode for navigation concerns and let vim handle the heavy movement work.

Vim allows us to use arrows so using the arrows is "right".

My issue has always been one of muscle memory.

The hjkl keys are fine to use when in Vim, but the majority of other products (browsers, word processors, spreadsheets, etc) don't support them for movement. So I have a constant context-switch in my muscle memory when moving between these different applications.

On a Mac keyboard, I find arrow-key navigation with my pinky finger very efficient… only requiring the rest of my fingers to move from the home row when pressing right.

I'm not sure this would work for everyone… but for me, one set of muscle memory is superior to convincing my nervous system to context-switch.

Not to be too cranky, but the rationale for avoiding the arrow keys is to keep your hands on the home row. So anything useful you find to do with them, if you do it often, will be inefficient.

Good point and something I should've written in the post. Text moving is not something needed very often, but I find using arrows in this case more pleasant than <gv or [egv. In normal mode, I'll usually use <<, because I'm already used to it.

It's a nice idea, but I don't buy it. What I like most about vim is how few unnecesary moves I make with my hands - in particular my wrists stay in the same place all the time; reaching for arrows would require moving the whole forearm, which I don't like and which breaks my flow.

Also I'm curious about this quote from OP:

  I just might remap hjkl to split navigation, 
  because I use hjkl less and less, favoring 
  navigating in other ways. Sounds crazy, I know ;)
Can you elaborate on your habits that cause it?

I got used to better ways to navigate in Vim - RegEx pointed out quite a few of them - http://news.ycombinator.com/item?id=4414502. I would especially emphasize f,F,t,T and w,W,b,B,e,E.

i have not been asked, but there are so many nice movements, that i hardly use hl. these might as well be remapped. i value k and l very much though.

I'm a rather advanced Vim user (10 years, vimrc with custom vimscript, use many shortcuts for movement), but I find it unproductive to NOT use arrows in insert mode. Example: inserting 'x' two characters before current position.

With arrows: <LEFT><LEFT>x<RIGHT>

Without arrows: <ESC>hix<ESC>lli

It also much depends on a keyboard design, the laptop-like often have arrows under ENTER and using them doesn't require hand movement.

In that situation I'd likely as not just backspace to the offending location and continue typing. Although I have been known to map caps+hjkl to the arrow keys.

(I'm assuming your situation is a typo while you're inserting a short string, rather than just defaulting to insert mode)

Cool idea. I read an article here a couple months ago about vim autocomplete and I mapped my tab key to autocomplete, but that was super annoying for, you know, adding indentation.

Now I've got my arrow keys mapped to Nop in all modes except I mapped my right key to autocomplete in insert mode, which has worked pretty well for the last 10 minutes. I'd really like to start using autocomplete more, so for me it's handier than text moving.

Check this[0] out. Autocomplete when typing words, indentation otherwise.

[0]: https://gist.github.com/3418565

thanks, that's a way better solution.

A very small corner case here, but on certain keyboard layouts, hjkl is actually harder to use than the arrow keys. To be more specific, I use an alphagrip keyboard most of the time where left,right,up,down are all under the left thumb and a trackball is under the right thumb. The way I interact with vim is thus very different from most folks, I imagine.

I prefer to map left and right arrow to navigate :bprev and :bnext. Paired with buftabs, I find it rather handy http://www.vim.org/scripts/script.php?script_id=1664

From my funny looking vimrc: navigating between splits with Alt+HJKL

    nnoremap ¬ <C-w>l
    nnoremap ˙ <C-w>h
    nnoremap ˚ <C-w>k
    nnoremap ∆ <C-w>j
(Mac :P)

I map left/right to next/previous tab, and up/down to next/previous buffer, since if I switch files my hand is often coming from the mouse and the arrows are along the way.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact