c++vimmacvim

How can I write a shortcut to compile and run a program in a separate terminal window in Vim? (OSX)


This Sublime Text build system compiles a C++ program, then opens a new window in Terminal.app and runs it there upon pressing ctrl+b.

{
    "shell_cmd": "g++-11 -std=c++20 '${file}' -o '${file_base_name}' && echo 'cd \"${file_path}/\"' > '/tmp/${file_base_name}' && echo './\"${file_base_name}\"' >> '/tmp/${file_base_name}' && echo read >> '/tmp/${file_base_name}' && chmod +x '/tmp/${file_base_name}' && open -a Terminal.app '/tmp/${file_base_name}'"
}

How would you write something similar for Vim?


Solution

  • You actually have a few options, so use whatever suits your needs the most.

    You can use vim's built in terminal :terminal (or :term; see :help :terminal), which is probably the easier way:

    :term g++-11 %:p && ./a.out<CR>
    

    Or you can use :compiler with :make (see :help :compile and :help 'makeprg'):

    :compiler gcc
    :let $CXXFLAGS='-std=c++20 -Wall -Werror'
    :make