2

Author: @mizlan
Source: https://github.com/mizlan/vim-and-cp/blob/master/cp.vim#L16

This code adds charm to neovim integrated terminal.
For cpp programs, I have these: F9 compile, F10 run, F12 Compile and run

tnoremap <Esc> <C-\><C-n>

" https://vi.stackexchange.com/a/22327/36876 if has('nvim') augroup terminal_setup | au! autocmd TermOpen * nnoremap <buffer><LeftRelease> <LeftRelease>i augroup end endif

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' endfunction

command! -nargs=0 Compile call TermWrapper(printf('g++ -std=c++17 -O2 -Wall %s -o %s.out', expand('%'), expand('%:r'))) autocmd FileType cpp nnoremap <F9> <Esc>:w <bar> Compile<CR>

command! -nargs=0 Run call TermWrapper(printf('./%s.out', expand('%:r'))) autocmd FileType cpp nnoremap <F10> :Run<CR>

command! -nargs=0 CompileAndRun call TermWrapper(printf('g++ -std=c++17 -O2 -Wall %s -o %s.out && ./%s.out', expand('%'), expand('%:r'), expand('%:r'))) autocmd FileType cpp nnoremap <F12> <Esc>:w <bar> CompileAndRun<CR>

let g:split_term_style = 'horizontal' let g:split_term_resize_cmd = 'resize 30' set splitbelow

This is my first try at Lua conversion. Some fixes would be appreciated.

vim.api.nvim_set_keymap('t', '<Esc>', '<C-\\><C-n>', { noremap = true })

if vim.fn.has('nvim') == 1 then vim.cmd[[ augroup terminal_setup autocmd! autocmd TermOpen * nnoremap <buffer><LeftRelease> <LeftRelease>i augroup end ]] end

function TermWrapper(command) if not vim.g.split_term_style then vim.g.split_term_style = 'vertical' end local buffercmd if vim.g.split_term_style == 'vertical' then buffercmd = 'vnew' elseif vim.g.split_term_style == 'horizontal' then buffercmd = 'new' else vim.api.nvim_err_writeln('ERROR! g:split_term_style is not a valid value (must be ''horizontal'' or ''vertical'' but is currently set to ''' .. vim.g.split_term_style .. ''')') error('ERROR! g:split_term_style is not a valid value (must be ''horizontal'' or ''vertical'')') end vim.cmd(buffercmd) if vim.g.split_term_resize_cmd then vim.cmd(vim.g.split_term_resize_cmd) end vim.cmd('term ' .. command) vim.cmd('setlocal nornu nonu') vim.cmd('startinsert') end

vim.cmd('command! -nargs=0 Compile call TermWrapper(printf(''g++ -std=c++17 -O2 -Wall %s -o %s.out'', expand(''%''), expand(''%:r'')))') vim.cmd('autocmd FileType cpp nnoremap <F9> <Esc>:w <bar> Compile<CR>')

vim.cmd('command! -nargs=0 Run call TermWrapper(printf(''./%s.out'', expand(''%:r'')))') vim.cmd('autocmd FileType cpp nnoremap <F10> :Run<CR>')

vim.cmd('command! -nargs=0 CompileAndRun call TermWrapper(printf(''g++ -std=c++17 -O2 -Wall %s -o %s.out && ./%s.out'', expand(''%''), expand(''%:r''), expand(''%:r'')))') vim.cmd('autocmd FileType cpp nnoremap <F12> <Esc>:w <bar> CompileAndRun<CR>')

vim.g.split_term_style = 'horizontal' vim.g.split_term_resize_cmd = 'resize 30' vim.opt.splitbelow = true

This error message is being displayed.

E5112: Error while creating lua chunk: /home/killerbee/.config/nvim/init.lua:64:
 ')' expected near ''horizontal''
Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
Mega Bang
  • 199
  • 11

1 Answers1

1

I would do that:

vim.api.nvim_set_keymap('t', '<Esc>', '<C-\\><C-n>', { noremap = true })

if vim.fn.has('nvim') == 1 then vim.cmd[[ augroup terminal_setup autocmd! autocmd TermOpen * nnoremap <buffer><LeftRelease> <LeftRelease>i augroup end ]] end

function TermWrapper(command) if not vim.g.split_term_style then vim.g.split_term_style = 'vertical' end local buffercmd if vim.g.split_term_style == 'vertical' then buffercmd = 'vnew' elseif vim.g.split_term_style == 'horizontal' then buffercmd = 'new' else vim.api.nvim_err_writeln("ERROR! g:split_term_style is not a valid value (must be 'horizontal' or 'vertical' but is currently set to '" .. vim.g.split_term_style .. "')") error("ERROR! g:split_term_style is not a valid value (must be 'horizontal' or 'vertical')") end vim.cmd(buffercmd) if vim.g.split_term_resize_cmd then vim.cmd(vim.g.split_term_resize_cmd) end vim.cmd('term ' .. command) vim.cmd('setlocal nornu nonu') vim.cmd('startinsert') end

vim.cmd([[command! -nargs=0 Compile lua TermWrapper(string.format('g++ -std=c++17 -O2 -Wall %s -o %s.out', vim.fn.expand("%"), vim.fn.expand("%:r")))]]) vim.cmd('autocmd FileType cpp nnoremap <F9> <Esc>:w <bar> Compile<CR>')

vim.cmd([[command! -nargs=0 Run lua TermWrapper(string.format("./%s.out", vim.fn.expand("%:r")))]]) vim.cmd('autocmd FileType cpp nnoremap <F10> :Run<CR>')

vim.cmd([[command! -nargs=0 CompileAndRun lua TermWrapper(string.format('g++ -std=c++17 -O2 -Wall %s -o %s.out && ./%s.out', vim.fn.expand("%"), vim.fn.expand("%:r"), vim.fn.expand("%:r")))]]) vim.cmd('autocmd FileType cpp nnoremap <F12> <Esc>:w <bar> CompileAndRun<CR>')

vim.g.split_term_style = 'horizontal' vim.g.split_term_resize_cmd = 'resize 30' vim.opt.splitbelow = true

PS: Since it is unclear what it is supposed to do for me I couldn't test properly yet (but it runs).

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
  • Yep, it runs. To test it, write a .cpp program. With given vimscript code and commands, we can compile, run, compile & run the c++ program internally. – Mega Bang Oct 11 '23 at 11:38
  • With this lua code, Neovim can't detect the three commands. – Mega Bang Oct 11 '23 at 11:40
  • 1
    Thanks for the feedback. Here is a new version where the commands are defined successfully – Vivian De Smedt Oct 11 '23 at 11:53
  • 1
    Wrong value sets the error message. Got it. Now, it is fool-proof. – Mega Bang Oct 11 '23 at 12:38
  • 1
    I have to admit, I don't see much teaching here. These Q&A won't be reusable: future querents will have a hard time understanding the conversion or how it might apply to them. I appreciate that it takes less effort to just post the code, but it's not my favorite kind of answer. – D. Ben Knoble Oct 11 '23 at 16:26