Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

flashios09/root.nvim

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

root.nvim

A tiny Lua utility module for Neovim to get the project root path, heavily inspired(stolen 😅) from the only and the one Folke.

demo-root-nvim.mp4

Installation

Using lazy.nvim:

return {
  "flashios09/root.nvim",
  lazy = true,
}

Usage

local root = require("root")
print(root.get()) -- `~/.config/nvim/`
vim.keymap.set("n", "<leader>e", function()
  require("snacks").explorer({ cwd = require("root").get() })
end, { desc = "Explorer Snacks (Root dir)" })
vim.keymap.set("n", "<leader><space>", function()
  require("snacks").picker.files({ cwd = require("root").get() })
end, { desc = "Find Files (Root Dir)" })
vim.keymap.set("n", "<leader>e", function()
  require("neo-tree.command").execute({ toggle = true, require("root").get() })
end, { desc = "Explorer NeoTree (Root Dir)" })
vim.keymap.set("n", "<leader><space>", function()
  require("telescope.builtin").find_files({ cwd = require("root").get() })
end, { desc = "Find Files (Root Dir)" })
vim.keymap.set("n", "<leader><space>", function()
  require("fzf-lua").files({ cwd = require("root").get() })
end, { desc = "Find Files (Root Dir)" })
return {
  "flashios09/barbecue.nvim",
  name = "barbecue",
  version = "*",
  dependencies = {
    "SmiteshP/nvim-navic",
    "flashios09/root.nvim",
    "nvim-tree/nvim-web-devicons",
  },
  config = function()
    require("barbecue").setup({
     -- your custom config here
     -- ..
     -- adding the project name to the barbecue lead section
      lead_custom_section = function()
        local project_name = vim.fn.fnamemodify(require("root").get(), ":t")
        local project_section = {
          { "" .. project_name .. " ", "BarbecueProject" },
          { "", "BarbecueProjectSeparator" },
          { " ", "BarbecueNormal" },
        }

        return project_section
      end,
    })
  end,
}

API

Return the buffer root directory.

Based on:

  • lsp workspace folders
  • lsp root_dir
  • root pattern of filename of the current buffer
  • root pattern of cwd
Example:
root.get() -- `"/Users/flashios09/.config/nvim/"`
root.get({ buffer = 1 }) -- `"/Users/flashios09/.config/nvim/"`

Get the git root path if exists.

Example:
root.git() -- `"/Users/flashios09/.config/nvim/"`

Get the buffer path.

Example:
root.bufpath(1) -- `"/Users/flashios09/.config/nvim/"`

Normalize the buffer path.

  • Replace the ~ with the users home dir.
  • Normalize the windows path(the directory separator): replace the \ with /.
Example:
root.norm("~/.config/nvim/") -- `"/Users/flashios09/.config/nvim/"`
root.norm("C:\.config\nvim\") -- `"C:/.config/nvim/"

Get all the available root spec(s).

Example:
root.detect()
-- output:
{
  {
    paths = { "/Users/flashios09/.config/nvim" },
    spec = "lsp"
  }, {
    paths = { "/Users/flashios09/.config/nvim" },
    spec = { ".git", "lua" }
  }, {
    paths = { "/Users/flashios09/.config/nvim" },
    spec = "cwd"
  }
}

About

Get nvim project root path

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

Morty Proxy This is a proxified and sanitized view of the page, visit original site.