diff options
| author | Josh Rahm <rahm@josher.dev> | 2026-10-18 15:54:35 -0600 |
|---|---|---|
| committer | Josh Rahm <rahm@josher.dev> | 2026-10-18 15:54:35 -0600 |
| commit | 457674c5b25b9f4be84f0f0ddf0cea7c1811174a (patch) | |
| tree | 69ce27a62afabd7a3d66830201c4d2d40a28e500 /lua/config/03-commands.lua | |
| parent | 715fd8b957d7c553f2df3070b430784517fc78ae (diff) | |
| download | config.vim-457674c5b25b9f4be84f0f0ddf0cea7c1811174a.tar.gz config.vim-457674c5b25b9f4be84f0f0ddf0cea7c1811174a.tar.bz2 config.vim-457674c5b25b9f4be84f0f0ddf0cea7c1811174a.zip | |
Redesign my vim config.
Now uses Lua primarily
Breaks apart the configuration into rc-service style configuration
files.
Deletes a ton of the cruft with old LSP settings.
Diffstat (limited to 'lua/config/03-commands.lua')
| -rw-r--r-- | lua/config/03-commands.lua | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/lua/config/03-commands.lua b/lua/config/03-commands.lua new file mode 100644 index 0000000..f8b0d47 --- /dev/null +++ b/lua/config/03-commands.lua @@ -0,0 +1,85 @@ +-- Commands and command-line abbreviations migrated from the original init.vim. +-- This file intentionally does not register autocommands or keymaps. + +local api = vim.api +local config_dir = vim.fn.stdpath('config') + +-- Edit the new configuration (original commands opened init.vim/local.vim). +api.nvim_create_user_command('Config', function() + vim.cmd.edit(vim.fn.fnameescape(config_dir .. '/init.lua')) +end, { desc = 'Edit Neovim init.lua' }) + +api.nvim_create_user_command('Configl', function() + vim.cmd.edit(vim.fn.fnameescape(config_dir .. '/lua/config/local/00-machine.lua')) +end, { desc = 'Edit machine-local configuration' }) + +-- :W is an easy-to-type alias for :w. Pass through arguments and !. +api.nvim_create_user_command('W', function(opts) + vim.cmd('write' .. (opts.bang and '!' or '') .. (opts.args ~= '' and ' ' .. opts.args or '')) +end, { bang = true, nargs = '*', complete = 'file', desc = 'Write buffer' }) + +-- Open a terminal in the specified directory, defaulting to the current +-- buffer's directory. The original function required a directory argument or +-- a buffer whose name was itself a directory; the default is more useful here. +_G.open_terminal_on_directory = function(dirname) + dirname = dirname or vim.fn.expand('%:p:h') + if vim.fn.isdirectory(dirname) ~= 1 then + vim.notify('Not a directory: ' .. dirname, vim.log.levels.WARN) + return + end + + local shell_cmd = string.format('cd %s && exec "$SHELL"', vim.fn.shellescape(dirname)) + vim.cmd('terminal sh -c ' .. vim.fn.shellescape(shell_cmd)) + vim.cmd.startinsert() + vim.cmd('silent! keepalt file ' .. vim.fn.fnameescape('TERM ' .. dirname)) +end + +api.nvim_create_user_command('TERM', function(opts) + _G.open_terminal_on_directory(opts.args ~= '' and vim.fn.expand(opts.args) or nil) +end, { nargs = '?', complete = 'dir', desc = 'Terminal in current file directory' }) + +-- Kept global because 01-mappings.lua invokes it with :lua run_format_code(). +_G.run_format_code = function() + local clients = vim.lsp.get_clients({ bufnr = 0, method = 'textDocument/formatting' }) + if #clients > 0 then + vim.lsp.buf.format({ async = false }) + elseif vim.fn.exists(':FormatCode') == 2 then + vim.cmd('FormatCode') + else + vim.notify('No LSP formatter or :FormatCode command available', vim.log.levels.WARN) + end +end + +-- Developer conveniences from the original config. +_G.remove_package = function(prefix) + for name in pairs(package.loaded) do + if name:sub(1, #prefix) == prefix then + package.loaded[name] = nil + end + end +end + +_G.reload_package = function(name) + _G.remove_package(name) + return require(name) +end + +-- Original command-line abbreviations. Limit them to a leading command-line +-- token, so they don't unexpectedly expand inside commands or search patterns. +local abbreviations = { + _config = config_dir, + _plugged = vim.fn.stdpath('data') .. '/plugged', -- legacy vim-plug path + _fm = vim.fn.stdpath('data') .. '/plugged/fieldmarshal.vim', -- legacy path + _home = vim.fn.stdpath('data'), + _projects = vim.fn.expand('~/Projects'), +} + +for word, replacement in pairs(abbreviations) do + vim.cmd('cnoreabbrev <expr> ' .. word .. ' (getcmdtype() ==# ":" && getcmdline() ==# "' .. word .. '") ? ' .. vim.fn.string(replacement) .. ' : "' .. word .. '"') +end + +vim.cmd([[cnoreabbrev <expr> _ (getcmdtype() ==# ':' && getcmdline() ==# '_') ? fnameescape(expand('%:h')) : '_']]) + +-- The old :UpdateInitVimHook belonged to vim-plug and is intentionally omitted. +-- Move the old InitVim/UseTerminal autocommands to a separate 04-autocmds.lua. + |