Skip to content

Instantly share code, notes, and snippets.

Created March 3, 2016 15:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save anonymous/1313c24b0908ec61af7d to your computer and use it in GitHub Desktop.
Save anonymous/1313c24b0908ec61af7d to your computer and use it in GitHub Desktop.
Vim rotating lines "screensaver"
" Press \r to start rotating lines and <C-c> (Control+c) to stop.
function! s:RotateString(string)
let split_string = split(a:string, '\zs')
return join(split_string[-1:] + split_string[:-2], '')
endfunction
function! s:RotateLine(line, leading_whitespace, trailing_whitespace)
return substitute(
\ a:line,
\ '^\(' . a:leading_whitespace . '\)\(.\{-}\)\(' . a:trailing_whitespace . '\)$',
\ '\=submatch(1) . <SID>RotateString(submatch(2)) . submatch(3)',
\ ''
\ )
endfunction
function! s:RotateLines()
let saved_view = winsaveview()
let first_visible_line = line('w0')
let last_visible_line = line('w$')
let lines = getline(first_visible_line, last_visible_line)
let leading_whitespace = map(
\ range(len(lines)),
\ 'matchstr(lines[v:val], ''^\s*'')'
\ )
let trailing_whitespace = map(
\ range(len(lines)),
\ 'matchstr(lines[v:val], ''\s*$'')'
\ )
try
while 1 " <C-c> to exit
let lines = map(
\ range(len(lines)),
\ '<SID>RotateLine(lines[v:val], leading_whitespace[v:val], trailing_whitespace[v:val])'
\ )
call setline(first_visible_line, lines)
redraw
sleep 50m
endwhile
finally
if &modified
silent undo
endif
call winrestview(saved_view)
endtry
endfunction
nnoremap <silent> <Plug>(RotateLines) :<C-u>call <SID>RotateLines()<CR>
nmap \r <Plug>(RotateLines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment