This repository has been archived on 2022-08-21. You can view files and clone it, but cannot push or open issues or pull requests.
dotfiles/home/.config/nvim/lua/mappings.lua

230 lines
7.4 KiB
Lua

local api = vim.api
local wk = require('which-key')
-- No one likes Esc
api.nvim_set_keymap('i', 'jk', [[<Esc>]], {noremap = true, silent = true})
-- Escape to normal mode in terminal buffer
api.nvim_set_keymap('t', '<Esc>', '<C-\\><C-n>', {noremap = true, silent = true})
-- Continuous indent
api.nvim_set_keymap('v', '<', '<gv', {noremap = true, silent = true})
api.nvim_set_keymap('v', '>', '>gv', {noremap = true, silent = true})
-- Clear search results
-- api.nvim_set_keymap('n', '<Esc>', '<Cmd>noh<CR>', {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'},
-- Easier moving between windows
['<C-h>'] = {'<C-w>h', 'Go to the left window'},
['<C-l>'] = {'<C-w>l', 'Go to the right window'},
['<C-j>'] = {'<C-w>j', 'Go to the down window'},
['<C-k>'] = {'<C-w>k', 'Go to the up window'},
-- Copy the whole buffer
['<C-a>'] = {'<Cmd>%y+<CR>', 'Copy whole buffer'},
-- 'Legacy' save buffer
-- ['<C-s>'] = {':w<CR>', 'Write buffer'},
-- Close buffer
['<C-x>'] = {':bd!<CR>', 'Close buffer'},
-- Remove trailing whitespace
['<A-w>'] = {':%s/\\s\\+$//e<CR>', 'Remove trailing'},
-- Resize buffer
['<A-j>'] = {':resize -2<CR>', 'Resize vertical -2'},
['<A-k>'] = {':resize +2<CR>', 'Resize vertical +2'},
['<A-h>'] = {':vertical resize -2<CR>', 'Resize horizontal -2'},
['<A-l>'] = {':vertical resize +2<CR>', 'Resize horizontal +2'},
-- Switch between tabs and spaces
['<A-t>'] = {
function()
if vim.o.expandtab == true then
vim.cmd('set noexpandtab nosmarttab softtabstop& shiftwidth&')
vim.cmd('echomsg "Switched to indent with tabs"')
else
vim.opt.expandtab = true
vim.opt.smarttab = true
vim.opt.softtabstop = 4
vim.opt.shiftwidth = 4
vim.cmd('echomsg "Switched to indent with 4 spaces"')
end
end,
'Switch indent style'
},
-- Naming common keys
['['] = {name = 'Block motions (previous)'},
[']'] = {name = 'Block motions (next)'},
g = {name = 'Goto motions'},
z = {name = 'Misc utils'},
-- Don't need to show bufferline numbers
['<leader>1'] = 'which_key_ignore',
['<leader>2'] = 'which_key_ignore',
['<leader>3'] = 'which_key_ignore',
['<leader>4'] = 'which_key_ignore',
['<leader>5'] = 'which_key_ignore',
['<leader>6'] = 'which_key_ignore',
['<leader>7'] = 'which_key_ignore',
['<leader>8'] = 'which_key_ignore',
['<leader>9'] = 'which_key_ignore',
-- Move between tabs
['<TAB>'] = {'<Cmd>BufferLineCycleNext<CR>', 'Next buffer'},
['<S-TAB>'] = {'<Cmd>BufferLineCyclePrev<CR>', 'Previous buffer'},
-- NvimTree
['<C-n>'] = {':NvimTreeToggle<CR>', 'NvimTree'},
-- ToggleTerm
['<C-\\>'] = {':ToggleTerm<CR>', 'Toggle terminal'}
})
-----------------------------------
-- Normal mode (with leader key) --
-----------------------------------
wk.register({
c = {':ColorizerToggle<CR>', 'Toggle colorizer'},
b = {
name = 'Buffer',
n = {':DashboardNewFile<CR>', 'New file'}
},
-- Telescope
f = {
name = 'Telescope',
a = {':Telescope autocommands<CR>', 'Autocommands'},
b = {':Telescope buffers<CR>', 'Buffers'},
c = {':Telescope commands<CR>', 'Commands'},
e = {':Telescope file_browser<CR>', 'File browser'},
f = {':Telescope find_files<CR>', 'Find files'},
g = {':Telescope live_grep<CR>', 'Live grep'},
h = {':Telescope help_tags<CR>', 'Help tags'},
i = {':Telescope highlights<CR>', 'Highlight groups'},
j = {':Telescope symbols<CR>', 'Pick emojis'},
k = {':Telescope keymaps<CR>', 'Normal keymaps'},
m = {':Telescope marks<CR>', 'Bookmarks'},
o = {':Telescope oldfiles<CR>', 'Recent files'},
p = {':Telescope man_pages<CR>', 'Man pages'},
r = {':Telescope reloader<CR>', 'Reload lua modules'},
s = {':Telescope treesitter<CR>', 'Treesitter'},
t = {':Telescope<CR>', 'Telescope'},
u = {':Telescope current_buffer_fuzzy_find<CR>', 'Search current buffer'},
v = {':Telescope vim_options<CR>', 'Vim options'},
y = {':Telescope filetypes<CR>', 'Filetypes'},
z = {':Telescope registers<CR>', 'Vim registers'}
},
l = {
name = 'LSP'
},
-- Git
g = {
name = 'Git',
b = 'Blame current line',
j = 'Next hunk',
k = 'Previous hunk',
p = 'Preview hunk',
r = 'Reset hunk',
R = 'Reset all hunks in buffer',
s = 'Stage hunk',
u = 'Undo hunk',
n = {':Neogit<CR>', 'Neogit'},
f = {
name = 'Telescope',
a = {':Telescope git_stash<CR>', 'Stash'},
b = {':Telescope git_bcommits<CR>', 'Buffer commits'},
c = {':Telescope git_commits<CR>', 'Commits'},
m = {':Telescope git_branches<CR>', 'Branches'},
s = {':Telescope git_status<CR>', 'Status'}
}
},
-- Tab related
t = {
name = 'Tab',
n = {'<Cmd>tabnext<CR>', 'Next tab'},
p = {'<Cmd>tabprev<CR>', 'Previous tab'},
t = {'<Cmd>tabnew<CR>', 'New tab'}
}
}, {prefix = '<leader>'})
-----------------------------------
-- Visual mode (with leader key) --
-----------------------------------
wk.register({
-- GitSigns
g = {
name = 'Git',
r = 'Reset hunk',
s = 'Stage hunk'
}
}, {mode = 'v', prefix = '<leader>'})
----------------
-- nvim-compe --
----------------
-- Use (S-)Tab in completion menu
-- See https://github.com/L3MON4D3/Luasnip/issues/1#issuecomment-835241958
local t = function(str)
return vim.api.nvim_replace_termcodes(str, true, true, true)
end
local function prequire(...)
local status, lib = pcall(require, ...)
if (status) then return lib end
return nil
end
local luasnip = prequire('luasnip')
local check_back_space = function()
local col = vim.fn.col('.') - 1
return col == 0 or vim.fn.getline('.'):sub(col, col):match('%s') ~= nil
end
_G.tab_complete = function()
if vim.fn.pumvisible() == 1 then
return t '<C-n>'
elseif luasnip and luasnip.expand_or_jumpable() then
return t '<Plug>luasnip-expand-or-jump'
elseif check_back_space() then
return t '<Tab>'
else
return vim.fn['compe#complete']()
end
end
_G.s_tab_complete = function()
if vim.fn.pumvisible() == 1 then
return t '<C-p>'
elseif luasnip and luasnip.jumpable(-1) then
return t '<Plug>luasnip-jump-prev'
else
return t '<S-Tab>'
end
end
vim.api.nvim_set_keymap('i', '<Tab>', 'v:lua.tab_complete()', {expr = true, noremap = true, silent = true})
vim.api.nvim_set_keymap('s', '<Tab>', 'v:lua.tab_complete()', {expr = true, noremap = true, silent = true})
vim.api.nvim_set_keymap('i', '<S-Tab>', 'v:lua.s_tab_complete()', {expr = true, noremap = true, silent = true})
vim.api.nvim_set_keymap('s', '<S-Tab>', 'v:lua.s_tab_complete()', {expr = true, noremap = true, silent = true})
vim.api.nvim_set_keymap('i', '<C-Space>', 'compe#complete()', {expr = true, noremap = true, silent = true})
vim.api.nvim_set_keymap('i', '<C-e>', 'compe#close(\'<C-e>\')', {expr = true, noremap = true, silent = true})