A small Neovim plugin that provides a command for LSP renaming with immediate visual feedback thanks to Neovim's command preview feature.
inc_rename_demo.mp4
This plugin requires Neovim's nightly version (0.8).
Install using your favorite package manager and call the setup
function.
Here is an example using packer.nvim:
use {
"smjonas/inc-rename.nvim",
config = function()
require("inc_rename").setup()
end,
}
Simply type :IncRename <new_name>
while your cursor is on an LSP identifier.
You could also create a keymap that types out the command name for you so you only have to
enter the new name:
vim.keymap.set("n", "<leader>rn", ":IncRename ")
If you want to prefill the word under the cursor you can use the following:
vim.keymap.set("n", "<leader>rn", function()
return ":IncRename " .. vim.fn.expand("<cword>")
end, { expr = true })
🌸 dressing.nvim
support
If your are using dressing.nvim
or a similar plugin that uses a separate buffer for typing the new name,
you can call the rename
function that inc-rename
provides:
require("inc_rename").rename(opts | nil)
-- To prefill the word under the cursor, pass a default value:
require("inc_rename").rename({ default = vim.fn.expand("<cword>") })
This function calls vim.ui.input()
with the optional default input (which dressing.nvim
hijacks)
and manages the highlighting in a more manual way (that means highlighting does not rely on Neovim's
command-preview feature).
⚠️ Note that highlighting will not work with the builtinvim.ui.input
function because it is currently not possible to modify the buffer while the user is still typing in the command line.
The result should look something like this:
💡 Tip - try these
dressing.nvim
settings to position the input box above the cursor to not cover the word being renamed (thank you @RaafatTurki for the suggestion!):
require("dressing").setup {
input = {
override = function(conf)
conf.col = -1
conf.row = 0
return conf
end,
},
}
You can override the default settings by passing a Lua table to the setup
function.
The default options are:
require("inc_rename").setup {
cmd_name = "IncRename", -- the name of the command
hl_group = "Substitute", -- the highlight group used for highlighting the identifier's new name
multifile_preview = true, -- whether to enable the command preview across multiple buffers
show_message = true, -- whether to display a `Renamed m instances in n files` message after a rename operation
}