Skip to content

Instantly share code, notes, and snippets.

@dharmx
Created October 30, 2023 18:08
Show Gist options
  • Save dharmx/d814b60de374477615205cd5ab917305 to your computer and use it in GitHub Desktop.
Save dharmx/d814b60de374477615205cd5ab917305 to your computer and use it in GitHub Desktop.
Fuzzily change cwd based off of the lines in the current buffer. Works well with oil.nvim.
---@brief [[
---Get lines from a buffer > feed the lines into telescope\
---Choose from the entries
---See if the chosen entry is a directory
---If yes then run :chd <chosen> else do nothing
---@brief ]]
local finders = require("telescope.finders")
local pickers = require("telescope.pickers")
local config = require("telescope.config")
local actions = require("telescope.actions")
local actions_state = require("telescope.actions.state")
local make_entry = require("telescope.make_entry")
local Path = require("plenary.path")
local options = {} -- config
-- open oil window
-- require("cd"):find()
-- alternatively: vim.keymap.set("n", ";cd", function() require("cd"):find() end)
-- make selection
return pickers.new(options, {
prompt_title = "Choose",
finder = finders.new_table({
results = vim.api.nvim_buf_get_lines(0, 0, -1, false),
entry_maker = make_entry.gen_from_file(options)
}),
sorter = config.values.file_sorter(options),
attach_mappings = function(buffer)
actions.select_default:replace(function()
local selection = actions_state.get_selected_entry()[1]
if Path:new(selection):is_dir() then vim.cmd.chdir(selection) end
actions.close(buffer)
end)
return true
end,
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment