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.
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.
Care to give any links to this? This is probably my biggest annoyance with Vim right now, how my current line is 0 and having to add 1 every time I do any operation gets old quick.
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.
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.
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.
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.
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.
I map <space> to : and it's been great. <space> is normally just a synonym for l, so I never used it; my only muscle memory issue is just an occasional : to enter command mode. Perhaps I should unmap :.
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.
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...
You are right, v will highlight per character, Shift-V highlights entire lines. Also, as you pointed out, I made a mistake on the paste, it's p, not y (which is copy).
- 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..?
I see lots of discussion about using tmux and vim together. Most of the articles are more about configuring each instead of how that persons workflow. I get stuck on this problem; where to draw the line between tmux splits and vim splits (and by extension window management).
When do you do use tmux for splitting versus vim? What are your key bindings for creating, resizing, and navigating each?
Personally, I drop back to the shell constantly so I tend to launch a lot of separate vims. The only time I use vim splits are if it explicitly occurs to me or if I'm using vimdiff. This means I have a lot of problems copy/pasting or end up opening the same file in multiple vim sessions (but different tmux panes/windows).
Oh yeah, it's not a problem or conflict. Many of the examples people give tend to remap similar or the same keys for splitting in vim and tmux or navigating between splits (alt or ctrl + hjkl for navigating them).
I'm looking for more of a mental model or workflow of when to use vimsplits over tmux splits and how to navigate. I generally avoid vimsplits because I haven't found a good workflow to use both and my muscle memory gets messed up when I mix them.
I recall there being a few gotchas last time I used debug mode and they're mentioned in the doc, but unfortunately I don't quite remember what they were.
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.
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)
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.
- `}` 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.