ExecuteThatThing.vim 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. " Author: Timothy Rice <trice@posteo.net>
  2. " Description: Pass text captured by motion to external filter (i.e. shell command)
  3. " This effectively turns Vim into a stateless coding notebook.
  4. if exists('g:loaded_execute_that_thing')
  5. finish
  6. endif
  7. let g:loaded_execute_that_thing = 1
  8. function! ExecuteThatThing(type, ...)
  9. let l:sel_save = &selection
  10. let &selection = 'inclusive'
  11. let l:reg_save = @@
  12. if a:0
  13. silent exe "normal! gvy`>"
  14. elseif a:type ==# 'line'
  15. silent exe "normal! '[V']y`>"
  16. else
  17. silent exe "normal! `[v`]y`>"
  18. endif
  19. " The following guard protects against ill-formed motions.
  20. " (Motivated by the dodgy InnerCommandMotion further down.)
  21. if @" != "\n"
  22. silent exec "r ! " . escape(@", "%!#\r\n")
  23. endif
  24. let &selection = l:sel_save
  25. let @@=l:reg_save
  26. endfunction
  27. nnoremap <silent> <Plug>(ExecThatThingNormal) :set opfunc=ExecuteThatThing<CR>g@
  28. vnoremap <silent> <Plug>(ExecThatThingVisual) :<C-U>call ExecuteThatThing(visualmode(), 1)<CR>
  29. " For executing the current line, by analogy with other 'inner motions' such as iw, i` etc
  30. onoremap <silent> <Plug>(InnerLineMotion) :<C-U>normal! 0v$<CR>
  31. " Treat first two characters as a prompt to be ignored. Execute the rest of the line.
  32. " WARNING: This is NOT robust, i.e. it simply silently skips the leading characters.
  33. " It does not check whether they actually look like a prompt,
  34. " and it gives an error for lines with fewer than three characters.
  35. onoremap <silent> <Plug>(InnerCommandMotion) :<C-U>normal! 3\|v$<CR>