Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #137 - Automatically wrap left and right

Created: October 14, 2001 21:15 Complexity: basic Author: Brian Medley Version: 5.7 Karma: 9/5 Imported from: Tip#137

I hate it when I hit left (or h) and my screen flickers. I want it to go up to the next line. Ditto fir right (or l). Below are two functions / mappings to help with that. I'm pretty sure that if you remove the <silent>, then it will work in 5.x...


nnoremap <silent> <Left> :call WrapLeft()<cr>

nnoremap <silent> <Right> :call WrapRight()<cr>


nnoremap <silent> h :call WrapLeft()<cr>

nnoremap <silent> l :call WrapRight()<cr>


function! WrapLeft()

let col = col(".") 


if 1 == col 
" don't wrap if we're on the first line 
if 1 == line(".") 
return 
endif 
normal! k$ 
else 
normal! h 
endif 

endfunction


function! WrapRight()

let col = col(".") 
if 1 != col("$") 
let col = col + 1 
endif 


if col("$") == col 
" don't wrap if we're on the last line 
if line("$") == line(".") 
return 
endif 
normal! j1

Comments

There is an existing feature for this in Vim: "set whichwrap=<,>,h,l,[,]"

this causes the cursorkeys, as well as h and l, to wrap when used at beginning and end of lines.

(the <> and [] differ in that <> are the cursorkeys used in normal and visual mode, and the [] are the cursorkeys in insert mode)

See :help whichwrap

So imho no complex scripting needed.


grtz,

Geert van der Ploeg

vimonline--AT--geert.triple-it.nl , October 15, 2001 0:45


I love you for this tip!


vschum1 at towson dot edu , November 30, 2003 9:20


Advertisement