Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda