0

Recently I've been into VIM and had many attempts to use it for competitive programming. So I tried google stuffs for information and up until now I've gathered codes from other's .vimrc while avoid using plugins (for some specific reasons). So finally I'm now using a pretty decent compile function that I borrowed from Mr.Michael Lan. Now part of my .vimrc look like this :

function! TermWrapper(command) abort
    if !exists('g:split_term_style') | let g:split_term_style = 'vertical' | endif
    if g:split_term_style ==# 'vertical'
        let buffercmd = 'vnew'
    elseif g:split_term_style ==# 'horizontal'
        let buffercmd = 'new'
    else
        echoerr 'ERROR! g:split_term_style is not a valid value (must be ''horizontal'' or ''vertical'' but is currently set to ''' . g:split_term_style . ''')'
        throw 'ERROR! g:split_term_style is not a valid value (must be ''horizontal'' or ''vertical'')'
    endif
    exec buffercmd
    if exists('g:split_term_resize_cmd')
        exec g:split_term_resize_cmd
    endif
    exec 'term ' . a:command
    exec 'setlocal nornu nonu'
    exec 'startinsert'
    autocmd BufEnter <buffer> startinsert
endfunction

let g:split_term_style = 'vertical'
let g:split_term_resize_cmd = 'vertical resize 80'

command! -nargs=0 CompileAndRun call TermWrapper(printf('g++ -std=c++11 %s && ./a.out', expand('%')))
autocmd FileType cpp nnoremap <leader>fw :CompileAndRun<CR>

As you can see, if I try :CompileAndRun, it will open a new buffer to the right side of my screen, run the .cpp file, etc ... But there is one problem I'm having with this. If you try :CompileAndRun for the second time, it will open a new buffer to run instead of using the old one, which I find ... annoying (well it kinda bug you if your screen for the main cpp file keep getting smaller and smaller, especially in a running contest am I right ?). And I don't see deleting these buffers manually as an option, as it is not very convenient. So can any of you guys help me to cope with this tedious task. Remember that I still want to keep using the :CompileAndRun command, and just want to reuse the buffer opened by the previous command.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Airi
  • 29
  • 4
  • 1
    The use of `:vnew` and `:new` shows that that snippet is designed for Neovim, not Vim. Both have the `:term` command but they are implemented differently so you will have to look up `:help :term` on your own or wait until a Neovim user solves your problem. – romainl Nov 07 '21 at 10:58
  • @romainl but sir, vnew worked well on my VIM .. At least if we are talking about opening a new buffer – Airi Nov 07 '21 at 11:06
  • 1
    In Neovim, `:vnew` then `:term` opens a new vertical window and turns it into a terminal, which leaves you with *2* windows: your code and a terminal for compiling and running. In Vim, the same sequence opens a new vertical window and then a horizontal terminal window, leaving you with *three* windows: your code, a useless empty window, and the terminal. This means that the snippet was written for Neovim, not Vim. So either you use Vim and that snippet needs more rewriting than you asked for (and the vim tag) or you use Neovim and it's a problem for Neovim users to solve. – romainl Nov 07 '21 at 11:42
  • 1
    FWIW: 1. The guards around `g:split_term_style` make sense in a generic plugin but not in a snippet in your `vimrc`. 2. `g:split_term_resize_cmd` is also pretty useless as Vim's `:term` takes up all the available space anyway. Plus, `:vertical resize 80` doesn't do anything useful. 3. Once the command has been executed, the terminal buffer is readonly so there is no point in systematically entering insert mode. You should probably explain your use case precisely so that we can modify that snippet to fit your needs. – romainl Nov 07 '21 at 12:12
  • @romainl Well, forgive my inexperience. As I describe above, I want a command to open a new buffer and run a C++ binary file (a.out by default after you run g++ *.cpp). Also, if I repeat that command, I want to open that binary file in the old buffer if I've already open one to insert input. Pls kindly help me. – Airi Nov 07 '21 at 12:31

0 Answers0