A Neovim plugin to quickly switch the word under the cursor to its opposite word.
For example, if the cursor is on enable
and you press <Leader>i
it will
switch to disable
and vice versa.
Warning
This plugin is based on my personal needs. Work in progress. 🚀
Other similar plugins are:
- Searches for the configured words and opposite words in the current line under the cursor.
- Switches the found word to its opposite word.
- The found word can also be a part of another word.
- e.g. enabled with the cursor in
enable
becomes disabled.
- e.g. enabled with the cursor in
- Adapts the capitalization of the replaced word.
- e.g.
true
,True
,TRUE
->false
,False
,FALSE
.
- e.g.
- The opposite words can be file type specific.
- Optionally notifies when the word is found or not.
- If several results are found, the user is asked which result to switch to.
- Neovim >= 0.10
return {
'tigion/nvim-opposites',
-- event = { 'BufReadPost', 'BufNewFile' },
keys = {
{ '<Leader>i', function() require('opposites').switch() end, desc = 'Switch to opposite word' },
},
---@type opposites.Config
opts = {},
}
Call require('opposites').switch()
to switch to the opposite word under the
cursor.
To add more words to the opposites list, add them to the opposites
or
opposites_by_ft
table in the opposites.Config
table.
Note
Redundant opposite words are removed automatically.
If use_default_opposites
and use_default_opposites_by_ft
is set to false
,
only the user defined words will be used.
opts = {
opposites = {
['angel'] = 'devil', -- Adds a new default.
['yes'] = 'ja', -- Replaces the default `['yes'] = 'no'`.
['min'] = nil, -- Removes a default.
},
opposites_by_ft = {
['lua'] = {
['=='] = '~=', -- Replaces the default `['=='] = '!='` for lua files.
},
['sql'] = {
['AND'] = 'OR', -- Adds a new for SQL files.
},
},
}
Tip
It doesn't have to be opposites words that are exchanged.
Flexible word recognition can be used to avoid having to configure every variant of capitalization. Activated by default. This means that variants with capital letters are also found for lower-case words and the replaced opposite word adapts the capitalization.
Rules:
- If the word is uppercase, the mask is upper case.
- If the word is lowercase, the mask is lower case.
- If the word is mixed case, the mask is a string to represent the case. Longer words are masked at the end with lower case letters.
Deactivate this behavior by setting use_case_sensitive_mask = false
.
Important
If a configured word or his opposite word contains capital letters, then for this words no mask is used.
Example with ['enable'] = 'disable'
:
- found:
enable
,Enable
,EnAbLe
andENABLE
- replaced with:
disable
,Disable
,diSAble
andDISABLE
Example with ['enable'] = 'Disable'
:
- found:
enable
- replaced with:
Disable
The default options are:
---@class opposites.Config -- opposites.config.config
---@field max_line_length? integer The maximum line length to search.
---@field use_case_sensitive_mask? boolean Whether to use a case sensitive mask.
---@field use_default_opposites? boolean Whether to use the default opposites.
---@field use_default_opposites_by_ft? boolean Whether to use the default opposites.
---@field opposites? opposites.Config.opposites The words with their opposite.
---@field opposites_by_ft? opposites.Config.opposites_by_ft The file type specific words with their opposite.
---@field notify? opposites.Config.notify The notifications to show.
---@alias opposites.Config.opposites table<string, string>
---@alias opposites.Config.opposites_by_ft table<string, opposites.Config.opposites>
---@class opposites.Config.notify
---@field found? boolean Whether to notify when a word is found.
---@field not_found? boolean Whether to notify when no word is found.
---@type opposites.Config
{
max_line_length = 1000,
use_case_sensitive_mask = true,
use_default_opposites = true,
use_default_opposites_by_ft = true,
opposites = {
['enable'] = 'disable',
['true'] = 'false',
['yes'] = 'no',
['on'] = 'off',
['left'] = 'right',
['up'] = 'down',
['min'] = 'max',
['=='] = '!=',
['<='] = '>=',
['<'] = '>',
},
opposites_by_ft = {
['lua'] = {
['=='] = '~=',
},
['sql'] = {
['asc'] = 'desc',
},
},
notify = {
found = false,
not_found = true,
},
}
For other plugin manager, call the setup function
require('opposites').setup({ ... })
directly.
- Limit and check the user configuration.
- Use
vim.ui.select
instead ofvim.fn.inputlist
. - Refactoring of the first quickly written code.
- Adapt the capitalization of the words to reduce words like
true
,True
,tRUe
andTRUE
. - Add file type specific opposites.