0

I've created the following script on my vimrc file:

function! ExecuteOnTerminal() 
    if (&ft=='javascript')
        :%terminal node
    elseif (&ft=='sh')
        :%terminal bash
    elseif (&ft=='python')
        :%terminal python3
    endif
endfunction

map <F6> :silent call ExecuteOnTerminal()<CR>

It works fine... It detects the type of the file that I'm editing and when I press F6 and I'm in Normal mode it will execute the current file on a splited terminal window. However, I'd like to go beyond this functionality and detect if I currently have selected lines on Vim. if I do, I'd like to execute only the current selected lines. If I don't, I'd like to execute the entire file (executing the entire file is the current behavior).

When I try to press F6 with this script I receive the error No range allowed. Is there any way of detecting if I'm in visual mode and executing a different command for it? Or would it be better creating a new command?

1 Answer 1

3
  1. "No range allowed" is due to :silent that never accepts line range, obviously. If you need to silent your command while still accepting line range you have to employ some alternative. Maybe :h map-silent, or :h map-cmd, etc.etc.

  2. As your function doesn't have the range keyword it will be executed once per each line in a given range that is probably not what you want. Hence, you'd better to add range to it and to process it accordingly. See :h :func-range, :h :call, :h function-range-example and all such.

Yet another alternative is to drop line range forcefully (by :h c_CTRL-U or :h map-cmd) and then to get line numbers with bookmarks '< and '> (for Visual-mode mapping) or line(".") and :h v:count (for Normal-mode mapping).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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