-- 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 ' .. word .. ' (getcmdtype() ==# ":" && getcmdline() ==# "' .. word .. '") ? ' .. vim.fn.string(replacement) .. ' : "' .. word .. '"') end vim.cmd([[cnoreabbrev _ (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.