Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 34 additions & 4 deletions doc/oil.txt
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,36 @@ toggle_hidden() *oil.toggle_hidde


get_current_dir({bufnr}): nil|string *oil.get_current_dir*
Get the current directory
Get the current directory for the given oil buffer. Returns the directory
path as a string, or nil in the following cases:
- The buffer is not an oil buffer
- The buffer uses a non-files adapter (ssh, s3, trash)
- The current buffer changed (e.g. after opening a Telescope picker)

When calling this function inside a keymap that also opens another
plugin's window (e.g. Telescope), capture the directory BEFORE the
call that changes the buffer context:
>lua
["<leader>ff"] = {
function()
local dir = require("oil").get_current_dir()
if dir then
require("telescope.builtin").find_files({ cwd = dir })
end
end,
mode = "n",
nowait = true,
desc = "Find files in the current directory"
},
<

You can also pass a specific buffer number to avoid depending on the
current buffer:
>lua
local bufnr = vim.api.nvim_get_current_buf()
-- ... operations that may change the current buffer ...
local dir = require("oil").get_current_dir(bufnr)
<

Parameters:
{bufnr} `nil|integer`
Expand Down Expand Up @@ -519,9 +548,10 @@ The `keymaps` option in `oil.setup` allow you to create mappings using all the s
-- a table with the mapping as the first element.
["<leader>ff"] = {
function()
require("telescope.builtin").find_files({
cwd = require("oil").get_current_dir()
})
local dir = require("oil").get_current_dir()
if dir then
require("telescope.builtin").find_files({ cwd = dir })
end
end,
mode = "n",
nowait = true,
Expand Down
42 changes: 42 additions & 0 deletions doc/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Have a cool recipe to share? Open a pull request and add it to this doc!
- [Toggle file detail view](#toggle-file-detail-view)
- [Show CWD in the winbar](#show-cwd-in-the-winbar)
- [Hide gitignored files and show git tracked hidden files](#hide-gitignored-files-and-show-git-tracked-hidden-files)
- [Open Telescope file finder in the current oil directory](#open-telescope-file-finder-in-the-current-oil-directory)

<!-- /TOC -->

Expand Down Expand Up @@ -125,3 +126,44 @@ require("oil").setup({
},
})
```

## Open Telescope file finder in the current oil directory

When using `get_current_dir()` in a keymap that also opens another plugin's UI (like Telescope), always capture the directory in a local variable **before** the call that changes the buffer context. Passing `get_current_dir()` directly as an argument works because Lua evaluates arguments before calling the function, but any subsequent calls will see the new buffer.

```lua
require("oil").setup({
keymaps = {
["<leader>ff"] = {
desc = "Find files in the current directory",
callback = function()
local dir = require("oil").get_current_dir()
if not dir then
vim.notify("Could not get oil directory", vim.log.levels.WARN)
return
end
require("telescope.builtin").find_files({ cwd = dir })
end,
},
["<leader>fg"] = {
desc = "Live grep in the current directory",
callback = function()
local dir = require("oil").get_current_dir()
if not dir then
vim.notify("Could not get oil directory", vim.log.levels.WARN)
return
end
require("telescope.builtin").live_grep({ cwd = dir })
end,
},
},
})
```

If you need the directory after an operation that might change the current buffer, pass the buffer number explicitly:

```lua
local bufnr = vim.api.nvim_get_current_buf()
-- ... some operation that changes the current buffer ...
local dir = require("oil").get_current_dir(bufnr)
```
3 changes: 3 additions & 0 deletions lua/oil/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ M.posix_to_os_path = function(path)
if M.is_windows then
if vim.startswith(path, "/") then
local drive = path:match("^/(%a+)")
if not drive then
return path
end
local rem = path:sub(drive:len() + 2)
return string.format("%s:%s", drive, rem:gsub("/", "\\"))
else
Expand Down