How to make horizontal scrolling easier when the cursor is not ideally located?

2017-09-18 07:41:59

For easy horizontal scrolling in long lines, I use these mappings that work well for me:

map 20zh

map 20zl

Something that has always annoyed me is when the following use case happens (| is the current cursor position):

some |short line

some other line

some other line

some other line

some other line

some other line

some veeeeeeeeeeeeeeeeeeeeeeeeery looooooooooooooooooooooooong liiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiine

Sometimes I want to scroll horizontally and have a quick peek at the very long line just to see if there is anything interesting in there. In GUI text editors, I would just horizontally scroll with my mouse, and scroll back. In Vim, if I try to scroll with zl, here is what happens:

..me short line|

..me other line

..me other line

..me other line

..me other line

..me other line

..me veeeeeeeeeeeeeeeeeeeeeeeeery looooooooooooooooooooooooong liiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiine

I can slightly scroll for a bit (as vis

  • After a lot of spelunking in the VIM help section and newsgroup archives, I came up with this not-so-straightforward solution. It even works in split-windows!

    map 20zh:call SetVirtualEdit()

    map :set virtualedit=all20zl

    noremap ^ :set virtualedit=^

    noremap $ :set virtualedit=$

    python import vim

    function! SetVirtualEdit()

    let absolute_col = virtcol('.') + pyeval('vim.current.window.col')

    let absolute_col += &foldcolumn + (&number ? &numberwidth : 0)

    let is_on_leftmost_screen = screencol() == absolute_col

    if is_on_leftmost_screen

    set virtualedit=

    else

    set virtualedit=all

    endif

    endfunction

    Notes:

    The mapping for is important for screencol() to work well.

    You need Vim compiled with python support

    2017-09-18 10:02:18