Skip to main content
added 3 characters in body
Source Link
Vivian De Smedt
  • 21.2k
  • 5
  • 22
  • 40
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
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
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
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
Source Link
Mega Bang
  • 319
  • 1
  • 12

Neovim: Converting Vimscript Terminal Functions to Lua

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