doc: clarify get_current_dir nil return and add Telescope recipe

Expand get_current_dir docs to explain when nil is returned (non-oil
buffer, non-files adapter, buffer context change). Add inline code
examples showing the capture-before-call pattern for Telescope keymaps.

Update the existing Telescope keymap example in oil.txt to guard against
nil. Add a dedicated Telescope recipe in recipes.md with find_files and
live_grep examples.

Add a defensive nil guard in posix_to_os_path for edge-case Windows
paths where the drive letter match fails.

Closes #682
This commit is contained in:
kubasync-clanker[bot] 2026-02-18 20:03:52 +00:00
parent f55b25e493
commit 1580de4c66
3 changed files with 79 additions and 4 deletions

View file

@ -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`
@ -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,