Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
vim: send text-selection to bash

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.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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>
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!