| 1 | -- Autocmds are automatically loaded on the VeryLazy event |
| 2 | -- Default autocmds that are always set: https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/config/autocmds.lua |
| 3 | -- |
| 4 | -- Add any additional autocmds here |
| 5 | -- with `vim.api.nvim_create_autocmd` |
| 6 | -- |
| 7 | -- Or remove existing autocmds by their group name (which is prefixed with `lazyvim_` for the defaults) |
| 8 | -- e.g. vim.api.nvim_del_augroup_by_name("lazyvim_wrap_spell") |
| 9 | |
| 10 | local autocmd = vim.api.nvim_create_autocmd |
| 11 | |
| 12 | -- remove trailing whitespaces on :w |
| 13 | autocmd({ "bufwritepre" }, { |
| 14 | pattern = "*", |
| 15 | command = [[%s/\s\+$//e]], |
| 16 | }) |
| 17 | |
| 18 | -- spell check markdown and tex files |
| 19 | vim.cmd([[ |
| 20 | augroup spellCheck |
| 21 | autocmd! |
| 22 | autocmd Filetype plaintext setlocal spell |
| 23 | autocmd FileType markdown setlocal spell |
| 24 | autocmd BufRead,BufNewFile *.md setlocal spell |
| 25 | autocmd BufRead,BufNewFile *.rmd setlocal spell |
| 26 | autocmd BufRead,BufNewFile *.Rmd setlocal spell |
| 27 | autocmd BufRead,BufNewFile *.tex setlocal spell |
| 28 | augroup END |
| 29 | ]]) |
| 30 | |
| 31 | local build_commands = { |
| 32 | c = "!g++ -std=c++17 -o %:p:r.o %", |
| 33 | cpp = "!g++ -std=c++17 -Wall -O2 -o %:p:r.o %", |
| 34 | rust = "!cargo build --release", |
| 35 | go = "!go build", |
| 36 | -- tex = "pdflatex %", |
| 37 | tex = "VimtexCompile", |
| 38 | javascript = "", |
| 39 | java = "!jrun %", |
| 40 | } |
| 41 | |
| 42 | local debug_build_commands = { |
| 43 | c = "!g++ -std=c++17 -g -o %:p:r.o %", |
| 44 | cpp = "!g++ -std=c++17 -g -o %:p:r.o %", |
| 45 | rust = "!cargo build", |
| 46 | go = "!go build", |
| 47 | } |
| 48 | |
| 49 | local run_commands = { |
| 50 | c = "%:p:r.o", |
| 51 | cpp = "%:p:r.o", |
| 52 | rust = "cargo run --release", |
| 53 | go = "go run .", |
| 54 | tex = "", |
| 55 | javascript = "node %", |
| 56 | python = "python3 %", |
| 57 | } |
| 58 | |
| 59 | vim.api.nvim_create_user_command("Build", function() |
| 60 | local filetype = vim.bo.filetype |
| 61 | |
| 62 | for file, command in pairs(build_commands) do |
| 63 | if filetype == file then |
| 64 | vim.cmd(command) |
| 65 | break |
| 66 | end |
| 67 | end |
| 68 | end, {}) |
| 69 | |
| 70 | vim.api.nvim_create_user_command("DebugBuild", function() |
| 71 | local filetype = vim.bo.filetype |
| 72 | |
| 73 | for file, command in pairs(debug_build_commands) do |
| 74 | if filetype == file then |
| 75 | vim.cmd(command) |
| 76 | break |
| 77 | end |
| 78 | end |
| 79 | end, {}) |
| 80 | |
| 81 | vim.api.nvim_create_user_command("Run", function() |
| 82 | local filetype = vim.bo.filetype |
| 83 | |
| 84 | for file, command in pairs(run_commands) do |
| 85 | if filetype == file then |
| 86 | vim.cmd("sp") -- Vertical split |
| 87 | -- vim.cmd("vs") -- Horizontal split |
| 88 | vim.cmd("term " .. command) |
| 89 | vim.cmd("resize 20N") -- Comment this if horizontal |
| 90 | local keys = vim.api.nvim_replace_termcodes("i", true, false, true) |
| 91 | vim.api.nvim_feedkeys(keys, "n", false) |
| 92 | break |
| 93 | end |
| 94 | end |
| 95 | end, {}) |
| 96 | |
| 97 | vim.api.nvim_create_user_command("Ha", function() |
| 98 | vim.cmd([[Build]]) |
| 99 | vim.cmd([[Run]]) |
| 100 | end, {}) |
| 101 | |
| 102 | vim.api.nvim_create_user_command("Config", function() |
| 103 | vim.cmd([[cd ~/.config/nvim]]) |
| 104 | end, {}) |