Skip to content

Commit aacc873

Browse files
new autocommand
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
1 parent c94e3a6 commit aacc873

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed

lua/core/autocommands.lua

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,3 +224,59 @@ vim.api.nvim_create_autocmd({ "BufWritePre" }, {
224224
vim.fn.mkdir(vim.fn.fnamemodify(file, ":p:h"), "p")
225225
end,
226226
})
227+
228+
-- CPP purposes
229+
local function TermWrapper(command)
230+
if vim.g.split_term_style == nil then
231+
vim.g.split_term_style = "vertical"
232+
end
233+
234+
local buffercmd
235+
if vim.g.split_term_style == "vertical" then
236+
buffercmd = "vnew"
237+
elseif vim.g.split_term_style == "horizontal" then
238+
buffercmd = "new"
239+
else
240+
error(
241+
'ERROR! g:split_term_style is not a valid value (must be "horizontal" or "vertical" but is currently set to "'
242+
.. vim.g.split_term_style
243+
.. '")'
244+
)
245+
end
246+
247+
vim.cmd(buffercmd)
248+
249+
if vim.g.split_term_resize_cmd ~= nil then
250+
vim.cmd(vim.g.split_term_resize_cmd)
251+
end
252+
253+
vim.cmd("term " .. command)
254+
vim.cmd("setlocal nornu nonu")
255+
vim.cmd("startinsert")
256+
vim.api.nvim_create_autocmd("BufEnter", {
257+
buffer = 0,
258+
command = "startinsert",
259+
})
260+
end
261+
262+
vim.api.nvim_create_user_command("CompileAndRun", function()
263+
local fileName = vim.fn.expand("%")
264+
local exeName = fileName:gsub("%.cpp$", "")
265+
TermWrapper("g++ -std=c++11 -o " .. exeName .. " " .. fileName .. " && ./" .. exeName)
266+
end, {})
267+
vim.api.nvim_create_user_command("CompileAndRunWithFile", function(args)
268+
TermWrapper("g++ -std=c++11 " .. vim.fn.expand("%") .. " && ./a.out < " .. args.args)
269+
end, {
270+
nargs = 1,
271+
complete = "file",
272+
})
273+
274+
vim.api.nvim_create_autocmd("FileType", {
275+
pattern = "cpp",
276+
command = "nnoremap <leader>fw :CompileAndRun<CR>",
277+
})
278+
279+
vim.api.nvim_create_autocmd("FileType", {
280+
pattern = "cpp",
281+
command = "nnoremap <leader>fr :CompileAndRunWithFile<CR>",
282+
})

0 commit comments

Comments
 (0)