Skip to content

Commit

Permalink
new autocommand
Browse files Browse the repository at this point in the history
a keymap which trigger neovim to compile C++ into an executable.
then run it inside neovim by opening a vertical terminal
pressing any kew will close the terminal
  • Loading branch information
bryant-the-coder committed Aug 12, 2024
1 parent c94e3a6 commit aacc873
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lua/core/autocommands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,59 @@ vim.api.nvim_create_autocmd({ "BufWritePre" }, {
vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
end,
})

-- CPP purposes
local function TermWrapper(command)
if vim.g.split_term_style == nil 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
error(
'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
.. '")'
)
end

vim.cmd(buffercmd)

if vim.g.split_term_resize_cmd ~= nil then
vim.cmd(vim.g.split_term_resize_cmd)
end

vim.cmd("term " .. command)
vim.cmd("setlocal nornu nonu")
vim.cmd("startinsert")
vim.api.nvim_create_autocmd("BufEnter", {
buffer = 0,
command = "startinsert",
})
end

vim.api.nvim_create_user_command("CompileAndRun", function()
local fileName = vim.fn.expand("%")
local exeName = fileName:gsub("%.cpp$", "")
TermWrapper("g++ -std=c++11 -o " .. exeName .. " " .. fileName .. " && ./" .. exeName)
end, {})
vim.api.nvim_create_user_command("CompileAndRunWithFile", function(args)
TermWrapper("g++ -std=c++11 " .. vim.fn.expand("%") .. " && ./a.out < " .. args.args)
end, {
nargs = 1,
complete = "file",
})

vim.api.nvim_create_autocmd("FileType", {
pattern = "cpp",
command = "nnoremap <leader>fw :CompileAndRun<CR>",
})

vim.api.nvim_create_autocmd("FileType", {
pattern = "cpp",
command = "nnoremap <leader>fr :CompileAndRunWithFile<CR>",
})

0 comments on commit aacc873

Please sign in to comment.