Skip to content

Instantly share code, notes, and snippets.

@hrsh7th
Last active August 27, 2021 11:50
Show Gist options
  • Save hrsh7th/1f6399b9ec0a7bb8576a443fcd07ce5d to your computer and use it in GitHub Desktop.
Save hrsh7th/1f6399b9ec0a7bb8576a443fcd07ce5d to your computer and use it in GitHub Desktop.
line oriented macro
command! MacroStart call s:command_macro_start()
function! s:command_macro_start() abort
normal! 0qq
endfunction
command! MacroFinish call s:command_macro_finish()
function! s:command_macro_finish() abort
normal! q
endfunction
command! -range MacroRun call s:command_macro_run(<range>)
function! s:command_macro_run(range) abort
if getreg('q') == ''
return
endif
let l:pos = getpos('.')
let l:start = line('.')
let l:end = l:start
if a:range == 2
let l:start = getpos("'<")[1]
let l:end = getpos("'>")[1]
endif
let l:changenr = changenr()
for l:i in range(l:start, l:end)
execute printf('normal! %sG0@q', l:i)
endfor
let l:lines = getbufline('%', l:start, l:end)
execute printf('%sundo', l:changenr)
call setbufline('%', l:start, l:lines)
call setpos('.', l:pos)
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment