Skip to main content
2 of 5
added 192 characters in body
Vivian De Smedt
  • 21.2k
  • 5
  • 22
  • 40

Here is the aggregated solution (still partial):

if vim.fn.exists('g:os') == 0 then
    local is_windows = vim.fn.has("win64") == 1 or vim.fn.has("win32") == 1 or vim.fn.has("win16") == 1
    if is_windows then
        vim.g.os = "Windows"
    else
        local uname_output = vim.fn.system('uname')
        vim.g.os = string.gsub(uname_output, '\n', '')
    end
end

function TabLcd()
    local current_tab = vim.fn.tabpagenr()
    vim.cmd("tabdo <commands>")
    vim.cmd("execute 'tabnext' " .. current_tab)
end

vim.api.nvim_set_keymap('n', '<leader>lcd', ':lua TabLcd()<CR>', { noremap = true, silent = 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 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

vim.api.nvim_set_keymap('n', '<Plug>(StopHL)', "execute('nohlsearch')[-1]", { noremap = true, expr = true })
vim.api.nvim_set_keymap('i', '<Plug>(StopHL)', "execute('nohlsearch')[-1]", { noremap = true, expr = true })

function HlSearch()
    local pos = vim.fn.match(vim.fn.getline('.'), vim.fn.getreg('/'), vim.fn.col('.') - 1) + 1
    if pos ~= vim.fn.col('.') then
        StopHL()
    end
end

function StopHL()
    if not vim.v.hlsearch or vim.fn.mode() ~= 'n' then
        return
    else
        vim.cmd[[silent! call feedkeys("\<Plug>(StopHL)", 'm')]]
    end
end

local searchHighlightGrp = vim.api.nvim_create_augroup('SearchHighlight', { clear = true })
vim.api.nvim_create_autocmd({"CursorMoved"}, {group = searchHighlightGrp, pattern = '*', callback = HlSearch})
vim.api.nvim_create_autocmd({"InsertEnter"}, {group = searchHighlightGrp, pattern = '*', callback = StopHL})

vim.api.nvim_create_autocmd({'TextYankPost'}, { callback = function() vim.highlight.on_yank {higroup="IncSearch", timeout=300} end })

Vivian De Smedt
  • 21.2k
  • 5
  • 22
  • 40