A high performance plugin to change your working directory to the project root when you open a file. Basically a minimal version of vim-rooter written in lua.
Using vim-plug
Plug 'notjedi/nvim-rooter.lua'
Using packer
-- NOTE: avoid lazy loading as the autocmds may not be caught by nvim-rooter.lua.
use {
'notjedi/nvim-rooter.lua',
config = function() require'nvim-rooter'.setup() end
}
One line setup. This will create an autocmd
for FileType *
to change to root directory everytime
you switch buffers.
lua require'nvim-rooter'.setup()
- For nvim-tree support/integration, add the following to you nvim-tree config:
require("nvim-tree").setup({
update_cwd = true,
update_focused_file = {
enable = true,
update_cwd = true
},
})
The following snippet is the default configuration. Also note that you can use globs.
require('nvim-rooter').setup {
rooter_patterns = { '.git', '.hg', '.svn' },
trigger_patterns = { '*' },
manual = false,
fallback_to_parent = false,
cd_scope = "global",
}
Specify the root is a certain directory, prefix it with =
lua require('nvim-rooter').setup { rooter_patterns = { '=src' } }
To invoke :Rooter
manually and not have it run everytime you switch/open buffers, you can pass the
following argument while setting up nvim-rooter.
lua require('nvim-rooter').setup { manual = true }
By default all files trigger :Rooter
but you can configure this by passing the trigger_patterns
argument while you call setup()
lua require('nvim-rooter').setup { trigger_patterns = { '*.py', '*.lua' } }
Exclude certain filetypes using the config below. Note that filetypes is must be passed as arguments
and not file extensions. For example: python
instead of py
. Use vim.bo.filetype
to get the
filetype of the current buffer.
lua require('nvim-rooter').setup { exclude_filetypes = { 'python', 'lua' } }
Change to the directory of the file when it has no root
lua require('nvim-rooter').setup { fallback_to_parent = true }
Change the scope of working directory change. The supported values are:
global
(default): Changes global cwd withvim.api.nvim_set_current_dir()
window
: Changes window-local cwd with:lcd
tabpage
: Changes tabpage-local cwd with:tcd
smart
: If local cwd is already set, changes local cwd. If not, changes global cwd. Check:h chdir()
for more detail.
lua require('nvim-rooter').setup { cd_scope = "window" }
vim-rooter | nvim-rooter.lua | rooter.nvim | |
---|---|---|---|
loading time | 0.185 ms | 0.083 ms | 3.206 ms |
:RooterToggle |
❌ | ✔️ | ❌ |
trigger :Rooter on filename match |
✔️ | ✔️ | ❌ |
exclude filetypes | ✔️ | ✔️ | ❌ |
resolve symlinks | ✔️ | ✔️ | ❌ |
emit autocmd |
✔️ | ❌ | ❌ |
= prefix |
✔️ | ✔️ | ❌ |
other prefixes | ✔️ | ❌ | ❌ |
support files in HDD | ✔️ | ✔️ | ❌ |
Note: what :RooterToggle
does is, it switches between root and current directory. whereas in
vim-rooter
, it toggles between automatic and manual mode.
Note: the loading time was profiled using the --startuptime
option.
- Change to project root directory using patterns
- cmd to toggle - something like
:RooterToggle
(not the same as vim-rooter) - Support
nvim-tree
- Resolve symlinks
- support something like
=src
- cache results
- use
setup()
- cleanup code
- manual trigger
- when rootless
- cleanup code
- Emit autocmd?
- write tests
- Extended support for patterns like
vim-rooter
? - Support which directories/files trigger
:Rooter
?
This was originally meant to be a PR to rooter.nvim, but then i ended up rewriting everything, cause I like the lua language and wanted to do something with it. Also there was way too many different and unrelated changes to PR, so I ended up forking it.
This plugin was written on stream. I don't have previous experience with lua (except for the nvim config), just skimmed through the first 2 references in 20 minutes and started writing this plugin, and this is my first lua project or even my first vim plugin so please don't judge what i was doing on stream.