How can I send the currently selected (in visual mode) text content to a bash command?
I know, bash commands can be called with :!, but for the rest I don't find documentation.
First, you can get visually selected text by a function. I brought this from https://stackoverflow.com/a/6271254/3108885:
function! s:GetVisualSelection()
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = getline(lnum1, lnum2)
let lines[-1] = lines[-1][:col2 - (&selection == 'inclusive' ? 1 : 2)]
let lines[0] = lines[0][col1 - 1:]
return join(lines, "\n")
endfunction
Then add a map for Visual mode:
vnoremap <buffer> <F5> :<C-U>exec '!python -c' shellescape(<SID>GetVisualSelection(), 1)<CR>
If you press F5, visually selected python code will be executed. You may define this map only for Python code, by prepending autocmd FileType python before vnoremap. So multiple file types can be handled.
autocmd FileType python vnoremap <buffer> <F5> :<C-U>exec '!python -c' shellescape(<SID>GetVisualSelection(), 1)<CR>
autocmd FileType ruby vnoremap <buffer> <F5> :<C-U>exec '!ruby -e' shellescape(<SID>GetVisualSelection(), 1)<CR>