local api = vim.api local wk = require('which-key') local function pandoc_convert(ofiletype) if ofiletype == nil then ofiletype = 'html' end local ifilename = vim.fn.expand('%:p') local ofilename = vim.fn.expand('%:p:r') .. '.' .. ofiletype local cmd if ofiletype == 'pdf' then cmd = string.format('pandoc %s -o %s --pdf-engine=xelatex -V "mainfont:Iosevka Etoile" -V "sansfont:Iosevka Aile" -V "monofont:Iosevka" -V "geometry:margin=1in"', ifilename, ofilename) else cmd = string.format('pandoc %s -o %s', ifilename, ofilename) end vim.fn.jobstart(cmd) end -- Escape to normal mode in terminal buffer api.nvim_set_keymap('t', '', '', {noremap = true, silent = true}) -- Continuous indent api.nvim_set_keymap('v', '<', '', '>gv', {noremap = true, silent = true}) -- Also move up/down virual lines (:set wrap) api.nvim_set_keymap('n', 'j', 'gj', {noremap = true, silent = true}) api.nvim_set_keymap('n', 'k', 'gk', {noremap = true, silent = true}) -- winshift.nvim api.nvim_set_keymap('n', '', ':WinShift', {noremap = true, silent = true}) api.nvim_set_keymap('n', 'm', ':WinShift', {noremap = true, silent = true}) api.nvim_set_keymap('n', '', ':WinShift left', {noremap = true, silent = true}) api.nvim_set_keymap('n', '', ':WinShift down', {noremap = true, silent = true}) api.nvim_set_keymap('n', '', ':WinShift up', {noremap = true, silent = true}) api.nvim_set_keymap('n', '', ':WinShift right', {noremap = true, silent = true}) ----------------- -- Normal mode -- ----------------- wk.register({ -- Better Y Y = {'y$', 'Yank to eol'}, -- Easier start and end of line H = {'^', 'Start of the line'}, L = {'$', 'End of the line'}, -- Close a window [''] = {'q', 'Quit current window'}, -- Close current buffer [''] = {':bdelete', 'Close current buffer'}, -- Copy the whole buffer [''] = {':%y+', 'Copy whole buffer'}, -- Remove trailing whitespace [''] = {':%s/\\s\\+$//e', 'Remove trailing'}, -- Resize buffer [''] = {':resize -2', 'Resize vertical -2'}, [''] = {':resize +2', 'Resize vertical +2'}, [''] = {':vertical resize -2', 'Resize horizontal -2'}, [''] = {':vertical resize +2', 'Resize horizontal +2'}, -- Switch between tabs and spaces [''] = { function() if vim.opt.expandtab:get() then vim.opt.expandtab = false vim.opt.smarttab = false vim.opt.softtabstop = 0 -- reset to default vim.notify('Indent with Tabs.', vim.log.levels.INFO) else vim.opt.expandtab = true vim.opt.smarttab = true vim.opt.softtabstop = -1 -- fallback to shiftwidth vim.notify('Indent with Spaces.', vim.log.levels.INFO) end end, 'Switch indent style' }, -- Naming common keys ['['] = { name = 'Block motions (previous)', d = 'Previous diagnostics', g = 'Previous git hunk', m = 'Previous start of outer class', M = 'Previous end of outer class', ['['] = 'Previous start of outer function', [']'] = 'Previous end of outer function' }, [']'] = { name = 'Block motions (next)', d = 'Next diagnostics', g = 'Next git hunk', m = 'Next start of outer class', M = 'Next end of outer class', ['['] = 'Next start of outer function', [']'] = 'Next end of outer function' }, g = { name = 'Goto motions', c = { name = 'Comment', c = 'Current line' }, d = 'Go to definition', D = 'Go to declaration', i = 'Go to implementation', n = { name = 'Incremental selection', i = 'Initialize selection' }, r = 'Go to references', R = {':TroubleToggle lsp_references', 'Reference list'} }, K = {name = 'Hover'}, z = {name = 'Misc utils'}, -- Window picker ['-'] = {':WindowPick', 'Choose window'}, -- Move between tabs [''] = {':BufferLineCycleNext', 'Next buffer'}, [''] = {':BufferLineCyclePrev', 'Previous buffer'}, -- NvimTree [''] = {':NvimTreeToggle', 'NvimTree'}, -- ToggleTerm [''] = {':ToggleTerm', 'Toggle terminal'}, [''] = {':ToggleTerm direction=float', 'Toggle float terminal'}, -- hop.nvim S = {':HopWord', 'Hop to word'}, [''] = {':HopChar1', 'Hop to character'}, [''] = {':HopPattern', 'Hop to pattern'} }) ----------------- -- Visual mode -- ----------------- wk.register({ a = { c = 'Outer class', f = 'Outer function' }, i = { c = 'Inner class', f = 'Inner function' }, g = { c = 'Comment', n = { name = 'Incremental selection', n = 'Increment node', s = 'Increment scope', m = 'Decrement node' } } }, {mode = 'v'}) ----------------------------------- -- Normal mode (with leader key) -- ----------------------------------- wk.register({ b = { name = 'Buffer', c = {':ColorizerToggle', 'Colorizer'}, d = { name = 'Delete', a = {':BDelete all', 'All buffers'}, d = {':BDelete this', 'Current buffer'}, h = {':BDelete hidden', 'Hidden buffers'}, n = {':BDelete nameless', 'Buffers without name'}, o = {':BDelete other', 'Other buffers'} }, j = {':BufferLineCyclePrev', 'Previous buffer'}, k = {':BufferLineCycleNext', 'Next buffer'}, n = {':enew', 'New buffer'}, u = {':UndotreeToggle', 'Undotree'}, v = { name = 'Convert', m = {function() pandoc_convert('md') end, 'To Markdown'}, o = {function() pandoc_convert('org') end, 'To Org'}, p = {function() pandoc_convert('pdf') end, 'To PDF'}, r = {function() pandoc_convert('rst') end, 'To RST'}, t = {function() pandoc_convert('tex') end, 'To LaTeX'}, w = {function() pandoc_convert('html') end, 'To HTML'} } }, d = { name = 'DAP', b = 'Toggle breakpoint', B = 'Set breakpoint with condition', c = 'Run to cursor', d = 'Disconnect adapter', e = 'Hover under cursor (UI)', f = 'Open float window (UI)', i = 'Step into function/method', l = 'List breakpoints', n = 'Resume the execution', o = 'Step out of function/method', r = 'Inspect in REPL', t = 'Toggle UI', v = 'Run again 1 step', z = 'Re-run the last adapter' }, e = { name = 'Editor', a = {':EasyAlign', 'Align elements'}, g = 'Generate annotations', h = {':TSHighlightCapturesUnderCursor', 'Syntax under cursor'}, s = {':ISwapWith', 'Swap elements'}, t = {':Twilight', 'Twilight mode'}, v = { function() local venn_enabled = vim.inspect(vim.b.venn_enabled) if venn_enabled == 'nil' then vim.b.venn_enabled = true vim.api.nvim_command('setlocal virtualedit=all') -- Draw lines with HJKL keystroke vim.api.nvim_buf_set_keymap(0, 'n', 'H', 'h:VBox', {noremap = true, silent = true}) vim.api.nvim_buf_set_keymap(0, 'n', 'J', 'j:VBox', {noremap = true, silent = true}) vim.api.nvim_buf_set_keymap(0, 'n', 'K', 'k:VBox', {noremap = true, silent = true}) vim.api.nvim_buf_set_keymap(0, 'n', 'L', 'l:VBox', {noremap = true, silent = true}) -- Draw boxes by pressing 'f' with visual selection vim.api.nvim_buf_set_keymap(0, 'v', 'f', ':VBox', {noremap = true, silent = true}) vim.notify('Virtual box edit enabled.', vim.log.levels.INFO) else vim.b.venn_enabled = nil vim.api.nvim_command('setlocal virtualedit=block') -- vim.api.nvim_command('mapclear ') -- quicker, but also deletes buf keymap for lsp vim.api.nvim_buf_del_keymap(0, 'v', 'f') vim.api.nvim_buf_del_keymap(0, 'n', 'H') vim.api.nvim_buf_del_keymap(0, 'n', 'J') vim.api.nvim_buf_del_keymap(0, 'n', 'L') local present, _ = pcall(require, 'lspconfig') -- Check to re-enable buf keymap for hover if present then vim.api.nvim_buf_set_keymap(0, 'n', 'K', ':lua vim.lsp.buf.hover()', {noremap = true, silent = true}) else vim.api.nvim_buf_del_keymap(0, 'n', 'K') end vim.notify('Virtual box edit disabled.', vim.log.levels.INFO) end end, 'Toggle virtual box edit' }, z = {':ZenMode', 'Zen mode'} }, -- Telescope f = { name = 'Telescope', a = {':Telescope autocommands', 'Autocommands'}, b = {':Telescope buffers', 'Buffers'}, c = {':Telescope commands', 'Commands'}, d = {':Telescope projects', 'Recent directories'}, e = {':Telescope file_browser', 'File browser'}, f = {':Telescope find_files', 'Find files'}, g = {':Telescope live_grep', 'Live grep'}, h = {':Telescope help_tags', 'Help tags'}, i = {':Telescope highlights', 'Highlight groups'}, j = {':Telescope symbols', 'Pick emojis'}, k = {':Telescope keymaps', 'Normal keymaps'}, m = {':Telescope marks', 'Bookmarks'}, n = {':Telescope man_pages', 'Man pages'}, o = {':Telescope oldfiles', 'Recent files'}, p = {':Telescope project display_type=full', 'Projects'}, r = {':Telescope reloader', 'Reload lua modules'}, s = {':Telescope treesitter', 'Treesitter'}, t = {':Telescope', 'Telescope'}, u = {':Telescope current_buffer_fuzzy_find', 'Search current buffer'}, v = {':Telescope vim_options', 'Vim options'}, y = {':Telescope filetypes', 'Filetypes'}, z = {':Telescope registers', 'Vim registers'} }, -- Git g = { name = 'Git', b = 'Blame current line', p = 'Preview hunk', r = 'Reset hunk', R = 'Reset all hunks in buffer', s = 'Stage hunk', S = 'Stage buffer', u = 'Undo hunk', U = 'Reset buffer index', y = 'Get remote url for cursorline', Y = 'Get remote url', n = {':Neogit', 'Neogit'}, f = { name = 'Telescope', a = {':Telescope git_stash', 'Stash'}, b = {':Telescope git_bcommits', 'Buffer commits'}, c = {':Telescope git_commits', 'Commits'}, m = {':Telescope git_branches', 'Branches'}, s = {':Telescope git_status', 'Status'} } }, -- translate-shell.vim j = { name = 'Translate', t = {':Trans', 'Translate'}, d = {':TransSelectDirection', 'Translate with direction'}, r = {'cw=system(\'trans -brief -no-ansi\', getreg(""))[:-2]', 'Translate and replace'}, c = {'cw=system(\'trans -brief -no-ansi :\', getreg(""))[:-2]', 'Translate and replace with direction'} }, l = { name = 'LSP', a = 'Add workspace folder', d = 'Type definition', e = 'Line diagnostics', l = 'Set diagnostics loclist', n = 'Rename in buffer', o = 'Format buffer', r = 'Remove workspace folder', w = 'List workspace folders', x = 'Signature help', f = { name = 'Telescope', a = {':Telescope lsp_code_actions', 'Code actions'}, d = {':Telescope lsp_document_diagnostics', 'Buffer diagnostics'}, D = {':Telescope lsp_workspace_diagnostics', 'Workspace diagnostics'}, e = {':Telescope lsp_dynamic_workspace_symbols', 'Dynamic workspace symbols'}, i = {':Telescope lsp_implementations', 'Implementations'}, n = {':Telescope lsp_definitions', 'Definitions'}, r = {':Telescope lsp_references', 'References'}, s = {':Telescope lsp_document_symbols', 'Buffer symbols'}, S = {':Telescope lsp_workspace_symbols', 'Workspace symbols'} }, g = {':SymbolsOutline', 'Symbol outline'}, t = { name = 'Trouble', d = {':TroubleToggle lsp_definitions', 'Definition list'}, e = {':TroubleToggle lsp_document_diagnostics', 'Document diagnostics list'}, w = {':TroubleToggle lsp_workspace_diagnostics', 'Workspace diagnostics list'}, l = {':TroubleToggle loclist', 'Location list items'}, q = {':TroubleToggle quickfix', 'Quickfix list'}, t = {':TroubleToggle', 'Summary'} } }, p = { name = 'Find/Replace', o = 'Open spectre', p = 'Search in current file', w = 'Find/replace cursorword' }, r = { name = 'REST', c = {'RestNvim', 'Run request under cursor'}, p = {'RestNvimPreview', 'Preview request cURL command'}, l = {'RestNvimLast', 'Re-run last request'} }, s = { name = 'Session', s = 'Save session for current directory', d = 'Stop automatically saving', l = 'Load last session', r = 'Load session for current directory' }, -- t = { -- name = 'Todo', -- f = {':TodoQuickFix', 'Quick fix'}, -- l = {':TodoLocList', 'Loclist'}, -- t = {':TodoTrouble', 'Open in Trouble'}, -- z = {':TodoTelescope', 'Open in Telescope'} -- }, y ={ name = 'Figlet', s = {':.!figlet -f standard', 'standard'}, l = {':.!figlet -f slant', 'slant'}, b = {':.!figlet -f banner', 'banner'}, e = {':.!figlet -f lean', 'lean'}, p = {':.!figlet -f speed', 'speed'}, r = {':.!figlet -f roman', 'roman'}, d = {':.!figlet -f doh', 'doh'}, w = {':.!figlet -f starwars', 'starwars'}, m = {':.!figlet -f morse', 'morse'}, x = {':.!toilet -f term -F border', 'border'} } }, {prefix = ''}) ----------------------------------- -- Visual mode (with leader key) -- ----------------------------------- wk.register({ a = { name = 'Action', a = {':EasyAlign', 'Range align'} }, d = { name = 'DAP', e = 'Hover on range (UI)' }, g = { name = 'Git', r = 'Reset hunk', s = 'Stage hunk', y = 'Get remote url for range' }, j = { name = 'Translate', t = {':Trans', 'Translate'}, d = {':TransSelectDirection', 'Translate with direction'}, r = {'c=system(\'trans -brief -no-ansi\', getreg(""))[:-2]', 'Translate and replace'}, c = {'c=system(\'trans -brief -no-ansi :\', getreg(""))[:-2]', 'Translate and replace with direction'} }, l = { name = 'LSP', f = { name = 'Telescope', a = {':Telescope lsp_range_code_actions', 'Range code actions'} }, o = 'Range format' }, p = 'Find/Replace' }, {mode = 'v', prefix = ''}) ------------------------ -- Filetype specified -- ------------------------ vim.api.nvim_command [[ autocmd FileType org lua whichkeyOrg() autocmd FileType markdown lua whichkeyMarkdown() autocmd FileType html lua whichkeyHtml() ]] _G.whichkeyOrg = function() wk.register({ ['o'] = { name = 'Org', a = 'Agenda', A = 'Toggle ARCHIVE', c = 'Capture', e = 'Export', i = { name = 'Insert', h = 'Add headline', t = 'Add TODO heading and content', T = 'Add TODO heading', }, J = 'Move subtree down', K = 'Move subtree up', o = 'Open at point', r = 'Refile', t = 'Set tags', ['$'] = 'Archive current headline' }, [''] = 'Org meta return', [''] = 'Org increase date', [''] = 'Org decrease date', ['cid'] = 'Org change date', ['cit'] = 'Org TODO', ['ciT'] = 'Org TODO prev', [''] = 'Org toggle checkbox', [''] = 'Org cycle folding', [''] = 'Org cycle global folding', ['<<'] = 'Org promote headline', ['>>'] = 'Org demote headline', ['s'] = 'Org demote subtree', ['}'] = 'Org next visible heading', ['{'] = 'Org previous visible heading', [']]'] = 'Org forward heading', ['[['] = 'Org backward heading', ['g{'] = 'Org parent heading', ['?'] = 'Org help' }) end _G.whichkeyMarkdown = function() wk.register({ ['bp'] = {':MarkdownPreviewToggle', 'Preview markdown'} }) end _G.whichkeyHtml = function() wk.register({ ['bp'] = {':Bracey', 'Preview html'} }) end