News for hackers. Stuff that matters.
Re:Vim Anti-Patterns by pjungwir on Wednesday October 05, 2016 @12:48PM (#12645523)
I never got into the habit of using { and }. I just use H M L (high/medium/low) to get approximately in the right part of the screen, then go line-by-line. You can also do 5H or 10L to get "5 lines from the top" or "10 lines from the bottom". I make pretty good use of vim features, but I like to mix some sloppiness with the precision. I don't often count things before typing commands, because that breaks the second-nature quality of navigation. If something is more than 2 or 3 objects away, I approximate. I do use counting with t, T, f, and F a lot to hop around, including things like c2f). Very frequently that combines well with spamming j.j.j.j.j. I use . with trivial movement (like j or n) at least 10x more than @a. Another way to move fast sloppily is W and B. I guess I'm saying: learn all the cool stuff you can do, but don't feel like you have to find the most precise way to do every little thing. If you're just starting out, relax and don't try too hard. You can always just pick one new thing every couple weeks and try to add it to your habits.

Oh also: he mentions O to insert above the current line. I use that a lot, but on my systems (going back 15 years or so I think) it has always required a pause, like vim is waiting to see if I'm typing O or some longer command. If I type O and immediately start entering text, strange things happen. This doesn't happen with o. Does anyone else experience this? Maybe it's just something weird in my own setup.

EDIT: Some more "moving fast sloppily": 1G goes to the top of the file. G goes to the bottom. Also you can not-move, but scroll the visible area so that your cursor is on the top line (zENTER), middle line (z.), or bottom line (z-). I use that a lot when I am Ctrl-Fing through a file, so I can see more context.

Re:Vim Anti-Patterns by dnr on Wednesday October 05, 2016 @04:37PM (#12647609)
My favorite "secret" vim feature, that no one has mentioned yet:

    set scrolloff=999
That will keep the cursor in the middle of the window always, except at the start and end of the file. Before discovering that I used to `zz` after nearly every movement; that's all gone now, saving a ton of time and frustration.
Re:Vim Anti-Patterns by philsnow on Wednesday October 05, 2016 @09:22PM (#12649401)
I would go nuts; I prefer to use the whole screen to see what I'm looking at, and if I've just searched a buffer for a function name or whatever, there's a good chance I want to see the whole function and not the (screen height / 2) lines above it. (In emacs this is done with `C-l C-l`, in vim it's `zt`)

While scanning a buffer for some pattern I'll quite often make a one-off keyboard macro that searches for a pattern and then invokes `C-l C-l`, it's very nice to then bounce on the "repeat keyboard macro" key and leave my eyes in the same place while the screen contents change under them.

Re:Vim Anti-Patterns by Gruselbauer on Thursday October 06, 2016 @02:40AM (#12650650)
Oh guys, thank you so much for new food for my vim-addiction. I'd hesitate to call myself anything but an interested beginner, so it's always very welcome to learn new stuff. I distinctly remember running vimtutor the first time as a teenager fresh of whatever Windows came before and thinking 'goddamn, whyyyyyy is this hot mess so popular among my mentors and idols?'...

Quite some years later, I don't think I could ever do without vim. Not saying that emacs wouldn't probably fill the same role, editor wars considered and all. I just had somebody tell me to pick an editor and get good with it, some blog or forum posting made me go with vim and it's been one of my favorite pieces of software since.

To contribute a little bit, what I've really come to learn recently is the 'hybrid line numbering mode' that was added with 7.4 iirc. When you add both. set relativenumber

set number

to your .vimrc, you get relative numbers for all lines except the current, which is displayed as absolute. Helped my navigation game a huge deal. Just in the unlikely case anyone here hadn't heard of it.

Re:Vim Anti-Patterns by lkbm on Friday October 14, 2016 @09:26AM (#12707884)
I tried this very briefly and thought "This seems useless." 1. It's annoying how they all change every time my cursor moves (though it'd be fine if I also did "scrolloff=999"), and g-4 doesn't work to jump up 4 lines.

Then I realized I could do y6k to pull the next six lines, rather than "told on, let me count/do the math on the line numbers". Took me another moment to realize "4j" is the way to move.

I think I'm going to like this.

Re:Vim Anti-Patterns by savanaly on Wednesday October 05, 2016 @01:38PM (#12645960)
I have two tricks for better movement, add the following to your .vimrc:

    " for scrolling up and down quickly
    nnoremap J 7j
    nnoremap K 7k
    vnoremap J 7j
    vnoremap K 7k
and the second (which requires a third party plugin):

    " easymotion allows us to jump to all places that could have been reached by
    " (w in this case), and the bd- indicates we want to search backwards too.
    nmap f <Plug>(easymotion-bd-w)
    vmap f <Plug>(easymotion-bd-w)
The first one originates from recognizing the problem from the antipatterns article: that spamming j j j j to get down to the line you want is dumb. But I choose not to follow the advice of most vim articles I read which is to learn built-in tricks such as typing 10j or :<line number>. The milliseconds it takes for me to count the number of lines I want to go down or assess the line number of the line I'm interested in aren't worth it, I'll just tap j if the line is close enough or J if it is five or more lines down. I think it is a good solution.

The even better solution (and I use them both in tandem) is to install easymotion [1] and use it like I show in the second example.

I also use gg and G to get to leap to the top and bottom of the file quite a lot.

---

As to your question about using O to insert above the current line, that should not work that way. Your specific issue sounds very much like a binding has been defined for O<other character>. Type :nmap in vim to display a list of bindings and check for lines starting with O. It might be in your .vimrc or some plugin defined something. I would definitely say remove the binding or the plugin rather than put up with that, O is very useful.

[1] https://github.com/easymotion/vim-easymotion

Re:Vim Anti-Patterns by mihaifm on Wednesday October 05, 2016 @02:44PM (#12646595)
For better scrolling:

    nmap <C-j> 3j3<C-e>
    nmap <C-k> 3k3<C-y>
(replace "3" with whatever number you like best). These shortcuts will enable you to scroll the text, while keeping the cursor fixed. It's similar to web page scrolling. It helpls you type in code while maintaining an overview on what's going on below and above it. Add these to your .vimrc and use them for some time. You'll thank me later.
Re:Vim Anti-Patterns by weaksauce on Wednesday October 05, 2016 @04:36PM (#12647608)
I could see using that with J and K because I use c-(hjkl) for moving between open buffers in a tab. Nice tip though.

   map <C-h> <C-w>h
   map <C-j> <C-w>j
   map <C-k> <C-w>k
   map <C-l> <C-w>l
Re:Vim Anti-Patterns by stephenboyd on Wednesday October 05, 2016 @05:56PM (#12648284)
I have some similar mappings in mine. Capital J and K go up or down 4 lines, capital H and L move by words instead of characters. It feels like holding down the button to run instead of walk in Super Mario Bros.
Re:Vim Anti-Patterns by x0x0 on Wednesday October 05, 2016 @02:42PM (#12646573)
Have you tried using :relativenumber? You still have to type eg 12j or whatever but at least vim does the counting for you. If so, can you share why you like easymotion more? Thanks!
Re:Vim Anti-Patterns by savanaly on Wednesday October 05, 2016 @05:29PM (#12648078)
Yes, I use :<line number> very occasionally, mainly if I know I need to edit a certain line number in a file and just opened that file. But for regular use the time to type in a line number is just too long since I can't touch type numbers very quickly (when do we ever get practice typing in long numbers after all?).

I use easymotion because it's the fastest way to go from my eye spotting something on the screen to my cursor being there. It's two button presses consistently, you can't really beat that with some combination like 5j$2b, especially since that combo is different every damn time.

Re:Vim Anti-Patterns by Tyr42 on Wednesday October 05, 2016 @06:20PM (#12648464)
Ahg, no I couldn't do that, I need to keep J as Join lines. That's pretty useful to me. Kinda the inverse of gq.
Re:Vim Anti-Patterns by savanaly on Wednesday October 05, 2016 @06:37PM (#12648567)
Ah, I bound join lines to :join since I don't use it that often. I still encourage you to try binding something to 7j (or whatever number of j's feels right). One of the best shortcuts of my life.
Re:Vim Anti-Patterns by yamaneko on Saturday October 08, 2016 @02:18PM (#12667956)
I tried to use J, but it's somewhat tied to my muscle memory already (and I'm surprised I use it quite often joining comments and stuff). If you're like me, remember that you can abbreviate :join to just :j.

The advantage of using J and K is that they're very vim movements, but if can't because of :join, another workaround would be to use <PageUp> and <PageDown> since they're redundantly bounded to <Ctrl-B> and <Ctrl-F>:

    nnoremap <PageDown> 7j
    nnoremap <PageUp> 7k
    vnoremap <PageDown> 7j
    vnoremap <PageUp> 7k
Re:Vim Anti-Patterns by christianmann on Wednesday October 05, 2016 @11:49PM (#12650114)
I use that a lot as well, along with K to look up the current word in the man page.
Re:Vim Anti-Patterns by guygurari on Wednesday October 05, 2016 @04:01PM (#12647281)
I use the same sloppy methods. What matters to me is speed and staying in the zone while editing. Trying to get the positions precisely right forces me to think consciously and takes me out of the zone.

About the 'O' problem, try ':set noesckeys' and see [1] for more.

[1] http://stackoverflow.com/questions/2158516/delay-before-o-op...

Re:Vim Anti-Patterns by mixedmath on Wednesday October 05, 2016 @01:08PM (#12645691)
I share some of your sentiments. I find that I so frequently want to go the first and last non-whitespace characters in a line that I remapped H to go to first non-whitespace character and L to go to the last. I think of these as "linewise-h" and "linewise-l", which almost fits with vim's other conventions. Then I substitute ctrl-d and ctrl-u to go up or down by half-screens.

I'll also note that although I do also frequently use () and {}, I very much like the plugin sneak, which adds an f-type motion. Usually f{char} goes to the first following instance of {char} on the line. Sneak adds in s{char}{char}, which goes to the first instance of {char}{char} (not restricted to the line), and is ;-repeatable just like f-type motions. What this means is that my default navigation is often, look where I want to go, find two letters, sneak to that combination, and then ;;; until I get to the right one.

On your also: My O doesn't stall at all. Have you tried performing the same action on an empty vimrc? You might start vim with `vim -u NONE`, enter some text, and try out O. Or maybe `:map O` or `:nmap O` will inform you that you have some operator-pending map beginning with O.

Re:Vim Anti-Patterns by Tyr42 on Wednesday October 05, 2016 @06:22PM (#12648479)
I think ^ goes the the first non-empty character on the line already. Think of it like the regex. Then 0 (zero) goes to column 0.
Re:Vim Anti-Patterns by raygull on Wednesday October 05, 2016 @02:37PM (#12646537)
I have similar habits, but use zz to recenter the cursor (instead of z.), and gg (instead of 1G) to move to the top. Very quick.

Also, with relativenumber set, it's easy to move to the exact line without counting, or to see how many lines you need to yank, etc.

But sometimes I'm just idly moving through a file with j and k, and it feels perfectly natural to do so, the same way I unconsciously move the mouse cursor over text as I read it (outside of vim).

Re:Vim Anti-Patterns by grault on Wednesday October 05, 2016 @01:41PM (#12645987)
I started combining {} with zt zz zb. I feel like it mimics physical handling of a scroll. This requires some empty lines though. In case I work with dense text, I usually switch to H/M/L also (combined with zt zz zb).

I have to mention that for navigation purposes, f/F is only useful with ;, otherwise you could find yourself looking through the line for a suitable character which slows you down. With ; it's enough to pick a "rare" character on the line which is near to/in the desired object.

Next to c2f, ci" (and ci) etc) could be really useful when you end up being in the middle of a string literal and just want to change the whole thing. Or in the middle of an expression in parens - http://fuckyeahvim.tumblr.com/image/122373002741

Re:Vim Anti-Patterns by owlish on Wednesday October 05, 2016 @06:23PM (#12648485)
I've found it helpful to use relative numbering in normal mode and switch to absolute numbering in insert mode:

  set relativenumber nonumber
  autocmd InsertEnter * :set norelativenumber number
  autocmd InsertLeave * :set relativenumber  nonumber
IIRC this tip was from the Practical Vim book
Re:Vim Anti-Patterns by OJFord on Wednesday October 05, 2016 @06:55PM (#12648667)
I can't remember or find what the "slow O" is about, but it's definitely real; I think I read about it in relation to it being fixed in v8.
Re:Vim Anti-Patterns by rogual on Wednesday October 05, 2016 @12:53PM (#12645563)
Sounds like you've got something mapped to a key sequence that starts with O.

You can look through :map and try and find it -- not sure if there's an easier way.

Re:Vim Anti-Patterns by pjungwir on Wednesday October 05, 2016 @01:03PM (#12645650)
So this prompted me to Google for it (Googling for one-letter vim commands works better than I thought!), and this looks like the issue:

http://stackoverflow.com/questions/2158516/delay-before-o-op...

Re:Vim Anti-Patterns by karmajunkie on Wednesday October 05, 2016 @05:12PM (#12647938)
on the O thing: check :nmap and friends to see if you have a 2-letter mapping set up that starts with O. This has caused issues for me in the past when I wanted to use a key that was doing double duty already—vim has to wait just a sec to see if you keep typing and it needs to act on that.
Re:Vim Anti-Patterns by gbrown_ on Wednesday October 05, 2016 @03:17PM (#12646868)
I remap { and } so I they land on the first and last lines of a block of text between blank lines, rather than the blank lines themselves. I find this is usually what I want then I just move/ take action in a more targeted fashion with f, t, w or the vim-sneak plugin.
Re:Vim Anti-Patterns by apazzolini on Wednesday October 05, 2016 @04:06PM (#12647316)
I find normal mode much more pleasant with space mapped to 10j and backspace mapped to 10k. This combined with / and ? make navigation pretty quick without having to mentally switch context to think about the vimgolf way of getting places.
Re:Vim Anti-Patterns by Manishearth on Wednesday October 05, 2016 @11:27PM (#12650029)
> If something is more than 2 or 3 objects away, I approximate

set number + set relativenumber is pretty awesome for line counting. This marks all lines with relative numbers, except the current one.