top | item 9574348

Vim Tips For Intermediate Users

51 points| realyze | 11 years ago |ideasintosoftware.com | reply

48 comments

order
[+] chrismorgan|11 years ago|reply
I’ve found relative line numbering to be distracting, and I find myself wanting absolute line numbering far more often. The reason that people want relative line numbering is almost always for relative motions, e.g. `6j`. But I find that this is seldom actually all that useful; there are various alternatives; here are a few:

- `}` to go forward by paragraph.

- `zj` to go down to the next fold.

- `gg` and `G` both accept a count (with which they are identical), so that `50gg` means “go to line 50”, and so absolute line numbers suffice though the command will typically be a little longer; of course, unlike `6j`, this will go to the start of the line if 'startofline' is not unset, so it’s not always ideal, but I find myself rather good at knowing at sight how many lines down and up I need to go anyway.

[+] isbadawi|11 years ago|reply
You can use a hybrid of absolute and relative numbering by setting both the 'number' and 'relativenumber' options (in vim 7.4). It displays the absolute number of the current line (instead of 0) but relative numbers of the other lines.
[+] userbinator|11 years ago|reply
I use :<line-number><enter> to go to a specific line; it has the advantage of showing the digits of the line number as it's entered.
[+] realyze|11 years ago|reply
Fair enough. I kinda switch between those two. Like, when I need to yank 5 lines from the middle of a function, I find it quite comfortable to see the relative numbers.
[+] vixsomnis|11 years ago|reply
Wow, I had no idea about the relative numbering.

One more thing that eases my daily frustration is

    set scrolloff=6
The above prevents getting the cursor stuck at the bottom or top of the screen, unless you're at the absolute bottom or top. 6 lines is perfect for me at 1920x1080, 13.3", 16pt Droid Sans Mono Regular.

It might help you if you find yourself scrolling down to move the view around, then scrolling up again to get where you want to edit.

[+] oxplot|11 years ago|reply
Just a note that this affects H (jump to top line on the screen) and L (jump to bottom line on the screen).
[+] vacri|11 years ago|reply
Thanks here as well. I never even thought such a feature might exist. Love it!
[+] quickpost|11 years ago|reply
Good one! Just added to my .vimrc. Thanks!
[+] csandreasen|11 years ago|reply
If you're looking for keys to remap, these are prime candidates:

  s: synonym for cl
  <cr> (enter): synonym for j
  _: acts like ^ when given without a count, acts like <count>jk^ when given with a count
  <bs> (backspace): synonym for h
  <space>: synonym for l
Doubling up operators with non-motion characters works well, too. For example, operators like 'c' or 'd' probably shouldn't be rebound, and they're often doubled ('cc', 'dd') or accept a motion ('c$', 'd}'), but combining them with another non-motion isn't valid ('dc','cs'). This makes those combinations good candidates for custom bindings. A lot of plugins like surround.vim take advantage of those combinations.

Vimcasts covered this in more depth here: http://vimcasts.org/blog/2014/02/follow-my-leader/

[+] batguano|11 years ago|reply
I remap the semicolon to the colon in my .vimrc:

  nmap ; :
This makes it much easier to type commands, since you don't need to use a keyboard chord.

After using this for a year or so, I found out that the semicolon actually has its own function--it can refer to the current insertion point when writing ranges. But I've never used that; one could remap : to ; if desired.

[+] xtagon|11 years ago|reply
In normal mode, after using f or F to jump to a character, ; repeats the motion and jumps to the next match. I find myself using this often enough that if I swapped mappings for : and ; I would be in a world of confusion.
[+] hyperpallium|11 years ago|reply
; also repeats f and t

but : is much more common so it makes sense to swap them

[+] PaulJulius|11 years ago|reply
Being able to open up multiple files and arrange them precisely how I want (with the keyboard!) is my absolute favorite part of vim. I use the following shortcuts for opening a file to the right/left or above/below:

    nnoremap <Leader>or :set splitright<CR>:vs
    nnoremap <Leader>ol :set nosplitright<CR>:vs
    nnoremap <Leader>oa :set nosplitbelow<CR>:sp
    nnoremap <Leader>ob :set splitbelow<CR>:sp
    " Don't let vim change the setup when closing windows
    set noequalalways
So I can simply type ',or ' and then the name of a file to get a vertical split.

You can then use <C-w>w<number>+/- to increase/decrease the height of a window and <C-w>w<number>'<'/'>' to change the width.

[+] frankerz|11 years ago|reply
Since I usually work with small laptop screens I tend to use vim tabs more than split. I find these key combinations quite comfortable:

nnoremap L :tabnext<CR>

nnoremap H :tabprevious<CR>

nnoremap <C-N> :tabnew<CR>

Also if you use offline docs (I only know Zeal) it would be pretty helpful to map the offline doc application to the K key depending on the language

nnoremap K :exec "!/usr/bin/zeal --query ".&ft.":".expand('<cword>')."&"

The above copies the word under the cursor and query it with zeal.

[+] timonv|11 years ago|reply
Some more tips that can make your vim life more awesome:

- Learn about registers

- '.' is more powerful than you think

- Having a hotkey to jump to your rc and auto resource the file on safe is awesome

- Learn about quickfix (if you work with a compiled language, still renders a bit crap for Rust, but it works)

- vim-dispatch (or neovim with jobs ext) is probably one of the nicest, time saving plugins around

- If your vim is slow (repaints due to ill setup Syntastic conflicts is common), fix it. Vim has excellent profiling support.

[+] mirkules|11 years ago|reply
You can simply mark your text with Shift-v, then d to cut, y to paste (correction edit: p is for paste). This saved me so much time and energy than trying to count the lines. The relative numbering is cool unless your text goes off screen...
[+] hackuser|11 years ago|reply
Wouldn't that be the following, in normal mode?: v (motion commands) d p
[+] 1_player|11 years ago|reply
The most useful shortcut I have in my .vimrc is the ability to "sudo save":

    command W :execute ':silent w !sudo tee % > /dev/null' | :edit!
No more exiting vim to start it again with sudo.
[+] realyze|11 years ago|reply
Huh. Well I kinda consider that `sudo` annoyance a feature. It tells me to think twice before potentially messing up badly. :-)
[+] senorito|11 years ago|reply
Isn't it much simpler to use tmux for the vertical split? Or are there any advantages of "vertical buffers" in VIM I should know about?
[+] harry8|11 years ago|reply
- yanking/cutting and pasting between the two displayed buffers in the panes.

- being able to quickly swap one pane to show a different file, then swap back to that same file again (or do it with the right pane)

- being able to horizontally split either vertical pane, ie you can tile the panes exactly how you like very fast.

:mouse=a

and now you can drag the split around

- showing differences between to versions you have open

:windo diffthis

highlights them - can't really do it at all with tmux

- Having the whole thing inside a screen session you can resume it from where you left off over ssh remotely later.

- Saving it as a vim session if you want to power the box down and resume from where you were, with precisely those splits and buffers open scrolled to what you see now.

- Backgrounding the whole thing to do something else quickly.

Yeah, there's a few, I doubt that list is exhaustive and doing things that way means some other trade offs. Whether these advantages are actually things you want is a different question that only you + experimentation can answer..?

[+] afarrell|11 years ago|reply
This is a longshot, but has anyone ever seen a way to drop into some sort of debugger for vim that is on a higher abstraction layer than gdb?
[+] yen223|11 years ago|reply
Is there any way to set relative numbering in Vim to be 1-indexed? I want to avoid mentally adding 1 every time I want to yank a few lines.
[+] greggyb|11 years ago|reply
Rather than <number>yy, you could do y<number>j or y<number>k and it works appropriately with the relative numbers; yj == 2yy. I always use this with c, d, and y commands.
[+] niix|11 years ago|reply
The vertical split recommendation is really great. I agree with the author that I always forget and often times end up doing horizontal.
[+] akkartik|11 years ago|reply
Several years ago[1] I requested an option called 'splitvertical' that would cause all window splits to be vertical by default. Christian Brabandt made me a patch I use to this day even though it was never merged: https://github.com/chrisbra/vim-mq-patches/blob/master/verst....

It's still fairly straightforward to merge. Ping me if you need help (email in profile)

[1] http://thread.gmane.org/gmane.editors.vim.devel/28395/focus=... http://www.reddit.com/r/vim/comments/2irn8j/vertical_split_b...

[+] chrismorgan|11 years ago|reply

    nnoremap <leader>w <C-w>v<C-w>l
If you set 'splitright', you should remove the <C-w>l.
[+] adamtj|11 years ago|reply
This mapping makes my life much nicer:

  nnoremap Q @q
It turns 'Q' into a sort of enhanced '.' command that can repeat a sequence of multiple commands. To use it, first start recording a macro to the 'q' register. That's the easiest register because 'q' is also the command to start and stop recording a macro: to start recording you just simply press 'qq'. Then you make your changes and press 'q' to stop. Now you can repeat that sequence of commands with 'Q'.

It turns out that macros are really useful, but I would rarely use them because it's just so awkward to press '@' each time I want to apply one. Unfortunately, '.' doesn't repeat the '@' command to apply a macro. I would often find myself mechanically repeating things by hand, or else wasting time writing complicated sequences of :s/substitution/commands/. Now I use quick ad-hoc macros all the time.

The speed and convenience are the real key for me. You might call it synergy, or a tipping point, or the straw that breaks the camel's back. For me, 'qq' is just a little bit faster and easier than any other register. Repeating 'Q' is just a little bit nicer than typing '@q' followed by repeating '@@'. (It's also kinda nice that it's all the same key.) Macros are nice, but two carefully made choices that each are only slightly better than the alternatives combine to make something vastly more useful. In my mind, 'Q' is almost its own thing, separate from the idea of other macros.

Now that I use macros, I've discovered some useful patterns. One technique I often use is to include a final motion in my macro to move to the next place I want to change. For example, maybe I want to edit several lines in a row. I press 'qq', make my changes to the current line, then press 'j' to move down a line. Only then do I stop recording. After that, I can just hold down the shift key and press 'Q' repeatedly until I'm done. Even if my changes are simple enough that '.' would work, it saves me from making mistakes when bouncing back and forth: 'j.j.j.j.jj.'. Oops.

You can do that with any motion(s), and I often use 'n'. Sometimes finding some simple marker text on each problem line is easy, but using find-replace to make the actual changes requires a hideous regex. In those cases, I'll do the simple search, record a macro as I make the first change, and finally press 'n' to move to the next match. Then I just lean on 'Q' until I'm done.

Sometimes I'll also throw a 'zz' after the last motion to center the current line in the window so I don't have to look around to locate the cursor when a change scrolls the window.

Normally 'Q' enters Ex mode, but so far I haven't found much use for that. If that ever changes, I'll probably use a <leader> mapping to get Ex mode back. Using something even a little bit slower than 'Q' would destroy the effect. I forget where I originally learned this mapping.