--[[ key.lua ]] ------------- Shorten Function Names -------------- local keymap = vim.keymap local map = function(mode, l, r, opts) opts = opts or {} opts.silent = true opts.noremap = true keymap.set(mode, l, r, opts) end local term_opts = { noremap = true, silent = false } --------------- Standard Operations --------------- -- Semi-colon as leader key vim.g.mapleader = ";" -- Jump to next match on line using `.` instead of `;` NOTE: commented out in favour of "ggandor/flit.nvim" --map("n", ".", ";") -- Repeat last command using `` instead of `.` NOTE: commented out in favour of "ggandor/flit.nvim" --map("n", "", ".") -- "jj" to exit insert-mode map("i", "jj", "") -- Reload nvim config map("n", "", "luafile ~/.config/nvim/init.lua | :echom ('Nvim config loading...') | :sl! | echo ('')") --------------- Extended Operations --------------- -- Combine buffers list with buffer name map("n", "b", ":buffers:buffer") -- Buffer confirmation map("n", "y", ":BufferPick") -- Map buffer next, prev and delete to +(n/p/d) respectively map("n", "n", ":bn") map("n", "p", ":bp") map("n", "d", ":bd") -- List marks map("n", "m", ":marks") -- Toggle set number map("n", "$", ":NumbersToggle") map("n", "%", ":NumbersOnOff") -- Easier split navigations, just ctrl-j instead of ctrl-w then j --map("n", "", "") --map("n", "", "") --map("n", "", "") --map("n", "", "") -- Split window map("n", "h", ":split") map("n", "v", ":vsplit") map("n", "c", "c") -- Resize Panes map("n", "+", ":resize +5") map("n", "-", ":resize -5") map("n", "<", ":vertical resize +5") map("n", ">", ":vertical resize -5") map("n", "=", "=") -- Map Alt+(h/j/k/l) in insert mode to move directional map("i", "", "") map("i", "", "") map("i", "", "") map("i", "", "") -- Map Alt+(h/j/k/l) in command mode to move directional map("c", "", "") map("c", "", "") map("c", "", "") map("c", "", "") -- Map Alt+(h/j/k/l) in selection mode to move directional map("s", "", "") map("s", "", "") map("s", "", "") map("s", "", "") -- Create tab, edit and move between them map("n", "n", ":tabnew") map("n", "e", ":tabedit") map("n", "[", ":tabprev") map("n", "]", ":tabnext") -- "Zoom" a split window into a tab and/or close it --map("n", ",", ":tabnew %") --map("n", ".", ":tabclose") -- Vim TABs map("n", "1", "1gt") map("n", "2", "2gt") map("n", "3", "3gt") map("n", "4", "4gt") map("n", "5", "5gt") map("n", "6", "6gt") map("n", "7", "7gt") map("n", "8", "8gt") map("n", "9", "9gt") map("n", "0", "10gt") -- Move to the next and previous item in the quickfixlist --map("n", "]c", "cnext") --map("n", "[c", "cprevious") -- Hitting ESC when inside a terminal to get into normal mode --map("t", "", [[]]) -- Move block (indentation) easily map("n", "<", "<<", term_opts) map("n", ">", ">>", term_opts) map("x", "<", "", ">gv", term_opts) -- Set alt+(j/k) to switch lines of texts or simply move them map("n", "", ':let save_a=@a"add"ap:let @a=save_a') map("n", "", ':let save_a=@a"add"ap:let @a=save_a') -- Search and replace map("v", "sr", 'y:%s/"//gc') -- Map delete to Ctrl+l map("i", "", "") -- Clear screen map("n", "", "!clear") -- Change file to an executable map("n", "x", ":!chmod +x %") -- Paste without replace clipboard map("v", "p", '"_dP') -- Swap two pieces of text, use x to cut in visual mode, then use Ctrl-x in -- visual mode to select text to swap with map("v", "", "`.``gvP``P") -- Clear messages or just refresh/redraw the screen map("n", "u", ":echo '' | redraw") -- Change Working Directory to current project map("n", "cd", ":cd %:p:h:pwd") -- Open the current file in the default program (on Mac this should just be just `open`) map('n', 'o', ':!xdg-open %') -- Unsets the 'last search pattern' register by hitting return --map("n", "", "!silent :noh") -- Toggle completion map("n", "tc", ":lua require('user.utils').toggle_completion()") -- Disable default completion. map('i', '', '') map('i', '', '') -- Set line wrap map("n", "", function() local wrap_status = vim.api.nvim_exec("set wrap ?", true) if wrap_status == "nowrap" then vim.api.nvim_command("set wrap linebreak") print("Wrap enabled") else vim.api.nvim_command("set wrap nowrap") print("Wrap disabled") end end, { silent = true }) -- Toggle between folds --utils.map("n", "", "&foldlevel ? 'zM' : 'zR'", { expr = true }) -- Use space to toggle fold --utils.map("n", "", "za") -- Make a copy of current file vim.cmd([[ map s :up \| saveas! %:p:r-=strftime("%y.%m.%d-%H:%M")-bak.=expand("%:e") \| 3sleep \| e # ]]) -- Toggle transparency vim.cmd([[ let t:is_transparent = 0 function! Toggle_transparent_background() if t:is_transparent == 0 hi Normal guibg=#111111 ctermbg=black let t:is_transparent = 1 else hi Normal guibg=NONE ctermbg=NONE let t:is_transparent = 0 endif endfunction nnoremap tb :call Toggle_transparent_background() ]]) --keymap('n', 'tb', ':Toggle_transparent_background') -- Toggle zoom vim.cmd([[ function! s:ZoomToggle() abort if exists('t:zoomed') && t:zoomed execute t:zoom_winrestcmd let t:zoomed = 0 else let t:zoom_winrestcmd = winrestcmd() resize vertical resize let t:zoomed = 1 endif endfunction command! ZoomToggle call s:ZoomToggle() ]]) map("n", "z", ":ZoomToggle") -- Toggle statusline vim.cmd([[ let s:hidden_all = 0 function! ToggleHiddenAll() if s:hidden_all == 0 let s:hidden_all = 1 set noshowmode set noruler set laststatus=0 set noshowcmd else let s:hidden_all = 0 set showmode set ruler set laststatus=2 set showcmd endif endfunction nnoremap :call ToggleHiddenAll() ]]) -- Open last closed buffer vim.cmd([[ function! OpenLastClosed() let last_buf = bufname('#') if empty(last_buf) echo "No recently closed buffer found" return endif let result = input("Open ". last_buf . " in (n)ormal (v)split, (t)ab or (s)plit ? (n/v/t/s) : ") if empty(result) || (result !=# 'v' && result !=# 't' && result !=# 's' && result !=# 'n') return endif if result ==# 't' execute 'tabnew' elseif result ==# 'v' execute "vsplit" elseif result ==# 's' execute "split" endif execute 'b ' . last_buf endfunction ]]) map("n", "", ":call OpenLastClosed() ") ---------------- Plugin Operations ---------------- -- Packer map("n", "Pc", "PackerCompile") map("n", "Pi", "PackerInstall") map("n", "Ps", "PackerSync") map("n", "PS", "PackerStatus") map("n", "Pu", "PackerUpdate") -- Tmux navigation (aserowy/tmux.nvim) map('n', '', 'NavigatorLeft') map('n', '', 'NavigatorRight') map('n', '', 'NavigatorUp') map('n', '', 'NavigatorDown') -- ToggleTerm --map("n", "tt", "ToggleTerm") -- LazyGit map("n", "gg", ":LazyGit") -- Fugitive git bindings map("n", "ga", ":Git add %:p") --map("n", "gs", ":Gstatus") map("n", "gc", ":Gcommit -v -q") map("n", "gt", ":Gcommit -v -q %:p") --map("n", "gd", ":Gdiff") map("n", "ge", ":Gedit") --map("n", "gr", ":Gread") map("n", "gw", ":Gwrite") map("n", "gl", ":silent! Glog:bot copen") --map("n", "gp", ":Ggrep") --map("n", "gp", ":Git push") --map("n", "gb", ":Gblame") map("n", "gm", ":Gmove") --map("n", "gb", ":Git branch") --map("n", "go", ":Git checkout") --map("n", "gps", ":Dispatch! git push") --map("n", "gpl", ":Dispatch! git pull") -- map[""] = { "ToggleTerm", desc = "Toggle terminal" } -- map["gg"] = { function() toggle_term_cmd "lazygit" end, desc = "ToggleTerm lazygit" } -- map["tn"] = { function() toggle_term_cmd "node" end, desc = "ToggleTerm node" } -- map["tu"] = { function() toggle_term_cmd "ncdu" end, desc = "ToggleTerm NCDU" } -- map["tt"] = { function() toggle_term_cmd "htop" end, desc = "ToggleTerm htop" } -- map["tp"] = { function() toggle_term_cmd "python" end, desc = "ToggleTerm python" } -- map["tl"] = { function() toggle_term_cmd "lazygit" end, desc = "ToggleTerm lazygit" } -- map["tf"] = { "ToggleTerm direction=float", desc = "ToggleTerm float" } -- map["th"] = { "ToggleTerm size=10 direction=horizontal", desc = "ToggleTerm horizontal split" } -- map["tv"] = { "ToggleTerm size=80 direction=vertical", desc = "ToggleTerm vertical split" } --end -- Telescope map("n", "ff", function() require("telescope.builtin").find_files { hidden = true, no_ignore = true } end) -- find all files map("n", "fF", "lua require('telescope.builtin').find_files()") -- find files with hidden option map("n", "fb", "lua require('telescope.builtin').current_buffer_fuzzy_find()") map("n", "fc", "lua require('telescope.builtin').commands()") map("n", "fg", "lua require('telescope.builtin').live_grep()") map("n", "fp", "Telescope pickers") map("n", "fr", "lua require('telescope.builtin').registers({})") -- registers picker map("n", "cf", "Telescope changed_files") map("n", "fd", "lua require('telescope.builtin').diagnostics()") map("n", "fh", "lua require('telescope.builtin').help_tags()") map("n", "fk", "lua require('telescope.builtin').keymaps()") map("n", "fn", [[lua require'plugins.telescope'.find_notes()]]) -- find notes map("n", "fgn", [[lua require'plugins.telescope'.grep_notes()]]) -- search notes map("n", "f.", [[lua require'plugins.telescope'.find_configs()]]) -- find configs map("n", "fm", "lua require('telescope').extensions.media_files.media_files({})") -- find media files -- FZF map("n", "fz", "lua require('fzf-lua').files()") -- Nvim-tree map("n", "f", ":NvimTreeToggle", {}) -- Markdown-preview map("n", "md", "MarkdownPreviewToggle") -- Autopairs Toggle_autopairs = function() local ok, autopairs = pcall(require, "nvim-autopairs") if ok then if autopairs.state.disabled then autopairs.enable() print("autopairs on") else autopairs.disable() print("autopairs off") end else print("autopairs not available") end end map("n", "ww", ":lua Toggle_autopairs()", term_opts) -- Tabularize --vim.cmd([[ -- vnoremap mm ':Tabularize /^\s*\S.*\zs' . split(&commentstring, '%s')[0] . "" -- nnoremap mm ':Tabularize /^\s*\S.*\zs' . split(&commentstring, '%s')[0] . "" -- "nnoremap i mc40A 080lDgelD`cP -- "vnoremap ii mc0f-20i`cdt=j --]]) -- EasyAlign --vim.cmd([[ -- " Start interactive EasyAlign in visual mode (e.g. vipga) -- xmap ga (EasyAlign) -- " Start interactive EasyAlign for a motion/text object (e.g. gaip) -- nmap ga (EasyAlign) -- if !exists('g:easy_align_delimiters') -- let g:easy_align_delimiters = {} -- endif -- let g:easy_align_delimiters['--'] = { 'pattern': '--', 'ignore_groups': ['String'] } -- nnoremap 21A d21\| -- imap a --]])