Skip to content

Instantly share code, notes, and snippets.

@bfrg
Created July 15, 2020 10:06
Show Gist options
  • Save bfrg/f035bc2cb346d155bb1c65c95d613b74 to your computer and use it in GitHub Desktop.
Save bfrg/f035bc2cb346d155bb1c65c95d613b74 to your computer and use it in GitHub Desktop.
Scroll the current window horizontally with h, l, H, L instead of zh, zl, zH, zL with the help of a popup window
" Simple script that adds a new horizontal scrolling mode using a popup
" window.
"
" Pressing z/ opens a small popup window in the botton right corner of the
" screen. While the popup window is open, the current window can be scrolled
" with h (left one char), l (right one char), H (one half screen left), L (one
" half screen right). Close the window with q or ENTER.
scriptencoding utf-8
function s:error(msg)
echohl ErrorMsg | echomsg a:msg | echohl None
endfunction
function s:popup_filter(winid, key) abort
if a:key ==# 'l'
execute 'normal! zl'
elseif a:key ==# 'h'
execute 'normal! zh'
elseif a:key ==# 'L'
execute 'normal! zL'
elseif a:key ==# 'H'
execute 'normal! zH'
elseif a:key ==# 's'
execute 'normal! zs'
elseif a:key ==# 'e'
execute 'normal! ze'
elseif a:key ==# 'q' || a:key ==# "\<cr>"
call popup_close(a:winid)
else
return v:false
endif
return v:true
endfunction
function s:scroll_horizontal() abort
if &wrap
return s:error("Horizonal scrolling only work when 'wrap' is off")
endif
let text =<< trim END
h → left one character
l → right one character
H → left half a screen width
L → right half a screen width
s → cursor to left screen side
e → cursor to right screen side
q → close
END
let winid = popup_create(text, {
\ 'pos': 'botright',
\ 'line': &lines - &cmdheight - 1,
\ 'col': &columns,
\ 'padding': [0,1,0,1],
\ 'border': [],
\ 'borderchars': ['─', '│', '─', '│', '╭', '╮', '╯', '╰'],
\ 'mapping': v:false,
\ 'filtermode': 'n',
\ 'filter': funcref('s:popup_filter')
\ })
call matchadd('Title', '^\w', 1, -1, {'window': winid})
call matchadd('Special', '→', 1, -1, {'window': winid})
endfunction
nnoremap <silent> z/ :<c-u>call <sid>scroll_horizonal()<cr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment