skywind3000 / asyncrun.vim

:rocket: Run Async Shell Commands in Vim 8.0 / NeoVim and Output to the Quickfix Window !!
https://www.vim.org/scripts/script.php?script_id=5431
MIT License
1.86k stars 111 forks source link

How to compile and save object file into new folder #192

Closed paraduxos closed 4 years ago

paraduxos commented 4 years ago

This is my current neovim command

:AsyncRun g++ -Wall -O2 "$(VIM_FILEPATH)" -o "$(VIM_FILEDIR)/$(VIM_FILENOEXT)" <cr>

How to create one subdirectory (if not exist) and save there, for instance:

" Create obj_dir if not exist
" Then save here
:AsyncRun g++ -Wall -O2 "$(VIM_FILEPATH)" -o "$(VIM_FILEDIR)/obj_dir/$(VIM_FILENOEXT)" <cr>
skywind3000 commented 4 years ago

concatinate commands with &&:

:AsyncRun command1 && command2
paraduxos commented 4 years ago

Thank you. This is what I did.

" create directory {cwd/object_files}
function! MakeObjectDir()
    " if not yet created
    if !isdirectory(expand("%:p:h") . '/object_files')
        call mkdir(expand("%:p:h") . '/object_files', 'p')
        echo "Created directory:\n" . expand("%:p:h") . '/object_files'
    else
        echo "Directory already exist:\n" . expand("%:p:h") . "/object_files"
    endif
endfunction

" run function before compile
nnoremap <silent> <leader>2 :call MakeObjectDir() <cr> <bar> :AsyncRun g++ -Wall -O2 "$(VIM_FILEPATH)" -o "$(VIM_FILEDIR)/object_files/$(VIM_FILENOEXT)" <cr>
nnoremap <silent> <leader>3 :AsyncRun -mode=term -pos=right -cwd=$(VIM_FILEDIR) "$(VIM_FILEDIR)/object_files/$(VIM_FILENOEXT)" <cr>
ASC8384 commented 4 years ago

concatinate commands with &&:

:AsyncRun command1 && command2

请问大佬,我使用neovim,想实现用一个命令完成「编译并运行」这个功能,该怎么写呢?

我试过:AsyncRun -save=1 g++ $(文件名) -o $(文件).exe && (:AsyncRun这个括号的内容加不加都一样) -mode=term -pos=right $(文件).exe,十分可惜不起作用。

如果使用autocmd User AsyncRunStop func的话,会导致其他AsyncRun命令结束后也使用了这里的函数,这就会造成很多困扰。

谢谢!

skywind3000 commented 4 years ago
:AsyncRun -save=1 g++ $(VIM_FILEPATH) -o $(VIM_PATHNOEXT).exe && $(VIM_PATHNOEXT).exe
ASC8384 commented 4 years ago
:AsyncRun -save=1 g++ $(VIM_FILEPATH) -o $(VIM_PATHNOEXT).exe && $(VIM_PATHNOEXT).exe

可惜这样只能在quickfix里显示输出,但我需要很多无法事先确定内容的输入,所以添加了-mode=term选项。

skywind3000 commented 4 years ago

自己写个脚本调用下呗。

skywind3000 commented 4 years ago

你还可以测试下:

let g:asyncrun_term_safe = 1

使用安全传参方式。

ASC8384 commented 4 years ago

你还可以测试下:

let g:asyncrun_term_safe = 1

使用安全传参方式。

还是无法在终端中打开并输入,我还是去研究研究脚本该怎么写吧,麻烦大佬了