2021-12-03 01:43:52 +00:00
# Dressing.nvim
With the release of Neovim 0.6 we were given the start of extensible core UI
hooks ([vim.ui.select](https://github.com/neovim/neovim/pull/15771) and
[vim.ui.input ](https://github.com/neovim/neovim/pull/15959 )). They exist to
allow plugin authors to override them with improvements upon the default
behavior, so that's exactly what we're going to do.
It is a goal to match and not extend the core Neovim API. All options that core
respects will be respected, and we will not accept any custom parameters or
options in the functions. Customization will be done entirely using a separate
[configuration ](#configuration ) method.
2021-12-10 01:38:36 +00:00
- [Requirements ](#requirements )
- [Screenshots ](#screenshots )
- [Installation ](#installation )
- [Configuration ](#configuration )
2022-06-08 02:02:23 +00:00
- [Highlights ](#highlights )
2021-12-10 01:38:36 +00:00
- [Advanced configuration ](#advanced-configuration )
2022-05-27 02:45:23 +00:00
- [Notes for plugin authors ](#notes-for-plugin-authors )
2021-12-17 19:38:34 +00:00
- [Alternative and related projects ](#alternative-and-related-projects )
2021-12-03 03:55:58 +00:00
2021-12-03 01:43:52 +00:00
## Requirements
2023-06-16 04:46:36 +00:00
Neovim 0.8.0+ (for earlier versions, use the [nvim-0.7 ](https://github.com/stevearc/dressing.nvim/tree/nvim-0.7 ) or [nvim-0.5 ](https://github.com/stevearc/dressing.nvim/tree/nvim-0.5 ) branch)
2021-12-03 01:43:52 +00:00
2021-12-03 03:52:58 +00:00
## Screenshots
`vim.input` replacement (handling a LSP rename)
2021-12-03 03:57:14 +00:00
2021-12-10 01:38:36 +00:00
![Screenshot from 2021-12-09 17-36-16 ](https://user-images.githubusercontent.com/506791/145502533-3dc2f87d-95ea-422d-a318-12c0092f1bdf.png )
2021-12-03 03:52:58 +00:00
`vim.select` (telescope)
2021-12-03 03:57:14 +00:00
2021-12-03 03:52:58 +00:00
![Screenshot from 2021-12-02 19-46-01 ](https://user-images.githubusercontent.com/506791/144541916-4fa60c50-cadc-4f0f-b3c1-6307310e6e99.png )
`vim.select` (fzf)
2021-12-03 03:57:14 +00:00
2021-12-03 03:52:58 +00:00
![Screenshot from 2021-12-02 19-46-54 ](https://user-images.githubusercontent.com/506791/144541986-6081b4f8-b3b2-418d-9265-b9dabec2c4c4.png )
`vim.select` (nui)
2021-12-03 03:57:14 +00:00
2021-12-03 03:52:58 +00:00
![Screenshot from 2021-12-02 19-47-56 ](https://user-images.githubusercontent.com/506791/144542071-1aa66f81-b07c-492e-9884-fdafed1006df.png )
`vim.select` (built-in)
2021-12-05 01:18:51 +00:00
![Screenshot from 2021-12-04 17-14-32 ](https://user-images.githubusercontent.com/506791/144729527-ede0d7ba-a6e6-41e0-be5a-1a5f16d35b05.png )
2021-12-03 03:52:58 +00:00
2021-12-03 01:43:52 +00:00
## Installation
2021-12-10 01:38:36 +00:00
2021-12-03 01:43:52 +00:00
dressing.nvim supports all the usual plugin managers
2023-04-27 14:07:17 +00:00
< details >
< summary > lazy.nvim< / summary >
```lua
{
'stevearc/dressing.nvim',
opts = {},
}
```
< / details >
2021-12-03 01:43:52 +00:00
< details >
< summary > Packer< / summary >
2021-12-10 01:38:36 +00:00
```lua
require('packer').startup(function()
use {'stevearc/dressing.nvim'}
end)
```
2021-12-03 01:43:52 +00:00
< / details >
< details >
< summary > Paq< / summary >
2021-12-10 01:38:36 +00:00
```lua
require "paq" {
{'stevearc/dressing.nvim'};
}
```
2021-12-03 01:43:52 +00:00
< / details >
< details >
< summary > vim-plug< / summary >
2021-12-10 01:38:36 +00:00
```vim
Plug 'stevearc/dressing.nvim'
```
2021-12-03 01:43:52 +00:00
< / details >
< details >
< summary > dein< / summary >
2021-12-10 01:38:36 +00:00
```vim
call dein#add('stevearc/dressing.nvim')
```
2021-12-03 01:43:52 +00:00
< / details >
< details >
< summary > Pathogen< / summary >
2021-12-10 01:38:36 +00:00
```sh
git clone --depth=1 https://github.com/stevearc/dressing.nvim.git ~/.vim/bundle/
```
2021-12-03 01:43:52 +00:00
< / details >
< details >
< summary > Neovim native package< / summary >
2021-12-10 01:38:36 +00:00
```sh
git clone --depth=1 https://github.com/stevearc/dressing.nvim.git \
"${XDG_DATA_HOME:-$HOME/.local/share}"/nvim/site/pack/dressing.nvim/start/dressing.nvim
```
2021-12-03 01:43:52 +00:00
< / details >
## Configuration
2021-12-10 01:38:36 +00:00
2021-12-03 01:43:52 +00:00
If you're fine with the defaults, you're good to go after installation. If you
want to tweak, call this function:
```lua
require('dressing').setup({
input = {
2022-01-18 00:03:13 +00:00
-- Set to false to disable the vim.ui.input implementation
enabled = true,
2021-12-03 01:43:52 +00:00
-- Default prompt string
2022-03-31 15:40:47 +00:00
default_prompt = "Input:",
2021-12-03 01:43:52 +00:00
2022-03-23 05:10:45 +00:00
-- Can be 'left', 'right', or 'center'
2023-04-07 19:21:37 +00:00
title_pos = "left",
2022-03-23 05:10:45 +00:00
2021-12-06 03:26:33 +00:00
-- When true, < Esc > will close the modal
insert_only = true,
2022-07-21 05:49:20 +00:00
-- When true, input will start in insert mode.
start_in_insert = true,
2021-12-03 01:43:52 +00:00
-- These are passed to nvim_open_win
border = "rounded",
2022-03-02 17:32:22 +00:00
-- 'editor' and 'win' will default to being centered
relative = "cursor",
2021-12-03 01:43:52 +00:00
-- These can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
prefer_width = 40,
2022-03-02 17:32:22 +00:00
width = nil,
-- min_width and max_width can be a list of mixed types.
-- min_width = {20, 0.2} means "the greater of 20 columns or 20% of total"
max_width = { 140, 0.9 },
min_width = { 20, 0.2 },
2021-12-03 01:43:52 +00:00
2022-12-05 07:45:45 +00:00
buf_options = {},
win_options = {
-- Window transparency (0-100)
winblend = 10,
-- Disable line wrapping
wrap = false,
2023-04-04 17:56:48 +00:00
-- Indicator for when text exceeds window
list = true,
listchars = "precedes:…,extends:…",
-- Increase this for more context when text scrolls off the window
sidescrolloff = 0,
2022-12-05 07:45:45 +00:00
},
2021-12-10 01:13:00 +00:00
2022-07-29 04:27:24 +00:00
-- Set to `false` to disable
mappings = {
n = {
["< Esc > "] = "Close",
["< CR > "] = "Confirm",
},
i = {
["< C-c > "] = "Close",
["< CR > "] = "Confirm",
["< Up > "] = "HistoryPrev",
["< Down > "] = "HistoryNext",
},
},
2022-03-02 17:32:22 +00:00
override = function(conf)
-- This is the config that will be passed to nvim_open_win.
-- Change values here to customize the layout
return conf
end,
2021-12-03 01:43:52 +00:00
-- see :help dressing_get_config
get_config = nil,
},
select = {
2022-01-18 00:03:13 +00:00
-- Set to false to disable the vim.ui.select implementation
enabled = true,
2021-12-03 01:43:52 +00:00
-- Priority list of preferred vim.select implementations
2022-01-17 22:24:39 +00:00
backend = { "telescope", "fzf_lua", "fzf", "builtin", "nui" },
2021-12-03 01:43:52 +00:00
2022-05-30 02:24:52 +00:00
-- Trim trailing `:` from prompt
trim_prompt = true,
2021-12-03 01:43:52 +00:00
-- Options for telescope selector
2022-03-17 04:04:55 +00:00
-- These are passed into the telescope picker directly. Can be used like:
-- telescope = require('telescope.themes').get_ivy({...})
telescope = nil,
2021-12-03 01:43:52 +00:00
-- Options for fzf selector
fzf = {
2021-12-03 03:30:55 +00:00
window = {
width = 0.5,
height = 0.4,
},
2021-12-03 01:43:52 +00:00
},
2023-05-12 12:44:47 +00:00
-- Options for fzf-lua
fzf_lua = {
-- winopts = {
-- height = 0.5,
-- width = 0.5,
-- },
},
2021-12-03 01:43:52 +00:00
-- Options for nui Menu
nui = {
position = "50%",
size = nil,
relative = "editor",
border = {
style = "rounded",
},
2022-06-05 02:11:05 +00:00
buf_options = {
swapfile = false,
filetype = "DressingSelect",
},
win_options = {
winblend = 10,
},
2021-12-03 01:43:52 +00:00
max_width = 80,
max_height = 40,
2022-06-05 02:11:05 +00:00
min_width = 40,
min_height = 10,
2021-12-03 01:43:52 +00:00
},
-- Options for built-in selector
builtin = {
-- These are passed to nvim_open_win
border = "rounded",
2022-03-02 17:32:22 +00:00
-- 'editor' and 'win' will default to being centered
relative = "editor",
2021-12-03 01:43:52 +00:00
2022-12-05 07:45:45 +00:00
buf_options = {},
win_options = {
-- Window transparency (0-100)
winblend = 10,
2023-06-16 04:54:06 +00:00
cursorline = true,
cursorlineopt = "both",
2022-12-05 07:45:45 +00:00
},
2021-12-03 03:53:18 +00:00
2021-12-03 01:43:52 +00:00
-- These can be integers or a float between 0 and 1 (e.g. 0.4 for 40%)
2022-03-02 17:32:22 +00:00
-- the min_ and max_ options can be a list of mixed types.
-- max_width = {140, 0.8} means "the lesser of 140 columns or 80% of total"
2021-12-03 01:43:52 +00:00
width = nil,
2022-03-02 17:32:22 +00:00
max_width = { 140, 0.8 },
min_width = { 40, 0.2 },
2021-12-03 01:43:52 +00:00
height = nil,
max_height = 0.9,
2022-03-02 17:32:22 +00:00
min_height = { 10, 0.2 },
2022-07-29 04:27:24 +00:00
-- Set to `false` to disable
mappings = {
["< Esc > "] = "Close",
["< C-c > "] = "Close",
["< CR > "] = "Confirm",
},
2022-03-02 17:32:22 +00:00
override = function(conf)
-- This is the config that will be passed to nvim_open_win.
-- Change values here to customize the layout
return conf
end,
2021-12-03 01:43:52 +00:00
},
2021-12-17 19:21:08 +00:00
-- Used to override format_item. See :help dressing-format
format_item_override = {},
2021-12-03 01:43:52 +00:00
-- see :help dressing_get_config
get_config = nil,
},
})
```
2022-06-08 02:02:23 +00:00
## Highlights
A common way to adjust the highlighting of just the dressing windows is by
2023-06-23 17:43:21 +00:00
providing a `winhighlight` option in the config. See `:help winhighlight`
for more details. Example:
```lua
require('dressing').setup({
input = {
2023-08-08 22:22:24 +00:00
win_options = {
winhighlight = 'NormalFloat:DiagnosticError'
}
2023-06-23 17:43:21 +00:00
}
})
```
2022-06-08 02:02:23 +00:00
2021-12-03 03:55:58 +00:00
## Advanced configuration
2021-12-18 20:47:51 +00:00
2021-12-03 01:43:52 +00:00
For each of the `input` and `select` configs, there is an option
`get_config` . This can be a function that accepts the `opts` parameter that
is passed in to `vim.select` or `vim.input` . It must return either `nil` (to
no-op) or config values to use in place of the global config values for that
module.
For example, if you want to use a specific configuration for code actions:
2021-12-18 20:47:51 +00:00
2021-12-03 01:43:52 +00:00
```lua
require('dressing').setup({
select = {
get_config = function(opts)
if opts.kind == 'codeaction' then
return {
backend = 'nui',
nui = {
relative = 'cursor',
max_width = 40,
}
}
end
end
}
})
```
2021-12-18 20:47:51 +00:00
2022-05-27 02:45:23 +00:00
## Notes for plugin authors
TL;DR: you can customize the telescope `vim.ui.select` implementation by passing `telescope` into `opts` .
The `vim.ui` hooks are a great boon for us because we can now assume that users
will have a reasonable UI available for simple input operations. We no longer
have to build separate implementations for each of fzf, telescope, ctrlp, etc.
The tradeoff is that `vim.ui.select` is less customizable than any of these
options, so if you wanted to have a preview window (like telescope supports), it
is no longer an option.
My solution to this is extending the `opts` that are passed to `vim.ui.select` .
You can add a `telescope` field that will be passed directly into the picker,
allowing you to customize any part of the UI. If a user has both dressing and
telescope installed, they will get your custom picker UI. If either of those
are not true, the selection UI will gracefully degrade to whatever the user has
configured for `vim.ui.select` .
An example of usage:
```lua
vim.ui.select({'apple', 'banana', 'mango'}, {
prompt = "Title",
telescope = require("telescope.themes").get_cursor(),
}, function(selected) end)
```
For now this is available only for the telescope backend, but feel free to request additions.
2021-12-17 19:38:34 +00:00
## Alternative and related projects
- [telescope-ui-select ](https://github.com/nvim-telescope/telescope-ui-select.nvim ) - provides a `vim.ui.select` implementation for telescope
2023-04-12 12:26:42 +00:00
- [fzf-lua ](https://github.com/ibhagwan/fzf-lua/blob/061a4df40f5238782fdd7b380fe55650fadd9384/README.md?plain=1#L259-L264 ) - provides a `vim.ui.select` implementation for fzf
2021-12-17 19:38:34 +00:00
- [nvim-fzy ](https://github.com/mfussenegger/nvim-fzy ) - fzf alternative that also provides a `vim.ui.select` implementation ([#13](https://github.com/mfussenegger/nvim-fzy/pull/13))
2021-12-18 20:47:51 +00:00
- [guihua.lua ](https://github.com/ray-x/guihua.lua ) - multipurpose GUI library that provides `vim.ui.select` and `vim.ui.input` implementations
2021-12-10 01:38:36 +00:00
- [nvim-notify ](https://github.com/rcarriga/nvim-notify ) - doing pretty much the
same thing but for `vim.notify`
- [nui.nvim ](https://github.com/MunifTanjim/nui.nvim ) - provides common UI
2023-04-12 12:50:14 +00:00
components for plugin authors. [The wiki ](https://github.com/MunifTanjim/nui.nvim/wiki/vim.ui ) has examples of how to build your own `vim.ui` interfaces.