Some time ago, I've made this question where I asked how to execute a file on the terminal with NeoVim... The solution I found was the following :terminal bash %
. Knowing that, I made the following configuration on my init.vim
file:
function! ExecuteOnTerminal(type) range
if (&ft=='javascript')
if a:type == "V"
:vert botright '<,'> terminal node %
else
:vert botright terminal node %
endif
elseif (&ft=='sh')
if a:type == "V"
:vert botright '<,'> terminal bash %
else
:vert botright terminal bash %
endif
elseif (&ft=='python')
if a:type == "V"
:vert botright '<,'> terminal python3 %
else
:vert botright terminal python3 %
endif
endif
endfunction
vnoremap <silent> <F6> :call ExecuteOnTerminal("V")<CR>
nnoremap <silent> <F6> :call ExecuteOnTerminal("I")<CR>
The above configuration works perfectly fine, it allows me to execute a file with bash
, node.js
and python
when I press F6
... And if I select some lines and press F6
it`ll execute only the selected lines.
The problem is that in my context, I need to execute files that are not stored on the disk. Let's say I have the following file.sh
:
echo "Hello World"
I need to open this file with cat file.sh | nvim -
(not nvim file.sh
)... When I do that, if I try using the command :vert botright terminal bash %
I receive the error:
E499: Empty file name for '%' or '#', only works with ":p:h"
I tried using set ft=sh
but it didn't work as well. Is there any workaround for this problem? In a way that I don't need to save this file on the disk to execute it with nvim
?
Observation: With vim
, I have a similar configuration in my vimrc
file, the only difference is that I use the commands :vert botright '<,'> %terminal bash
and vert botright %terminal bash
instead. With vim
it works perfectly fine to execute files opened from pipe. Like in cat file.sh | vi -
. This issue only exists on NeoVim.
term_start
and a command with the text to execute or anin_buf
if the command accepts stdin