1

I've recently switched to neovim (NVIM v0.4.4) from vim.

Previously in my .vimrc file I had these two mappings that allowed me to run javascript and c++ files inside vim.

nnoremap <f4> :!node %<CR>
nnoremap <f9> :!clear && g++ -std=c++17 % -Wall -g -o %.out && ./%.out<CR>

But in neovim if I use them some programs don’t print things correctly and don’t accept user input. For example this JavaScript code doesn't work:

process.stdin.pipe(process.stdout);

And the output I get is:

:! node index.js
[No write since last change]                                                                             

Press ENTER or type command to continue

then if I press any key it just closes the command panel.

After that I found the following code snippet that allowed me to open the terminal in neo-vim.

"open terminal on ctrl+n"
function! OpenTerminalVartically()
  vsplit term://bash
endfunction

function! OpenTerminalHorizontally()
  split term://bash
  resize 10
endfunction
nnoremap <c-n> :call OpenTerminalHorizontally()<CR>
nnoremap <c-m> :call OpenTerminalVartically()<CR>

At first it just opened the terminal below the code editor. Then I figured out a way to open it vertically by modifying the function.

As I'm new to vim I don't know much about vim-scripting. Is there any way I can do the following:

nnoremap <f4> :call OpenTerminalVartically(node %)<CR>
" I just want to pass the command in the function and execute it in the newly opened terminal.

to run the current JavaScript file? Could you kindly give me some hint how can I accomplish this?

5
  • What happens when you do something like :split term://node ?
    – B Layer
    Jul 19 at 12:57
  • Hey @BLayer I modified the function as ":split term://node %" and it successfully ran the currently opened JavaScript file. But how can pass this <kbd>node %</kbd> as an argument to the function?
    – h-sifat
    Jul 19 at 13:58
  • Why are you passing "node"? :call myfunc(%) and then in the function you build a command with :execute....something like exe ":split term://node " . a:filename.
    – B Layer
    Jul 19 at 14:47
  • I’ve edited for some code formatting, clarity in a few places, and in particular note that thanks/blessings/etc., while polite in everyday conversation, are simply eschewed in questions and answer (that is not to say you cannot be polite! Just that the politeness must serve the function of the question or answer, rather than being extraneous).
    – D. Ben Knoble
    Jul 19 at 21:07
  • @D.BenKnoble Thanks for your edit. I didn't know much about that.
    – h-sifat
    Jul 20 at 6:38
1

As explained here, among other places (better references welcome), NeoVim's ! is implemented using a subprocess with no interactive TTY attached.

Use :terminal to run interactive commands inside NeoVim.

Change your mappings to

nnoremap <f4> :terminal node %<CR>
nnoremap <f9> :terminal g++ -std=c++17 % -Wall -g -o %.out && ./%.out<CR>

or maybe consider using an ftplugin to map <buffer> a key to each file-types runner, or (my favorite) set the :compiler appropriately for buffers and use :make. Use :terminal to run interactive things, of course.

1
  • Actually, for the last one, you may need :terminal sh -c '…'
    – D. Ben Knoble
    Jul 19 at 21:07

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.