简体   繁体   中英

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

I have this as a Sublime Text build system right now: It 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}'"
}

However, I'm not sure how I could get something in (Mac)Vim. I've read some similar questions, but none of the ones I've seen mention opening in a separate terminal.

I've tried writing some shortcuts/commands to compile and run within Vim which mostly worked, but I would still rather my programs run in a separate window (edited).

noremap <C-b> :!g++-11 %:p && ./a.out<CR>

希望neovim的这个插件可以帮到你: https : //github.com/michaelb/sniprun

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM