diff --git a/init.lua b/init.lua index ae264c5..6fabec4 100644 --- a/init.lua +++ b/init.lua @@ -32,6 +32,9 @@ require 'options' -- [[ Basic Keymaps ]] require 'keymaps' +-- [[ User Commands ]] +require 'usercmds' + -- [[ Setting autocmd's ]] require 'autocmd' diff --git a/lua/autocmd.lua b/lua/autocmd.lua index cc00167..d7dd70d 100644 --- a/lua/autocmd.lua +++ b/lua/autocmd.lua @@ -2,11 +2,6 @@ -- See `:help autocmd` -- See `:help lua-guide-autocommands` --- These should be self-explanatory -vim.api.nvim_create_user_command('Cd', 'cd %:h', { desc = 'Change directory to the current file' }) -vim.api.nvim_create_user_command('Py', '!python %', { desc = 'Execute python file' }) -vim.api.nvim_create_user_command('Trim', 'lua MiniTrailspace.trim()', { desc = 'Trim trailing whitespace' }) - -- Instantly move help window to the right -- This is almost certainly a hacky way to do this) vim.api.nvim_create_autocmd('FileType', { @@ -25,66 +20,4 @@ vim.api.nvim_create_autocmd('TextYankPost', { end, }) -vim.api.nvim_create_user_command('CDToGitRoot', function() - local handle = io.popen 'git rev-parse --show-toplevel 2>/dev/null' - local git_root - - if handle then - git_root = handle:read('*a'):gsub('\n', '') - handle:close() - end - - if git_root == '' then - print 'Not in a Git repository' - else - vim.cmd('cd ' .. git_root) - print('Changed directory to Git root: ' .. git_root) - end -end, { desc = 'Change directory to the current Git repository root' }) - -vim.api.nvim_create_user_command('WordCount', function() - local buf = vim.api.nvim_get_current_buf() - local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false) - local words = 0 - for _, line in ipairs(lines) do - for _ in line:gmatch '%S+' do - words = words + 1 - end - end - print('Word count: ' .. words) -end, { desc = 'Count words in current buffer' }) - -vim.api.nvim_create_user_command('HeaderGuard', function() - local filename = vim.fn.expand '%:t' -- filename.ext - local stem = vim.fn.expand '%:t:r' -- filename without extension - local ext = vim.fn.expand '%:e' -- file extension - - if ext ~= 'h' then - print 'HeaderGuard: Not a .h file!' - return - end - - local guard = string.upper(stem) .. '_H' - - -- Insert at top - vim.api.nvim_buf_set_lines(0, 0, 0, false, { - '#ifndef ' .. guard, - '#define ' .. guard, - '', - }) - - -- Insert at bottom - local last_line = vim.api.nvim_buf_line_count(0) - vim.api.nvim_buf_set_lines(0, last_line, last_line, false, { - '', - '#endif // ' .. guard, - }) - - print('Header guard inserted: ' .. guard) -end, { desc = 'Insert header guard for current .h file' }) - -vim.api.nvim_create_user_command('ExpandVars', function() - vim.cmd [[silent! %s/\$\([A-Za-z_][A-Za-z0-9_]*\)/\${\1}/g]] -end, { desc = 'Expand all bash vars' }) - -- vim: ts=2 sts=2 sw=2 et diff --git a/lua/usercmds.lua b/lua/usercmds.lua new file mode 100644 index 0000000..0cfc1d9 --- /dev/null +++ b/lua/usercmds.lua @@ -0,0 +1,73 @@ +-- These should be self-explanatory +vim.api.nvim_create_user_command('Cd', 'cd %:h', { desc = 'Change directory to the current file' }) +vim.api.nvim_create_user_command('Py', '!python %', { desc = 'Execute python file' }) +vim.api.nvim_create_user_command('Bash', '!bash %', { desc = 'Execute bash file' }) +vim.api.nvim_create_user_command('Trim', 'lua MiniTrailspace.trim()', { desc = 'Trim trailing whitespace' }) + +-- Set working directory to git root directory +vim.api.nvim_create_user_command('CDToGitRoot', function() + local handle = io.popen 'git rev-parse --show-toplevel 2>/dev/null' + local git_root + + if handle then + git_root = handle:read('*a'):gsub('\n', '') + handle:close() + end + + if git_root == '' then + print 'Not in a Git repository' + else + vim.cmd('cd ' .. git_root) + print('Changed directory to Git root: ' .. git_root) + end +end, { desc = 'Change directory to the current Git repository root' }) + +-- Count words in the current buffer +vim.api.nvim_create_user_command('WordCount', function() + local buf = vim.api.nvim_get_current_buf() + local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false) + local words = 0 + for _, line in ipairs(lines) do + for _ in line:gmatch '%S+' do + words = words + 1 + end + end + print('Word count: ' .. words) +end, { desc = 'Count words in current buffer' }) + +-- Generate header guards for .h files +vim.api.nvim_create_user_command('HeaderGuard', function() + local filename = vim.fn.expand '%:t' -- filename.ext + local stem = vim.fn.expand '%:t:r' -- filename without extension + local ext = vim.fn.expand '%:e' -- file extension + + if ext ~= 'h' then + print 'HeaderGuard: Not a .h file!' + return + end + + local guard = string.upper(stem) .. '_H' + + -- Insert at top + vim.api.nvim_buf_set_lines(0, 0, 0, false, { + '#ifndef ' .. guard, + '#define ' .. guard, + '', + }) + + -- Insert at bottom + local last_line = vim.api.nvim_buf_line_count(0) + vim.api.nvim_buf_set_lines(0, last_line, last_line, false, { + '', + '#endif // ' .. guard, + }) + + print('Header guard inserted: ' .. guard) +end, { desc = 'Insert header guard for current .h file' }) + +-- For bash: sed $VAR to ${VAR}, essentially +vim.api.nvim_create_user_command('ExpandVars', function() + vim.cmd [[silent! %s/\$\([A-Za-z_][A-Za-z0-9_]*\)/\${\1}/g]] +end, { desc = 'Expand all bash vars' }) + +-- vim: ts=2 sts=2 sw=2 et