Skip to content

Switch next in current line

mg979 edited this page Sep 20, 2021 · 1 revision

If you want a mapping that emulates the built-in <c-a>/<c-x>, so that it will switch whatever it can in the current line, jumping to it, you can try adding this mapping and function:

nnoremap <silent> <Plug>(SwitchInLine) :<C-u>call SwitchLine(v:count1)<cr>
nmap <C-s> <Plug>(SwitchInLine)

fun! SwitchLine(cnt)
    let tick = b:changedtick
    let start = getcurpos()
    for n in range(a:cnt)
        Switch
    endfor
    if b:changedtick != tick
        return
    endif
    while v:true
        let pos = getcurpos()
        normal! w
        if pos[1] != getcurpos()[1] || pos == getcurpos()
            break
        endif
        for n in range(a:cnt)
            Switch
        endfor
        if b:changedtick != tick
            return
        endif
    endwhile
    call setpos('.', start)
endfun

Clone this wiki locally