From f8e91ab24576b2a7442e1c4b5176e56fe81f4c79 Mon Sep 17 00:00:00 2001 From: Fabian David Schmidt Date: Thu, 13 Jan 2022 14:57:51 +0100 Subject: [PATCH] rfc!: mappings --- README.md | 97 +++++++++++++++------- lua/telescope/_extensions/file_browser.lua | 30 ++++--- 2 files changed, 82 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index ebf6b1f3..f9e6c7cf 100644 --- a/README.md +++ b/README.md @@ -119,47 +119,80 @@ Note: `path` corresponds to the folder the `file_browser` is currently in. `telescope-file-browser.nvim` comes with a lot of default mappings for discoverability. You can use `telescope`'s `which_key` (insert mode: ``, normal mode: `?`) to list mappings attached to your picker. -| Insert / Normal | Action | -|-----------------|-------------------------------------------------------------------------------| -| `/c` | Create file/folder at current `path` (trailing path separator creates folder) | -| `/r` | Rename multi-selected files/folders | -| `/m` | Move multi-selected files/folders to current `path` | -| `/y` | Copy (multi-)selected files/folders to current `path` | -| `/d` | Delete (multi-)selected files/folders | -| `/o` | Open file/folder with default system application | -| `/b` | Go to parent directory | -| `/e` | Go to home directory | -| `/w` | Go to current working directory (cwd) | -| `/t` | Change nvim's cwd to selected folder/file(parent) | -| `/f` | Toggle between file and folder browser | -| `/h` | Toggle hidden files/folders | -| `/s` | Toggle all entries ignoring `./` and `../` | - -`path` denotes the folder the `file_browser` is currently in. - -#### Remappings - -As part of the [setup](#setup-and-configuration), you can remap actions as you like. The default mappings can also be found in this [file](https://github.com/nvim-telescope/telescope-file-browser.nvim/blob/master/lua/telescope/_extensions/file_browser.lua). +The code snippet below highlights how can customize your own mappings. It is not required to map the `telescope-file-browser`-specific defaults (telescope [defaults](https://github.com/nvim-telescope/telescope.nvim#default-mappings) not shown)! They are merely provided to simplify remapping. ```lua local fb_actions = require "telescope".extensions.file_browser.actions --- mappings in file_browser extension of telescope.setup -... +local actions = require "telescope.actions" + +require("telescope").setup { + extensions = { + file_browser = { mappings = { ["i"] = { - -- remap to going to home directory - [""] = fb_actions.goto_home_dir - [""] = function(prompt_bufnr) - -- your custom function - end + -- default insert mode mappings -- NOT NEEDED TO CONFIGURE + [""] = fb_actions.create, -- create file/dir at `path` (trailing separator creates dir) + [""] = fb_actions.rename, -- rename multi-selected files/folders + [""] = fb_actions.move, -- move multi-selected files/folders to current `path` + [""] = fb_actions.copy, -- copy multi-selected files/folders to current `path` + [""] = fb_actions.remove, -- remove multi-selected files/folders to current `path` + [""] = fb_actions.open, -- open file/folder with default system application + + [""] = fb_actions.toggle_browser, -- toggle between file and folder browser + [""] = fb_actions.goto_parent_dir, -- goto parent directory; alias to normal-mode + + ["="] = fb_actions.change_cwd, -- change nvim cwd to selected file (parent) or folder + ["~"] = fb_actions.goto_home_dir, -- go to home directory + ["`"] = fb_actions.goto_cwd, -- go to cwd + ["+"] = fb_actions.toggle_all, -- toggle selection of all shown entries ignoring `.` and `..` + [";"] = fb_actions.toggle_hidden, -- toggle showing hidden files and folders + + -- remove a mapping + ["KEY"] = false, + + -- your custom function + ["KEY"] = function(prompt_bufnr) + print("Implement your custom function; see actions.lua for inspiration") + end, + }, ["n"] = { - -- unmap toggling `fb_actions.toggle_browser` - f = false, + -- default normal mode mappings -- NOT NEEDED TO CONFIGURE + ["c"] = fb_actions.create, -- create file/dir at `path` (trailing separator creates dir) + ["r"] = fb_actions.rename, -- rename multi-selected files/folders + ["m"] = fb_actions.move, -- move multi-selected files/folders to current `path` + ["y"] = fb_actions.copy, -- copy multi-selected files/folders to current `path` + ["d"] = fb_actions.remove, -- remove multi-selected files/folders to current `path` + ["o"] = fb_actions.open, -- open file/folder with default system application + + + -- normal mode movement + ["h"] = actions.goto_parent_dir, -- goto parent directory + ["j"] = actions.move_selection_next, -- next entry + ["k"] = actions.move_selection_previous, -- previous entry + ["l"] = actions.select_default, -- confirm selection + + ["f"] = fb_actions.toggle_browser, -- toggle between file and folder browser + ["="] = fb_actions.change_cwd, -- change nvim cwd to selected file (parent) or folder + ["~"] = fb_actions.goto_home_dir, -- go to home directory + ["`"] = fb_actions.goto_cwd, -- go to home directory + ["-"] = fb_actions.goto_parent_dir, -- change nvim cwd to selected file (parent) or folder + ["+"] = fb_actions.toggle_all, -- toggle selection of all shown entries ignoring `.` and `..` + [";"] = fb_actions.toggle_hidden, -- toggle showing hidden files and folders + + -- your custom normal mode mappings + ... }, -... + }, + }, + }, +} + ``` -See [fb_actions](https://github.com/nvim-telescope/telescope-file-browser.nvim/blob/master/lua/telescope/_extensions/file_browser/actions.lua) for a list of native actions and inspiration on how to write your own custom action. As additional reference, `plenary`'s [Path](https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/path.lua) library powers a lot of the built-in actions. + +Once more, `path` denotes the folder the `file_browser` is currently in. + +Furthermore, see [fb_actions](https://github.com/nvim-telescope/telescope-file-browser.nvim/blob/master/lua/telescope/_extensions/file_browser/actions.lua) for a list of native actions and inspiration on how to write your own custom action. As additional reference, `plenary`'s [Path](https://github.com/nvim-lua/plenary.nvim/blob/master/lua/plenary/path.lua) library powers a lot of the built-in actions. For more information on `telescope` actions and remappings, see also the [upstream documentation](https://github.com/nvim-telescope/telescope.nvim#default-mappings) and associated vimdocs at `:h telescope.defaults.mappings`. diff --git a/lua/telescope/_extensions/file_browser.lua b/lua/telescope/_extensions/file_browser.lua index 0645b955..d4dd085f 100644 --- a/lua/telescope/_extensions/file_browser.lua +++ b/lua/telescope/_extensions/file_browser.lua @@ -53,6 +53,7 @@ local fb_actions = require "telescope._extensions.file_browser.actions" local fb_finders = require "telescope._extensions.file_browser.finders" local fb_picker = require "telescope._extensions.file_browser.picker" +local actions = require "telescope.actions" local action_state = require "telescope.actions.state" local action_set = require "telescope.actions.set" local Path = require "plenary.path" @@ -66,14 +67,14 @@ local pconf = { [""] = fb_actions.move, [""] = fb_actions.copy, [""] = fb_actions.remove, - [""] = fb_actions.open, - [""] = fb_actions.goto_home_dir, - [""] = fb_actions.goto_cwd, - [""] = fb_actions.change_cwd, + [""] = fb_actions.open, [""] = fb_actions.toggle_browser, - [""] = fb_actions.toggle_hidden, - [""] = fb_actions.toggle_all, - [""] = fb_actions.goto_parent_dir, + [""] = fb_actions.goto_parent_dir, + ["="] = fb_actions.change_cwd, + ["~"] = fb_actions.goto_home_dir, + ["`"] = fb_actions.goto_cwd, + ["+"] = fb_actions.toggle_all, + [";"] = fb_actions.toggle_hidden, }, ["n"] = { ["c"] = fb_actions.create, @@ -82,13 +83,16 @@ local pconf = { ["y"] = fb_actions.copy, ["d"] = fb_actions.remove, ["o"] = fb_actions.open, - ["b"] = fb_actions.goto_parent_dir, - ["e"] = fb_actions.goto_home_dir, - ["w"] = fb_actions.goto_cwd, - ["t"] = fb_actions.change_cwd, ["f"] = fb_actions.toggle_browser, - ["h"] = fb_actions.toggle_hidden, - ["s"] = fb_actions.toggle_all, + ["h"] = fb_actions.goto_parent_dir, + ["j"] = actions.move_selection_next, + ["k"] = actions.move_selection_previous, + ["l"] = actions.select_default, + ["="] = fb_actions.change_cwd, + ["~"] = fb_actions.goto_home_dir, + ["`"] = fb_actions.goto_cwd, + ["+"] = fb_actions.toggle_all, + [";"] = fb_actions.toggle_hidden, }, }, attach_mappings = function(prompt_bufnr, _)