sponsor Vim development Vim logo Vim Book Ad

basic Tip #137: automatically wrap left and right

 tip karma   Rating 5/4, Viewed by 1658 

created:   October 14, 2001 21:15      complexity:   basic
author:   Brian Medley      as of Vim:   5.7

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|
    else
        normal! l
    endif
endfunction

 rate this tip  Life Changing Helpful Unfulfilling 

<<Remapping Alt, Ctrl and Caps in Win2k | Getting name of the function >>

Additional Notes

[email protected], October 15, 2001 0:45
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
vschum1 at towson dot edu, November 30, 2003 9:20
I love you for this tip!
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo