-1

How can I write a command that does the following?

  • With one mapping, compile and/or run a program based on the filetype (*.c, *.cpp, *.js, *.py, etc.), and open a vertical terminal window with the output.
  • With another mapping, close the vertical terminal window.

I know you can open the terminal next to Vim with tmux, but I want to do it with just Vim.

1 Answer 1

3

Yes. This is basically a 2 part question.

  1. Can you compile a program in vim, yes :make will run your Makefile, you can also set makeprg to your compile program if its different.

type :he :make and :he makeprg for more info

  1. Can you see the output of a command in vim, yes

:vertical terminal ++shell ++cols=40 dir Will run the command dir and show the output in a vertical split window.

So combining these together, you get this

nnoremap <c-m> :make<cr>:vertical terminal ++shell ++cols=40 YourProgram.exe<cr>

type :he :vertical and :he :terminal for more info

An additional tip, to close the window <C-w>c is a default keybind in Vim to do that for you.

6
  • but how to manage extension .c , .cpp , .py ?
    – Visrut
    Commented Oct 2, 2020 at 10:20
  • @Visrut for that, set your makeprg appropriately (based on filetype or project structure or some other mechanism). And you can adjust the map to be more generic (“run the program in the variable b:compiled_program, for example, and set that variable too)
    – D. Ben Knoble
    Commented Oct 2, 2020 at 12:41
  • @D.ben Knoble if you know vim script than please write your answer, if you found any way to do that because I don't know vim script very well.
    – Visrut
    Commented Oct 2, 2020 at 15:44
  • 1
    May I add that in most cases we don't need to change makeprg for languages supported by gnumake implicit rules, i.e. C++, C, and Fortran at least -- there may be other languages. However, the right compilation command would be :make %< for single file programs. And to execute the result it would be :vert term ./%<. Also, for some languages, proper 'makeprg, and errorformat are provided through compiler plugins (:h compiler) Commented Oct 3, 2020 at 9:50
  • 1
    I get some issues with neovim , I think terminal not properly shown , we have to use plugin called floatterm in nvim , so anybody know how to do that
    – Visrut
    Commented Oct 6, 2020 at 15:45

Your Answer

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

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