Skip to content

wsdjeg/rooter.nvim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

53 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

rooter.nvim

rooter.nvim changes the working directory to the project root when you open a file. It is inspired by vim-rooter.

This plugin also provides a telescope extension to fuzzy find recently opened project.

GitHub License GitHub Issues or Pull Requests GitHub commit activity GitHub Release luarocks

✨ Features

  • Automatic project root detection
  • Caching for better performance
  • Outermost directory support
  • Flexible behavior for non-project files
  • Optional logging support
  • Callback APIs
  • Command-line interface
  • Telescope integration
  • Picker.nvim integration

📦 Installation

using nvim-plug

require('plug').add({
  {
    'wsdjeg/rooter.nvim',
    config = function()
      require('rooter').setup({
        root_pattern = { '.git/' },
      })
    end,
  }
})

🔧 Configuration

require('rooter').setup({
  root_patterns = { '.git/' },
  outermost = true,
  enable_cache = true,
  project_non_root = '',  -- this can be '', 'home' or 'current'
  enable_logger = true,   -- enable runtime log via logger.nvim
  command = 'lcd',        -- cd, tcd or lcd
})

⚙️ Basic Usage

Telescope extension

This plugin also provides a telescope extension:

:Telescope project

Image

Picker.nvim extension

picker project

:Picker project

key bindings for picker project:

key binding description
<C-f> file project files
<C-d> delete project
<C-s> search text in project, require flygrep.nvim

Callback function

To add new callback function when project changed. You can use rooter.reg_callback, for example:

update c code runner based on project .clang file.

local c_runner = {
    exe = 'gcc',
    targetopt = '-o',
    usestdin = true,
    opt = { '-std=c11', '-xc', '-' },
}
require('code-runner').setup({
    runners = {
        c = { c_runner, '#TEMP#' },
    },
})
vim.keymap.set(
    'n',
    '<leader>lr',
    '<cmd>lua require("code-runner").open()<cr>',
    { silent = true }
)

-- make sure rooter.nvim plugin is loaded before code-runner

local function update_clang_flag()
    if vim.fn.filereadable('.clang') == 1 then
        local flags = vim.fn.readfile('.clang')
        local opt = { '-std=c11' }
        for _, v in ipairs(flags) do
            table.insert(opt, v)
        end
        table.insert(opt, '-xc')
        table.insert(opt, '-')
        c_runner.opt = opt
    end
end

require('rooter').reg_callback(update_clang_flag)

Commands

This plugin also provides a user command :Rooter.

  1. switch to project root manually.

:Rooter

  1. clear cached projects.

:Rooter clear

  1. Delete all buffers for the specified project.

:Rooter kill project_name1 project_name2

🐛 Debug

You can enable logger and install logger.nvim to debug this plugin:

require('plug').add({
  {
    'wsdjeg/rooter.nvim',
    config = function()
      require('rooter').setup({
        root_pattern = { '.git/' },
        enable_logger = true,
      })
    end,
    depends = {
      {
        'wsdjeg/logger.nvim',
        config = function()
          vim.keymap.set(
            'n',
            '<leader>hL',
            '<cmd>lua require("logger").viewRuntimeLog()<cr>',
            { silent = true }
          )
        end,
      },
    },
  },
})

and the runtime log of rooter is:

[   rooter ] [23:22:50:576] [ Info  ] start to find root for: D:/wsdjeg/rooter.nvim/lua/rooter/init.lua
[   rooter ] [23:22:50:576] [ Info  ]         (.git/):D:/wsdjeg/rooter.nvim/
[   rooter ] [23:22:50:576] [ Info  ] switch to project:[rooter.nvim]
[   rooter ] [23:22:50:576] [ Info  ]        rootdir is:D:/wsdjeg/rooter.nvim/

💬 Feedback

If you encounter any bugs or have suggestions, please file an issue in the issue tracker

🙏 Credits

📣 Self-Promotion

Like this plugin? Star the repository on GitHub.

Love this plugin? Follow me on GitHub.

📄 License

Licensed under GPL-3.0.

About

Changes Neovim working directory to project root.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Languages