1

I have a command:

command! -nargs=1 TmuxSession !tmux.session "<args>"

and I want to run it in visual mode so that selected text will be passed as argument.
By selected text I mean selected in visual mode in vim.

How can I achieve that?

I tried running

command -nargs=1 -range -register TmuxSession !tmux.session "<args>"

but it keeps saying E471: Argument required

Edit:
I know about using <C-r>" to paste an argument and that is how I use the upper command at the moment, but I don't want to copy selection and paste it every time to the command, because this can be done faster using range.

Edit2:

Following solution does not work:

function! PrintRange() range
    echo getline(a:firstline, a:lastline)
endfunction

command! -range PassRange <line1>,<line2>call PrintRange()

because when I want to select second and third word from line containing one two three - I get the whole line (obviously).

I need to pass only the selected part of the text.

Thanks

3

See this answer on stackoverflow.

The example given is:

 function! PrintGivenRange() range
     echo "firstline ".a:firstline." lastline ".a:lastline
     " Do some more things
 endfunction

 command! -range PassRange <line1>,<line2>call PrintGivenRange()

Thus you might want to create a function which would use the range and do the tmux call you want.

In your function, to get the text in the range you can use the getline() function which will take a:firstline and a:lastline as parameters and return the text in the buffer between theses lines. You can have a look at :h getline() or :h getbufline().


Edit If you want to get the text characterwise and not linewise you'll have to play with the marks. This edit is inspired from this SO answer.

So when you call you command with :'<,'>PassRange the marks '< and '> are set. The doc :h '< tells us that these marks are used to describe the last visual selection.

Your function would need to look like this:

function! PrintGivenRange() range
    " Get the line and column of the visual selection marks
    let [lnum1, col1] = getpos("'<")[1:2]
    let [lnum2, col2] = getpos("'>")[1:2]

    " Get all the lines represented by this range
    let lines = getline(lnum1, lnum2)         

    " The last line might need to be cut if the visual selection didn't end on the last column
    let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)]
    " The first line might need to be trimmed if the visual selection didn't start on the first column
    let lines[0] = lines[0][col1 - 1:]

    " Get the desired text
    let selectedText = join(lines, "\n")         

    " Do the call to tmux
    execute "!tmux.session " . selectedText 
endfunction
  • Thanks, but I've seen this answer and it is not helpful. The return of this echo is giving me line numbers but I need to use the actual selection content. – lewiatan Jan 18 '17 at 14:10
  • @lewiatan in your function you can then use functions like getline() or getbufline() for example to get the content of your buffer between these lines. See :h getline() or :h getbufline() – statox Jan 18 '17 at 14:14
  • it is better now, but I still think that's not it. There must be a better way to get the selection. – lewiatan Jan 18 '17 at 14:29
  • @lewiatan Let's way for an answer which better fits your needs then :) – statox Jan 18 '17 at 14:32
  • 1
    @lewiatan I think I understand better what you want now. Have a look at this one you called your command with the range '<,'> you can play with getpos() to get the exact position of the mark on the line and thus the code. – statox Jan 18 '17 at 14:58
2

you can also adapt this one liner

command -range TmuxSession execute "!tmux.session " . @*

or if you do need multiline visual selection change @* to substitute(@*,"\n"," ","g")

The @* register stores current selection (for linux, on windows there could be different naming convention).

The -range is only to avoid an error if you want to execute this command with range marks, i.e. '<,'>TmuxSession.

  • I did not express myself specific enough. What I meant was the text that is selected in visual mode, but this is also useful. – lewiatan Jan 19 '17 at 8:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.