Skip to content

Commit d9ce5d0

Browse files
authored
feat: add change_cwd and goto_home_dir action (#40)
1 parent d1032b4 commit d9ce5d0

File tree

3 files changed

+49
-13
lines changed

3 files changed

+49
-13
lines changed

README.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,21 @@ vim.api.nvim_set_keymap(
7070

7171
## Mappings
7272

73-
| Insert / Normal | Action |
74-
|------------------|------------------------------------------------------|
75-
| `<C-f>/f` | Toggle between file and folder browser |
76-
| `<C-y>/y` | Copy (multi-selected) files or folders to cwd |
77-
| `<C-d>/dd` | Delete (multi-selected) files or folders |
78-
| `<C-r>/r` | Rename (multi-selected) files |
79-
| `<C-e>/e` | Add File/Folder at cwd; trailing `/` creates folder |
80-
| `--/m` | Move multi-selected files to cwd |
81-
| `<C-h>/h` | Toggle hidden files |
82-
| `<C-o>/o` | Open file with default system application |
83-
| `<C-g>/g` | Go to parent directory |
84-
| `<C-w>/w` | Go to current working directory |
85-
| `<A-e>/--` | Toggle all entires ignoring `./` and `../` |
73+
| Insert / Normal | Action |
74+
|------------------|-------------------------------------------------------|
75+
| `<C-f>/f` | Toggle between file and folder browser |
76+
| `<C-y>/y` | Copy (multi-selected) files or folders to cwd |
77+
| `<C-d>/dd` | Delete (multi-selected) files or folders |
78+
| `<C-r>/r` | Rename (multi-selected) files |
79+
| `<C-e>/e` | Add File/Folder at cwd; trailing `/` creates folder |
80+
| `--/m` | Move multi-selected files to cwd |
81+
| `<C-h>/h` | Toggle hidden files |
82+
| `<C-o>/o` | Open file with default system application |
83+
| `<C-g>/g` | Go to parent directory |
84+
| `<C-s>/s` | Go to home directory |
85+
| `<C-t>/t` | Change nvim's cwd to selected folder or file (parent) |
86+
| `<C-w>/w` | Go to current working directory |
87+
| `<A-e>/--` | Toggle all entires ignoring `./` and `../` |
8688

8789
Copying and moving files typically requires you to multi-select your files and folders and then moving to the target directory to copy and move the selections to (cf. [demo](#demo)).
8890

lua/telescope/_extensions/file_browser.lua

+4
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ local pconf = {
7070
["<C-o>"] = fb_actions.open_file,
7171
["<C-r>"] = fb_actions.rename_file,
7272
["<C-w>"] = fb_actions.goto_cwd,
73+
["<C-s>"] = fb_actions.goto_home_dir,
74+
["<C-t>"] = fb_actions.change_cwd,
7375
["<C-y>"] = fb_actions.copy_file,
7476
},
7577
["n"] = {
@@ -82,6 +84,8 @@ local pconf = {
8284
["o"] = fb_actions.open_file,
8385
["r"] = fb_actions.rename_file,
8486
["w"] = fb_actions.goto_cwd,
87+
["s"] = fb_actions.goto_home_dir,
88+
["t"] = fb_actions.change_cwd,
8589
["y"] = fb_actions.copy_file,
8690
},
8791
},

lua/telescope/_extensions/file_browser/actions.lua

+30
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,36 @@ fb_actions.goto_cwd = function(prompt_bufnr)
417417
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
418418
end
419419

420+
--- Change working directory of nvim to the selected file/folder in |builtin.file_browser|.
421+
---@param prompt_bufnr number: The prompt bufnr
422+
fb_actions.change_cwd = function(prompt_bufnr)
423+
local current_picker = action_state.get_current_picker(prompt_bufnr)
424+
local finder = current_picker.finder
425+
local entry_path = action_state.get_selected_entry().Path
426+
finder.path = entry_path:is_dir() and entry_path:absolute() or entry_path:parent():absolute()
427+
finder.cwd = finder.path
428+
vim.cmd("cd " .. finder.path)
429+
if current_picker.results_border then
430+
local title = Path:new(finder.path):make_relative(finder.path) .. os_sep
431+
current_picker.results_border:change_title(title)
432+
end
433+
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
434+
print "[telescope] Changed nvim's current working directory"
435+
end
436+
437+
--- Goto home directory in |builtin.file_browser|.
438+
---@param prompt_bufnr number: The prompt bufnr
439+
fb_actions.goto_home_dir = function(prompt_bufnr)
440+
local current_picker = action_state.get_current_picker(prompt_bufnr)
441+
local finder = current_picker.finder
442+
finder.path = vim.loop.os_homedir()
443+
if current_picker.results_border then
444+
local new_title = finder.files and Path:new(finder.path):make_relative(vim.loop.cwd()) .. os_sep or finder.cwd
445+
current_picker.results_border:change_title(new_title)
446+
end
447+
current_picker:refresh(finder, { reset_prompt = true, multi = current_picker._multi })
448+
end
449+
420450
--- Toggle between file and folder browser for |builtin.file_browser|.
421451
---@param prompt_bufnr number: The prompt bufnr
422452
fb_actions.toggle_browser = function(prompt_bufnr, opts)

0 commit comments

Comments
 (0)