问题
Is it possible to configure vim such that a movement command "wraps around" the vertical (or horizontal) buffer?
In other words, assume I'm on line 2 in a buffer, in visual mode. I press 3k. My cursor is now positioned on the last line of the buffer.
Or more simply, my cursor is on the first line of the file -- I press k. Now my cursor flips to the last line of the file.
Apologies if this has been asked before, but I couldn't find any references searching for "circular scrolling" or "wrap-around scrolling".
回答1:
It's probably possible with some vimscript hackery, but it's much more universal to become efficient with motions like G to go to the bottom of a file and gg or 1G or <C-Home> to go to the top of the file. Likewise, $ for the end of a line and 0 for the beginning of the line or ^ for the first non-blank character.
You can also set :help whichwrap, which specifies which keys will move of to the next line when moving past the end of the line or move to the previous line when moving past the beginning of the line. Other then that I don't think there's built in functionality for what you're asking. You can could do it with some vimscript but it would require remapping h,j,k, and l to functions and handling whether they are at the end/beginning of the line/file. To me that seems overkill and rather messy.
All that being said, if you must...
nnoremap j :call CheckJ()<cr>
nnoremap k :call CheckK()<cr>
nnoremap h :call CheckH()<cr>
nnoremap l :call CheckL()<cr>
fun! CheckJ()
if line('.') == line('$')
norm! gg
else
norm! j
endif
endfun
fun! CheckK()
if line('.') == 1
norm! G
else
norm! k
endif
endfun
fun! CheckH()
if col('.') == 1
norm! $
else
norm! h
endif
endfun
fun! CheckL()
if col('.') == (col('$') - 1)
norm! 0
else
norm! l
endif
endfun
回答2:
Vim is a text editor, and text has both physical and logical properties of having a beginning and an end, both in columns and lines. Therefore, the feature you're requesting doesn't exist, and probably won't ever be included in Vim.
It can, however, with some effort be emulated in Vimscript, by binding most movement commands to custom implementations. But that would introduce inconsistencies in the usage model, as ranges (e.g. :42,10) still wouldn't wrap around.
Why would you want such a wrap-around? Is this for a particular file type, or are you used to it from another editor?
来源:https://stackoverflow.com/questions/12380689/is-circular-scrolling-possible-in-vim