this repo has no description
1-- ┌─────────────────┐
2-- │ Custom mappings │
3-- └─────────────────┘
4--
5-- This file contains definitions of custom general and Leader mappings.
6
7-- General mappings ===========================================================
8
9-- Use this section to add custom general mappings. See `:h vim.keymap.set()`.
10
11-- An example helper to create a Normal mode mapping
12local nmap = function(lhs, rhs, desc)
13 -- See `:h vim.keymap.set()`
14 vim.keymap.set('n', lhs, rhs, { desc = desc })
15end
16
17-- Paste linewise before/after current line
18-- Usage: `yiw` to yank a word and `]p` to put it on the next line.
19nmap('[p', '<Cmd>exe "put! " . v:register<CR>', 'Paste Above')
20nmap(']p', '<Cmd>exe "put " . v:register<CR>', 'Paste Below')
21
22-- Many general mappings are created by 'mini.basics'. See 'plugin/30_mini.lua'
23
24-- stylua: ignore start
25-- The next part (until `-- stylua: ignore end`) is aligned manually for easier
26-- reading. Consider preserving this or remove `-- stylua` lines to autoformat.
27
28-- Leader mappings ============================================================
29
30-- Neovim has the concept of a Leader key (see `:h <Leader>`). It is a configurable
31-- key that is primarily used for "workflow" mappings (opposed to text editing).
32-- Like "open file explorer", "create scratch buffer", "pick from buffers".
33--
34-- In 'plugin/10_options.lua' <Leader> is set to <Space>, i.e. press <Space>
35-- whenever there is a suggestion to press <Leader>.
36--
37-- This config uses a "two key Leader mappings" approach: first key describes
38-- semantic group, second key executes an action. Both keys are usually chosen
39-- to create some kind of mnemonic.
40-- Example: `<Leader>f` groups "find" type of actions; `<Leader>ff` - find files.
41-- Use this section to add Leader mappings in a structural manner.
42--
43-- Usually if there are global and local kinds of actions, lowercase second key
44-- denotes global and uppercase - local.
45-- Example: `<Leader>fs` / `<Leader>fS` - find workspace/document LSP symbols.
46--
47-- Many of the mappings use 'mini.nvim' modules set up in 'plugin/30_mini.lua'.
48
49-- Create a global table with information about Leader groups in certain modes.
50-- This is used to provide 'mini.clue' with extra clues.
51-- Add an entry if you create a new group.
52Config.leader_group_clues = {
53 { mode = 'n', keys = '<Leader>b', desc = '+Buffer' },
54 { mode = 'n', keys = '<Leader>e', desc = '+Explore/Edit' },
55 { mode = 'n', keys = '<Leader>f', desc = '+Find' },
56 { mode = 'n', keys = '<Leader>g', desc = '+Git' },
57 { mode = 'n', keys = '<Leader>l', desc = '+Language' },
58 { mode = 'n', keys = '<Leader>m', desc = '+Map' },
59 { mode = 'n', keys = '<Leader>o', desc = '+Other' },
60 { mode = 'n', keys = '<Leader>s', desc = '+Session' },
61 { mode = 'n', keys = '<Leader>t', desc = '+Terminal' },
62 { mode = 'n', keys = '<Leader>v', desc = '+Visits' },
63
64 { mode = 'x', keys = '<Leader>g', desc = '+Git' },
65 { mode = 'x', keys = '<Leader>l', desc = '+Language' },
66}
67
68-- Helpers for a more concise `<Leader>` mappings.
69-- Most of the mappings use `<Cmd>...<CR>` string as a right hand side (RHS) in
70-- an attempt to be more concise yet descriptive. See `:h <Cmd>`.
71-- This approach also doesn't require the underlying commands/functions to exist
72-- during mapping creation: a "lazy loading" approach to improve startup time.
73local nmap_leader = function(suffix, rhs, desc)
74 vim.keymap.set('n', '<Leader>' .. suffix, rhs, { desc = desc })
75end
76local xmap_leader = function(suffix, rhs, desc)
77 vim.keymap.set('x', '<Leader>' .. suffix, rhs, { desc = desc })
78end
79
80-- b is for 'Buffer'. Common usage:
81-- - `<Leader>bs` - create scratch (temporary) buffer
82-- - `<Leader>ba` - navigate to the alternative buffer
83-- - `<Leader>bw` - wipeout (fully delete) current buffer
84local new_scratch_buffer = function()
85 vim.api.nvim_win_set_buf(0, vim.api.nvim_create_buf(true, true))
86end
87
88nmap_leader('ba', '<Cmd>b#<CR>', 'Alternate')
89nmap_leader('bd', '<Cmd>lua MiniBufremove.delete()<CR>', 'Delete')
90nmap_leader('bD', '<Cmd>lua MiniBufremove.delete(0, true)<CR>', 'Delete!')
91nmap_leader('bs', new_scratch_buffer, 'Scratch')
92nmap_leader('bw', '<Cmd>lua MiniBufremove.wipeout()<CR>', 'Wipeout')
93nmap_leader('bW', '<Cmd>lua MiniBufremove.wipeout(0, true)<CR>', 'Wipeout!')
94
95-- e is for 'Explore' and 'Edit'. Common usage:
96-- - `<Leader>ed` - open explorer at current working directory
97-- - `<Leader>ef` - open directory of current file (needs to be present on disk)
98-- - `<Leader>ei` - edit 'init.lua'
99-- - All mappings that use `edit_plugin_file` - edit 'plugin/' config files
100local edit_plugin_file = function(filename)
101 return string.format('<Cmd>edit %s/plugin/%s<CR>', vim.fn.stdpath('config'), filename)
102end
103local explore_at_file = '<Cmd>lua MiniFiles.open(vim.api.nvim_buf_get_name(0))<CR>'
104local explore_quickfix = function()
105 vim.cmd(vim.fn.getqflist({ winid = true }).winid ~= 0 and 'cclose' or 'copen')
106end
107local explore_locations = function()
108 vim.cmd(vim.fn.getloclist(0, { winid = true }).winid ~= 0 and 'lclose' or 'lopen')
109end
110
111nmap_leader('ed', '<Cmd>lua MiniFiles.open()<CR>', 'Directory')
112nmap_leader('ef', explore_at_file, 'File directory')
113nmap_leader('ei', '<Cmd>edit $MYVIMRC<CR>', 'init.lua')
114nmap_leader('ek', edit_plugin_file('20_keymaps.lua'), 'Keymaps config')
115nmap_leader('em', edit_plugin_file('30_mini.lua'), 'MINI config')
116nmap_leader('en', '<Cmd>lua MiniNotify.show_history()<CR>', 'Notifications')
117nmap_leader('eo', edit_plugin_file('10_options.lua'), 'Options config')
118nmap_leader('ep', edit_plugin_file('40_plugins.lua'), 'Plugins config')
119nmap_leader('eq', explore_quickfix, 'Quickfix list')
120nmap_leader('eQ', explore_locations, 'Location list')
121
122-- f is for 'Fuzzy Find'. Common usage:
123-- - `<Leader>ff` - find files; for best performance requires `ripgrep`
124-- - `<Leader>fg` - find inside files; requires `ripgrep`
125-- - `<Leader>fh` - find help tag
126-- - `<Leader>fr` - resume latest picker
127-- - `<Leader>fv` - all visited paths; requires 'mini.visits'
128--
129-- All these use 'mini.pick'. See `:h MiniPick-overview` for an overview.
130local pick_added_hunks_buf = '<Cmd>Pick git_hunks path="%" scope="staged"<CR>'
131
132nmap_leader('f/', '<Cmd>Pick history scope="/"<CR>', '"/" history')
133nmap_leader('f:', '<Cmd>Pick history scope=":"<CR>', '":" history')
134nmap_leader('fa', '<Cmd>Pick git_hunks scope="staged"<CR>', 'Added hunks (all)')
135nmap_leader('fA', pick_added_hunks_buf, 'Added hunks (buf)')
136nmap_leader('fb', '<Cmd>Pick buffers<CR>', 'Buffers')
137nmap_leader('fc', '<Cmd>Pick git_commits<CR>', 'Commits (all)')
138nmap_leader('fC', '<Cmd>Pick git_commits path="%"<CR>', 'Commits (buf)')
139nmap_leader('fd', '<Cmd>Pick diagnostic scope="all"<CR>', 'Diagnostic workspace')
140nmap_leader('fD', '<Cmd>Pick diagnostic scope="current"<CR>', 'Diagnostic buffer')
141nmap_leader('ff', '<Cmd>Pick files<CR>', 'Files')
142nmap_leader('fg', '<Cmd>Pick grep_live<CR>', 'Grep live')
143nmap_leader('fG', '<Cmd>Pick grep pattern="<cword>"<CR>', 'Grep current word')
144nmap_leader('fh', '<Cmd>Pick help<CR>', 'Help tags')
145nmap_leader('fH', '<Cmd>Pick hl_groups<CR>', 'Highlight groups')
146nmap_leader('fl', '<Cmd>Pick buf_lines scope="all"<CR>', 'Lines (all)')
147nmap_leader('fL', '<Cmd>Pick buf_lines scope="current"<CR>', 'Lines (buf)')
148nmap_leader('fm', '<Cmd>Pick git_hunks<CR>', 'Modified hunks (all)')
149nmap_leader('fM', '<Cmd>Pick git_hunks path="%"<CR>', 'Modified hunks (buf)')
150nmap_leader('fr', '<Cmd>Pick resume<CR>', 'Resume')
151nmap_leader('fR', '<Cmd>Pick lsp scope="references"<CR>', 'References (LSP)')
152nmap_leader('fs', '<Cmd>Pick lsp scope="workspace_symbol"<CR>', 'Symbols workspace')
153nmap_leader('fS', '<Cmd>Pick lsp scope="document_symbol"<CR>', 'Symbols document')
154nmap_leader('fv', '<Cmd>Pick visit_paths cwd=""<CR>', 'Visit paths (all)')
155nmap_leader('fV', '<Cmd>Pick visit_paths<CR>', 'Visit paths (cwd)')
156
157-- g is for 'Git'. Common usage:
158-- - `<Leader>gs` - show information at cursor
159-- - `<Leader>go` - toggle 'mini.diff' overlay to show in-buffer unstaged changes
160-- - `<Leader>gd` - show unstaged changes as a patch in separate tabpage
161-- - `<Leader>gL` - show Git log of current file
162local git_log_cmd = [[Git log --pretty=format:\%h\ \%as\ │\ \%s --topo-order]]
163local git_log_buf_cmd = git_log_cmd .. ' --follow -- %'
164
165nmap_leader('ga', '<Cmd>Git diff --cached<CR>', 'Added diff')
166nmap_leader('gA', '<Cmd>Git diff --cached -- %<CR>', 'Added diff buffer')
167nmap_leader('gc', '<Cmd>Git commit<CR>', 'Commit')
168nmap_leader('gC', '<Cmd>Git commit --amend<CR>', 'Commit amend')
169nmap_leader('gd', '<Cmd>Git diff<CR>', 'Diff')
170nmap_leader('gD', '<Cmd>Git diff -- %<CR>', 'Diff buffer')
171nmap_leader('gl', '<Cmd>' .. git_log_cmd .. '<CR>', 'Log')
172nmap_leader('gL', '<Cmd>' .. git_log_buf_cmd .. '<CR>', 'Log buffer')
173nmap_leader('go', '<Cmd>lua MiniDiff.toggle_overlay()<CR>', 'Toggle overlay')
174nmap_leader('gs', '<Cmd>lua MiniGit.show_at_cursor()<CR>', 'Show at cursor')
175
176xmap_leader('gs', '<Cmd>lua MiniGit.show_at_cursor()<CR>', 'Show at selection')
177
178-- l is for 'Language'. Common usage:
179-- - `<Leader>ld` - show more diagnostic details in a floating window
180-- - `<Leader>lr` - perform rename via LSP
181-- - `<Leader>ls` - navigate to source definition of symbol under cursor
182--
183-- NOTE: most LSP mappings represent a more structured way of replacing built-in
184-- LSP mappings (like `:h gra` and others). This is needed because `gr` is mapped
185-- by an "replace" operator in 'mini.operators' (which is more commonly used).
186nmap_leader('la', '<Cmd>lua vim.lsp.buf.code_action()<CR>', 'Actions')
187nmap_leader('ld', '<Cmd>lua vim.diagnostic.open_float()<CR>', 'Diagnostic popup')
188nmap_leader('lf', '<Cmd>lua require("conform").format()<CR>', 'Format')
189nmap_leader('li', '<Cmd>lua vim.lsp.buf.implementation()<CR>', 'Implementation')
190nmap_leader('lh', '<Cmd>lua vim.lsp.buf.hover()<CR>', 'Hover')
191nmap_leader('lr', '<Cmd>lua vim.lsp.buf.rename()<CR>', 'Rename')
192nmap_leader('lR', '<Cmd>lua vim.lsp.buf.references()<CR>', 'References')
193nmap_leader('ls', '<Cmd>lua vim.lsp.buf.definition()<CR>', 'Source definition')
194nmap_leader('lt', '<Cmd>lua vim.lsp.buf.type_definition()<CR>', 'Type definition')
195
196xmap_leader('lf', '<Cmd>lua require("conform").format()<CR>', 'Format selection')
197
198-- m is for 'Map'. Common usage:
199-- - `<Leader>mt` - toggle map from 'mini.map' (closed by default)
200-- - `<Leader>mf` - focus on the map for fast navigation
201-- - `<Leader>ms` - change map's side (if it covers something underneath)
202nmap_leader('mf', '<Cmd>lua MiniMap.toggle_focus()<CR>', 'Focus (toggle)')
203nmap_leader('mr', '<Cmd>lua MiniMap.refresh()<CR>', 'Refresh')
204nmap_leader('ms', '<Cmd>lua MiniMap.toggle_side()<CR>', 'Side (toggle)')
205nmap_leader('mt', '<Cmd>lua MiniMap.toggle()<CR>', 'Toggle')
206
207-- o is for 'Other'. Common usage:
208-- - `<Leader>oz` - toggle between "zoomed" and regular view of current buffer
209nmap_leader('or', '<Cmd>lua MiniMisc.resize_window()<CR>', 'Resize to default width')
210nmap_leader('ot', '<Cmd>lua MiniTrailspace.trim()<CR>', 'Trim trailspace')
211nmap_leader('oz', '<Cmd>lua MiniMisc.zoom()<CR>', 'Zoom toggle')
212
213-- s is for 'Session'. Common usage:
214-- - `<Leader>sn` - start new session
215-- - `<Leader>sr` - read previously started session
216-- - `<Leader>sd` - delete previously started session
217local session_new = 'MiniSessions.write(vim.fn.input("Session name: "))'
218
219nmap_leader('sd', '<Cmd>lua MiniSessions.select("delete")<CR>', 'Delete')
220nmap_leader('sn', '<Cmd>lua ' .. session_new .. '<CR>', 'New')
221nmap_leader('sr', '<Cmd>lua MiniSessions.select("read")<CR>', 'Read')
222nmap_leader('sw', '<Cmd>lua MiniSessions.write()<CR>', 'Write current')
223
224-- t is for 'Terminal'
225nmap_leader('tT', '<Cmd>horizontal term<CR>', 'Terminal (horizontal)')
226nmap_leader('tt', '<Cmd>vertical term<CR>', 'Terminal (vertical)')
227
228-- v is for 'Visits'. Common usage:
229-- - `<Leader>vv` - add "core" label to current file.
230-- - `<Leader>vV` - remove "core" label to current file.
231-- - `<Leader>vc` - pick among all files with "core" label.
232local make_pick_core = function(cwd, desc)
233 return function()
234 local sort_latest = MiniVisits.gen_sort.default({ recency_weight = 1 })
235 local local_opts = { cwd = cwd, filter = 'core', sort = sort_latest }
236 MiniExtra.pickers.visit_paths(local_opts, { source = { name = desc } })
237 end
238end
239
240nmap_leader('vc', make_pick_core('', 'Core visits (all)'), 'Core visits (all)')
241nmap_leader('vC', make_pick_core(nil, 'Core visits (cwd)'), 'Core visits (cwd)')
242nmap_leader('vv', '<Cmd>lua MiniVisits.add_label("core")<CR>', 'Add "core" label')
243nmap_leader('vV', '<Cmd>lua MiniVisits.remove_label("core")<CR>', 'Remove "core" label')
244nmap_leader('vl', '<Cmd>lua MiniVisits.add_label()<CR>', 'Add label')
245nmap_leader('vL', '<Cmd>lua MiniVisits.remove_label()<CR>', 'Remove label')
246-- stylua: ignore end