How to call an external program from vim with visually labeled text as a parameter?

Call Scheme: path-to-programm visual-marked-text filetype directory

Example: "C:\Programme\WinGrep\grep32.exe" search-pattern *.sql D:\MyProject\build

+3
source share
4 answers

To do this, you can use the following Vim-script function.

function! FeedVisualCmd(cmdpat)
    let [qr, qt] = [getreg('"'), getregtype('"')]
    silent norm! gvy
    let cmd = printf(a:cmdpat, shellescape(@"))
    call setreg('"', qr, qt)
    echo system(cmd)
    if v:shell_error
        echohl ErrorMsg | echom 'Failed to run ' . cmd | echohl NONE
    endif
endfunction

It copies the selected text to an unnamed register (see :help ""), launches the specified command template through a function printf, and then executes the resulting command, echoes its output.

If the only part of the command that changes is a template, it’s convenient to define a mapping,

vnoremap <leader>g :<c-u>call FeedVisualCmd('"C:\Programme\WinGrep\grep32.exe" %s *.sql D:\MyProject\build')<cr>
+4
source

You select the text and then enter:

:!<program>

For example, to sort rows, select them and enter:

:!sort

,

+4

y :

:! cmd Ctrl-R " [ ]

+4

, xargs linux/unix ( cygwin windows), :

:'<,'>!xargs -I {} path-to-program {} filetype directory

, , :, ! .

{} xargs, . , path-to-program ( ).

0

Source: https://habr.com/ru/post/1775148/


All Articles