Skip to content

Instantly share code, notes, and snippets.

@Konfekt
Last active April 16, 2024 05:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Konfekt/52efaf7d9ee84d7f87f95fc6748e170b to your computer and use it in GitHub Desktop.
Save Konfekt/52efaf7d9ee84d7f87f95fc6748e170b to your computer and use it in GitHub Desktop.
Add evaluation of selected code as comment
" From https://old.reddit.com/r/neovim/comments/1c3gf93/commentreplnvim_run_code_from_your_buffer_and/
"
" Add the following lines to `ftplugin/{python,sh,zsh,lua,perl, ...}.vim
" and call :EvaluateAsComment to add the evaluation of the selected lines as a comment:
"
" print(1+2)
"
" becomes
"
" # 3
" print(1+2)
"
" Alternatively, https://github.com/gpanders/vim-medieval evaluates code in a (markdown) code block
let b:evaluator = &filetype
command! -buffer -range -bar EvaluateAsComment
\ silent <line1>,<line2>copy<line2> |
\ exe "<line1>,<line2>!"..b:evaluator.." | sed 's/^.*$/"..printf(escape(&commentstring, '\/.*^$[]#'), '\0').."/'"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment