Very often, my preferred way of navigating a file in vim is by "scrolling" line by line. By scrolling, I mean shifting the lines up/down while keeping the cursor on the same line and position. <c-d>
and <c-u>
implement this already, but they do so for half a page, not one line.
So I made bindings that implement what I need:
noremap K k<c-y>
(technically I have noremap K @='k<C-Y>'<CR>
to accept a count)
noremap J j<c-e>
(technically I have noremap J @="j\<lt>C-E>"<CR>
to accept a count)
So hitting j
and k
move the cursor up/down one line, and J
and K
scroll up/down one line.
But I've learned that vim bindings are made very intelligently. For example, I was always annoyed that searching using /<term>
and ?<term>
highlight the term and automatically make the cursor jump to the next/previous occurrence of it. I initially wanted them to just highlight the terms and then optionally choose to jump to them by hitting n
, so as not to reset my view if the term doesn't exist in the current view. But I soon found myself using these search methods not necessarily to search for these terms, but to actually jump to them, as a way of fast navigation. I.e. it is sometimes much faster to search and jump to a pattern to navigate to a line position than by manually moving the cursor (using default movements) to the desired position at a line and position, even if that position is currently in view. And if the term doesn't exist in the current view, I can just call <c-o>
to reset the view back to where I was.
So is there a reason why line by line scroll isn't implemented by default? I suspect that by making a macro for scrolling, I might be using vim not the way it is intended to be used/not in the most efficient way.
Edit: in terms of the scrolling functionality itself, I think the mappings above implements it fairly well and are convenient to use for my purposes.
Edit1: I just found out that in Tmux copy-mode
with vi bindings enabled, i.e. set-window-option -g mode-keys vi
, they do implement scrolling the way described above, also using K
and J
.