From c349d1f7998d97f60d3a3945ce8da166b33bf1c8 Mon Sep 17 00:00:00 2001 From: srdusr Date: Wed, 12 Oct 2022 23:55:30 +0200 Subject: Initial commit --- lua/keys.lua | 483 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 483 insertions(+) create mode 100644 lua/keys.lua (limited to 'lua/keys.lua') diff --git a/lua/keys.lua b/lua/keys.lua new file mode 100644 index 0000000..1995976 --- /dev/null +++ b/lua/keys.lua @@ -0,0 +1,483 @@ +--[[ key.lua ]] + +-- Shorten function name +local map = vim.api.nvim_set_keymap + +local function new_desc(d) + return { desc = d } +end + +local d = new_desc + +local opts = { noremap = true, silent = true } + +local term_opts = { silent = true } + +--------------- Standard Operations --------------- +-- Semi-colon as leader key +vim.g.mapleader = ";" +--vim.g.maplocalleader = ";" + +-- "jj" to exit insert-mode +map("i", "jj", "", opts) + +-- save quickly +map("n", ";w", ":w", d("Save buffer")) + +--Easier split navigations, just ctrl-j instead of ctrl-w then j +map("n", "", "", opts) +map("n", "", "", opts) +map("n", "", "", opts) +map("n", "", "", opts) + +-- Combine buffers list with buffer name +map("n", "b", ":buffers:buffer", opts) + +-- Map buffer next, prev and delete to +map("n", "n", ":bn", opts) +map("n", "p", ":bp", opts) +map("n", "d", ":bd", 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', opts) +map("n", "", ':let save_a=@a"add"ap:let @a=save_a', opts) + +-- move block easily +map("n", "<", "<<", d("Decrease indent")) +map("n", ">", ">>", d("Increase indent")) +map("x", "<", "", ">gv", d("Decrease indent")) + +-- Resize Panes +map("n", "+", ":resize +5", opts) +map("n", "-", ":resize -5", opts) +map("n", "<", ":vertical resize +5", opts) +map("n", ">", ":vertical resize -5", opts) +map("n", "=", "=", opts) + +-- New tab +map("n", "e", ":tabedit", opts) + +-- create tab like window +map("n", "h", ":tabprevious", d("Goto previous tab")) +map("n", "l", ":tabnext", d("Goto next tab")) +map("n", "n", ":tabnew", d("Create a new tab")) + +-- Vim TABs +map("n", "1", "1gt", opts) +map("n", "2", "2gt", opts) +map("n", "3", "3gt", opts) +map("n", "4", "4gt", opts) +map("n", "5", "5gt", opts) +map("n", "6", "6gt", opts) +map("n", "7", "7gt", opts) +map("n", "8", "8gt", opts) +map("n", "9", "9gt", opts) +map("n", "0", "10gt", opts) + +-- Split window +map("n", "h", ":split", opts) +map("n", "v", ":vsplit", opts) +map("n", "c", "c", opts) + +-- Toggle set number +map("n", "$", ":NumbersToggle", opts) +map("n", "%", ":NumbersOnOff", opts) + +-- Change mode to executable +map("n", "x", ":!chmod +x %", opts) + +-- Paste without replace clipboard +map("v", "p", '"_dP', opts) + +-- Paste end of line +--map("n", ",", "$p", opts) +vim.cmd([[ + nmap , $p +]]) + +-- Select entire buffer +map("v", "", "ggG", opts) + +-- Delete without changing the registers +--map('n', 'x', '"_x', opts) + +-- Select all text in current buffer +--map('n', 'a', ':keepjumps normal! ggVG') + +-- 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", opts) + +-- Search and replace +map("v", "sr", 'y:%s/"//gc', opts) +--vnoremap ; :call Get_visual_selection() +-- +--function! Get_visual_selection() +-- " Why is this not a built-in Vim script function?! +-- let [lnum1, col1] = getpos("'<")[1:2] +-- let [lnum2, col2] = getpos("'>")[1:2] +-- let lines = getline(lnum1, lnum2) +-- let lines[-1] = lines[-1][: col2 - (&selection == 'inclusive' ? 1 : 2)] +-- let lines[0] = lines[0][col1 - 1:] +-- let selection = join(lines,'\n') +-- let change = input('Change the selection with: ') +-- execute ":%s/".selection."/".change."/g" +--endfunction +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() +]]) + +-------------- Telescope -------------- +--Telescope find_files cwd=.. +map("n", "fc", "lua require('telescope.builtin').commands()", opts) +map( + "n", + "ft", + "lua require('telescope.builtin').builtin(require('telescope.themes').get_dropdown({}))", + opts +) + +map("n", "fg", "lua require('telescope.builtin').live_grep()", opts) +map("n", "fb", "lua require('telescope.builtin').current_buffer_fuzzy_find()", opts) + +--map("n", "fz", ":FZF", opts) +--map("t", [[]], [[]], opts) +--map("n", "ff", ":NvimTreeToggle", {}) +map("n", "ff", ":NvimTreeToggle", {}) +-- This avoids crashing fzf menu running in TERMINAL MODE (:q if you do) +-- Find files in config dirs +--key_map("n", "e", ":lua require('plugins.telescope').find_configs()", opts) +map("n", "f.", "lua require('plugins.telescope').find_configs({})", opts) +map("n", "ft", "lua require('plugins.telescope').file_explorer({})", opts) +map("n", "fd", "lua require('plugins.telescope').find_notes({})", opts) +map("n", "fm", "lua require('telescope').extensions.media_files.media_files({})", opts) +-- registers picker +map("n", "r", "lua require('telescope.builtin').registers({})", opts) +-- find files including gitignored +--keymap( +-- "n", +-- "fg", +-- "lua require('telescope.builtin').find_files({find_command={'fd','--no-ignore-vcs'}})", opts) +-- open available commands & run it +map("n", "fc", "lua require('telescope.builtin').commands({results_title='Commands Results'})", opts) + +-------------- 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) + +-------------- Functions -------------- +-- 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', opts) + +-- 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", opts) +-- "Zoom" a split window into a tab and/or close it +--keymap('n', ',', ':tabnew %', opts) +--keymap('n', '.', ':tabclose', opts) + +-- 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() ", 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 +]]) + +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 +]]) +--:'<,'>EasyAlign /--/ +--EasyAlign /--/ +--:'<,'>Tabularize /-- + +map("n", ",", ":hide", opts) +map("n", ".", ":unhide", opts) + +--" Clean trailing whitespace +--nnoremap ww mz:%s/\s\+$//:let @/=''`z + +-- Save with root permission (not working for now) +--vim.api.nvim_create_user_command('W', 'w !sudo tee > /dev/null %', {}) + +-- Copy and Paste with and +--keymap('n', ' p', (v:register =--= '"' && &clipboard =~ 'unnamed' ? '"*p' : '"' . v:register . 'p')'', opts) +-- Use command :Vb for Visual Block or since is used for Copy +--command! Vb normal! +-- Map to save/edit a root permission/read-only file, only works in +-- traditional vim and not neovim +--keymap('c', 'w!! %!sudo tee > /dev/null', opts) +--" Copying text to the system clipboard. +--" +--" For some reason Vim no longer wants to talk to the OS X pasteboard through "*. +--" Computers are bullshit. +--function! g:FuckingCopyTheTextPlease() +-- let old_z = @z +-- normal! gv"zy +-- call system('pbcopy', @z) +-- let @z = old_z +--endfunction +--noremap p :silent! set paste"*p:set nopaste +--" noremap p mz:r!pbpaste`z +--vnoremap y :call g:FuckingCopyTheTextPlease() + +--" Indent/dedent/autoindent what you just pasted. +--nnoremap > V`]< +--nnoremap > V`]> +--nnoremap =- V`]= + +--" Keep the cursor in place while joining lines +--nnoremap J mzJ`z + +--" Toggle [i]nvisible characters +--nnoremap i :set list! +-- +--" Unfuck my screen +--nnoremap U :syntax sync fromstart:redraw! + +--" Ranger +--nnoremap r :silent !ranger %:h:redraw! +--nnoremap R :silent !ranger:redraw! +-- +--" Insert Mode Completion {{{ +-- +--inoremap +--inoremap +--inoremap +---- Open the current file in the default program (on Mac this should just be just `open`) +--keymap('n', 'x', ':!xdg-open %') + +--keymap("n", "ff", "lua require('telescope.builtin').find_files()", opts) +--keymap("n", "ff", "lua require('telescope.builtin').find_files cwd=..()", opts) +--keymap('n', 'k', ':nohlsearch') +-- +--"This unsets the "last search pattern" register by hitting return +vim.cmd([[ + nnoremap :noh +]]) + +--keymap('n', 'Q', ':bufdo bdelete') +-- +---- Allow gf to open non-existent files +--keymap('', 'gf', ':edit ') +-- +---- Reselect visual selection after indenting +--keymap('v', '<', '', '>gv') +-- +---- Maintain the cursor position when yanking a visual selection +---- http://ddrscott.github.io/blog/2016/yank-without-jank/ +--keymap('v', 'y', 'myy`y') +--keymap('v', 'Y', 'myY`y') +--keymap("n", "", ":q", opts) +--keymap("n", "", ":qa!", opts) + +--" Sort lines +--nnoremap s vip:!sort +--vnoremap s :!sort +-- +--" Tabs +--nnoremap ( :tabprev +--nnoremap ) :tabnext +-- +--" Wrap +--nnoremap W :set wrap! + +--set foldlevelstart=0 +-- +--" Space to toggle folds. +--nnoremap za +--vnoremap za +--" Make zO recursively open whatever fold we're in, even if it's partially open. +--nnoremap zO zczO + +-- Packer +--maps.n["pc"] = { "PackerCompile", desc = "Packer Compile" } +--maps.n["pi"] = { "PackerInstall", desc = "Packer Install" } +--maps.n["ps"] = { "PackerSync", desc = "Packer Sync" } +--maps.n["pS"] = { "PackerStatus", desc = "Packer Status" } +--maps.n["pu"] = { "PackerUpdate", desc = "Packer Update" } +-- NeoTree +--if is_available "neo-tree.nvim" then +-- keymaps.n["e"] = { "Neotree toggle", desc = "Toggle Explorer" } +-- keymaps.n["o"] = { "Neotree focus", desc = "Focus Explorer" } +--end +-- Alpha +--if is_available "alpha-nvim" then maps.n["d"] = { "Alpha", desc = "Alpha Dashboard" } end + +-- Package Manager +-- TODO: v2 rework these key bindings to be more general +--if is_available "mason.nvim" then maps.n["lI"] = { "Mason", desc = "LSP installer" } end +-- Telescope +--if is_available "telescope.nvim" then +-- maps.n["fw"] = { function() require("telescope.builtin").live_grep() end, desc = "Search words" } +-- maps.n["fW"] = { +-- function() +-- require("telescope.builtin").live_grep { +-- additional_args = function(args) return vim.list_extend(args, { "--hidden", "--no-ignore" }) end, +-- } +-- end, +-- desc = "Search words in all files", +-- } +-- maps.n["gt"] = { function() require("telescope.builtin").git_status() end, desc = "Git status" } +-- maps.n["gb"] = { function() require("telescope.builtin").git_branches() end, desc = "Git branches" } +-- maps.n["gc"] = { function() require("telescope.builtin").git_commits() end, desc = "Git commits" } +-- maps.n["ff"] = { function() require("telescope.builtin").find_files() end, desc = "Search files" } +-- maps.n["fF"] = { +-- function() require("telescope.builtin").find_files { hidden = true, no_ignore = true } end, +-- desc = "Search all files", +-- } +-- maps.n["fb"] = { function() require("telescope.builtin").buffers() end, desc = "Search buffers" } +-- maps.n["fh"] = { function() require("telescope.builtin").help_tags() end, desc = "Search help" } +-- maps.n["fm"] = { function() require("telescope.builtin").marks() end, desc = "Search marks" } +-- maps.n["fo"] = { function() require("telescope.builtin").oldfiles() end, desc = "Search history" } +-- maps.n["fc"] = +-- { function() require("telescope.builtin").grep_string() end, desc = "Search for word under cursor" } +-- maps.n["sb"] = { function() require("telescope.builtin").git_branches() end, desc = "Git branches" } +-- maps.n["sh"] = { function() require("telescope.builtin").help_tags() end, desc = "Search help" } +-- maps.n["sm"] = { function() require("telescope.builtin").man_pages() end, desc = "Search man" } +-- maps.n["sn"] = +-- { function() require("telescope").extensions.notify.notify() end, desc = "Search notifications" } +-- maps.n["sr"] = { function() require("telescope.builtin").registers() end, desc = "Search registers" } +-- maps.n["sk"] = { function() require("telescope.builtin").keymaps() end, desc = "Search keymaps" } +-- maps.n["sc"] = { function() require("telescope.builtin").commands() end, desc = "Search commands" } +-- maps.n["ls"] = { +-- function() +-- local aerial_avail, _ = pcall(require, "aerial") +-- if aerial_avail then +-- require("telescope").extensions.aerial.aerial() +-- else +-- require("telescope.builtin").lsp_document_symbols() +-- end +-- end, +-- desc = "Search symbols", +-- } +-- maps.n["lR"] = { function() require("telescope.builtin").lsp_references() end, desc = "Search references" } +-- maps.n["lD"] = { function() require("telescope.builtin").diagnostics() end, desc = "Search diagnostics" } +--end +-- +---- Terminal +--if is_available "toggleterm.nvim" then +-- local toggle_term_cmd = astronvim.toggle_term_cmd +-- maps.n[""] = { "ToggleTerm", desc = "Toggle terminal" } +-- maps.n["gg"] = { function() toggle_term_cmd "lazygit" end, desc = "ToggleTerm lazygit" } +-- maps.n["tn"] = { function() toggle_term_cmd "node" end, desc = "ToggleTerm node" } +-- maps.n["tu"] = { function() toggle_term_cmd "ncdu" end, desc = "ToggleTerm NCDU" } +-- maps.n["tt"] = { function() toggle_term_cmd "htop" end, desc = "ToggleTerm htop" } +-- maps.n["tp"] = { function() toggle_term_cmd "python" end, desc = "ToggleTerm python" } +-- maps.n["tl"] = { function() toggle_term_cmd "lazygit" end, desc = "ToggleTerm lazygit" } +-- maps.n["tf"] = { "ToggleTerm direction=float", desc = "ToggleTerm float" } +-- maps.n["th"] = { "ToggleTerm size=10 direction=horizontal", desc = "ToggleTerm horizontal split" } +-- maps.n["tv"] = { "ToggleTerm size=80 direction=vertical", desc = "ToggleTerm vertical split" } +--end +-- +---- Stay in indent mode +--maps.v["<"] = { ""] = { ">gv", desc = "indent line" } +-- +---- Improved Terminal Mappings +--maps.t[""] = { "", desc = "Terminal normal mode" } +--maps.t["jk"] = { "", desc = "Terminal normal mode" } +--maps.t[""] = { "h", desc = "Terminal left window navigation" } +--maps.t[""] = { "j", desc = "Terminal down window navigation" } +--maps.t[""] = { "k", desc = "Terminal up window navigation" } +--maps.t[""] = { "l", desc = "Terminal right window naviation" } +-- LSP Installer +--if is_available "mason-lspconfig.nvim" then maps.n["li"] = { "LspInfo", desc = "LSP information" } end + +-- ALE: toggle _ALE activity +--keymap('n', 'a',[[:ALEToggle]]) + +--keymap('n', 'cd', ':call fzf#run({'source': 'fd -t d -H . ~', 'sink': 'cd'})', opts) +-- ":lua require('neogen').generate()", opts) +--keymap("n", "ww", ":set wrap!", opts) +-- -- cgit v1.2.3