Use utility module for keymap/abbrev functions

This commit is contained in:
ItsDrike 2021-12-06 22:49:03 +01:00
parent 950d19e1a0
commit 768764a899
No known key found for this signature in database
GPG key ID: FB8CA11A2CF3A843
8 changed files with 110 additions and 116 deletions

View file

@ -1,26 +1,3 @@
local vim = require("vim")
local fn = vim.fn
-- Define some global functions which can then be called
-- in the other required scripts
-- Load an arbitrary .vim or .lua file
function LoadFile(file_path)
local extension = file_path:match("^.+(%..+)$")
local run_cmd
if (extension == ".vim") then run_cmd = "source" else run_cmd = "luafile" end
fn.execute(run_cmd .. " " .. file_path)
end
-- Define a key mapping
function Keymap(mode, shortcut, command, options)
-- Assume silent, noremap unless specified otherwise
local opts = {noremap=true, silent=true}
if options then opts = vim.tbl_extend("force", opts, options) end
vim.api.nvim_set_keymap(mode, shortcut, command, opts)
end
-- Require additional scripts which contain individual configurations
require "options"

View file

@ -1,27 +1,9 @@
local vim = require("vim")
local cmd = vim.cmd
local fn = vim.fn
local m = require("utility.mappings")
-- I'm not aware of abbreviations having direct lua support like mappings,
-- though I'm not certain on that and may be completely wrong, if there is
-- a better way, this is opened to pull requests.
-- TODO: Check direct abbrev lua support
-- Make these function definitions global as they could be reused
-- in plugin specific scripts or in other places.
function abbrev(mode, input, result, reabbrev)
-- Assume noreabbrev unless specified otherwise
reabbrev = reabbrev or false
local command
if reabbrev then
command = mode .. "abbrev"
else
command = mode .. "noreabbrev"
end
cmd(command .. " " .. input .. " " .. result)
end
function cabbrev(input, result, reabbrev)
abbrev("c", input, result, reabbrev)
local function cabbrev(input, result, reabbrev)
m.abbrev("c", input, result, reabbrev)
end
-- Invalid case abbreviations
@ -40,6 +22,5 @@ cabbrev("Qall!", "qall!")
cabbrev("w!!", "w !sudo tee > /dev/null %")
-- Reload lua configuration
-- TODO: Get the path dynamically
cabbrev("reload", "luafile ~/.config/nvim/init.lua")
local initlua = fn.stdpath("config") .. "init.lua"
cabbrev("reload", "luafile " .. initlua)

View file

@ -1,3 +1,4 @@
local m = require("utility.mappings")
local vim = require("vim")
local g = vim.g
@ -6,86 +7,86 @@ local g = vim.g
g.mapleader = "\\"
-- Unmap arrow keys in normal mode to remove bad habits
Keymap("n", "<Down>", "<nop>")
Keymap("n", "<Left>", "<nop>")
Keymap("n", "<Right>", "<nop>")
Keymap("n", "<Up>", "<nop>")
m.keymap("n", "<Down>", "<nop>")
m.keymap("n", "<Left>", "<nop>")
m.keymap("n", "<Right>", "<nop>")
m.keymap("n", "<Up>", "<nop>")
-- Tab navigation
Keymap("n", "<Tab>", "gt")
Keymap("n", "<S-Tab>", "gT")
Keymap("n", "<A-t>", ":tabnew<CR>")
Keymap("n", "<A-2>", ":tabmove +<CR>")
Keymap("n", "<A-1>", ":tabmove -<CR>")
Keymap("n", "<A-p>", ":tabp<CR>")
Keymap("n", "<A-n>", ":tabn<CR>")
Keymap("n", "<A-c>", ":tabc<CR>")
m.keymap("n", "<Tab>", "gt")
m.keymap("n", "<S-Tab>", "gT")
m.keymap("n", "<A-t>", ":tabnew<CR>")
m.keymap("n", "<A-2>", ":tabmove +<CR>")
m.keymap("n", "<A-1>", ":tabmove -<CR>")
m.keymap("n", "<A-p>", ":tabp<CR>")
m.keymap("n", "<A-n>", ":tabn<CR>")
m.keymap("n", "<A-c>", ":tabc<CR>")
-- Buffer navigation
Keymap("n", "<A-N>", ":bn<CR>")
Keymap("n", "<A-P>", ":bp<CR>")
Keymap("n", "<A-d>", ":bd<CR>")
m.keymap("n", "<A-N>", ":bn<CR>")
m.keymap("n", "<A-P>", ":bp<CR>")
m.keymap("n", "<A-d>", ":bd<CR>")
-- Set splits navigation to just ALT + hjkl
Keymap("n", "<C-h>", "<C-w>h")
Keymap("n", "<C-j>", "<C-w>j")
Keymap("n", "<C-k>", "<C-w>k")
Keymap("n", "<C-l>", "<C-w>l")
m.keymap("n", "<C-h>", "<C-w>h")
m.keymap("n", "<C-j>", "<C-w>j")
m.keymap("n", "<C-k>", "<C-w>k")
m.keymap("n", "<C-l>", "<C-w>l")
-- Split size adjusting
Keymap("n", "<C-Left>", ":vertical resize +3<CR>")
Keymap("n", "<C-Right>", ":vertical resize -3<CR>")
Keymap("n", "<C-Up>", ":resize +3<CR>")
Keymap("n", "<C-Down>", ":resize -3<CR>")
m.keymap("n", "<C-Left>", ":vertical resize +3<CR>")
m.keymap("n", "<C-Right>", ":vertical resize -3<CR>")
m.keymap("n", "<C-Up>", ":resize +3<CR>")
m.keymap("n", "<C-Down>", ":resize -3<CR>")
-- Define some common shortcuts
Keymap("n", "<C-s>", ":w<CR>")
Keymap("i", "<C-s>", "<Esc>:w<CR>i")
Keymap("n", "<C-z>", ":undo<CR>")
Keymap("n", "<C-y>", ":redo<CR>")
m.keymap("n", "<C-s>", ":w<CR>")
m.keymap("i", "<C-s>", "<Esc>:w<CR>i")
m.keymap("n", "<C-z>", ":undo<CR>")
m.keymap("n", "<C-y>", ":redo<CR>")
-- Terminal
Keymap("n", "<C-t>", ":split term://zsh<CR>:resize -7<CR>i")
Keymap("n", "<C-A-t>", ":vnew term://zsh<CR>i")
Keymap("n", "<A-T>", ":tabnew term://zsh<CR>i")
Keymap("t", "<Esc>", "<C-\\><C-n>")
m.keymap("n", "<C-t>", ":split term://zsh<CR>:resize -7<CR>i")
m.keymap("n", "<C-A-t>", ":vnew term://zsh<CR>i")
m.keymap("n", "<A-T>", ":tabnew term://zsh<CR>i")
m.keymap("t", "<Esc>", "<C-\\><C-n>")
-- Use space for folding/unfolding sections
Keymap("n", "<space>", "za")
Keymap("v", "<space>", "zf")
m.keymap("n", "<space>", "za")
m.keymap("v", "<space>", "zf")
-- Use shift to quickly move 10 lines up/down
Keymap("n", "K", "10k")
Keymap("n", "J", "10j")
m.keymap("n", "K", "10k")
m.keymap("n", "J", "10j")
-- Enable/Disable auto commenting
Keymap("n", "<leader>c", ":setlocal formatoptions-=cro<CR>")
Keymap("n", "<leader>C", ":setlocal formatoptions+=cro<CR>")
m.keymap("n", "<leader>c", ":setlocal formatoptions-=cro<CR>")
m.keymap("n", "<leader>C", ":setlocal formatoptions+=cro<CR>")
-- Don't leave visual mode after indenting
Keymap("v", "<", "<gv")
Keymap("v", ">", ">gv")
m.keymap("v", "<", "<gv")
m.keymap("v", ">", ">gv")
-- System clipboard copying
Keymap("v", "<C-c>", '"+y')
m.keymap("v", "<C-c>", '"+y')
-- Alias replace all
Keymap("n", "<A-s>", ":%s//gI<Left><Left><Left>", {silent=false})
m.keymap("n", "<A-s>", ":%s//gI<Left><Left><Left>", {silent=false})
-- Stop search highlight with Esc
Keymap("n", "<esc>", ":noh<CR>")
m.keymap("n", "<esc>", ":noh<CR>")
-- Start spell-check
Keymap("n", "<leader>s", ":setlocal spell! spelllang=en_us<CR>")
m.keymap("n", "<leader>s", ":setlocal spell! spelllang=en_us<CR>")
-- Run shell check
Keymap("n", "<leader>p", ":!shellckeck %<CR>")
m.keymap("n", "<leader>p", ":!shellckeck %<CR>")
-- Compile opened file (using custom script)
Keymap("n", "<A-c>", ":w | !comp <c-r>%<CR>")
m.keymap("n", "<A-c>", ":w | !comp <c-r>%<CR>")
-- Close all opened buffers
Keymap("n", "<leader>Q", ":bufdo bdelete<CR>")
m.keymap("n", "<leader>Q", ":bufdo bdelete<CR>")
-- Don't set the incredibely annoying mappings that trigger dynamic SQL
-- completion with dbext and keeps on freezing vim whenever pressed with

View file

@ -1,9 +1,10 @@
local m = require("utility.mappings")
local vim = require("vim")
local cmd = vim.cmd
-- Set up shortcuts to quickly comment some code
Keymap("n", "<A-/>", ":Commentary<CR>")
Keymap("v", "<A-/>", ":Commentary<CR>")
m.keymap("n", "<A-/>", ":Commentary<CR>")
m.keymap("v", "<A-/>", ":Commentary<CR>")
-- Set up comments for unhandled file types
cmd[[autocmd FileType apache setlocal commentstring=#\ %s]]

View file

@ -1,3 +1,4 @@
local m = require("utility.mappings")
local vim = require("vim")
local cmd = vim.cmd
local g = vim.g
@ -26,10 +27,10 @@ command! -bang -nargs=? -complete=dir AllFiles
\ call fzf#run(fzf#wrap('allfiles', fzf#vim#with_preview({ 'dir': <q-args>, 'sink': 'e', 'source': 'rg --files --hidden --no-ignore' }), <bang>0))
]]
Keymap("n", "<leader>f", ":Files<CR>")
Keymap("n", "<leader>F", ":AllFiles<CR>")
Keymap("n", "<leader>b", ":Buffers<CR>")
Keymap("n", "<leader>h", ":History<CR>")
Keymap("n", "<leader>r", ":Rg<CR>")
Keymap("n", "<leader>R", ":Rg<space>", { silent = false })
Keymap("n", "<leader>gb", ":GBranches<CR>")
m.keymap("n", "<leader>f", ":Files<CR>")
m.keymap("n", "<leader>F", ":AllFiles<CR>")
m.keymap("n", "<leader>b", ":Buffers<CR>")
m.keymap("n", "<leader>h", ":History<CR>")
m.keymap("n", "<leader>r", ":Rg<CR>")
m.keymap("n", "<leader>R", ":Rg<space>", { silent = false })
m.keymap("n", "<leader>gb", ":GBranches<CR>")

View file

@ -1,3 +1,4 @@
local m = require("utility.mappings")
local vim = require("vim")
local g = vim.g
local fn = vim.fn
@ -5,7 +6,7 @@ local cmd = vim.cmd
-- Implement manual NERDTreeToggle, but use NERDTreeFind for openning.
-- This makes NERDTree open with the current file pre-selected
Keymap("n", "<C-n>", "g:NERDTree.IsOpen() ? ':NERDTreeClose<CR>' : @% == '' ? ':NERDTree<CR>' : ':NERDTreeFind<CR>'", {expr=true})
m.keymap("n", "<C-n>", "g:NERDTree.IsOpen() ? ':NERDTreeClose<CR>' : @% == '' ? ':NERDTree<CR>' : ':NERDTreeFind<CR>'", {expr=true})
g.NERDTreeShowHidden = 1
g.NERDTreeMinimalUI = 1
@ -31,6 +32,8 @@ autocmd VimEnter * if argc() == 1 && isdirectory(argv()[0]) && !exists('s:std_in
]]
-- Exit Vim if NERDTree is the only window left.
-- WARNING: This causes issues when closing buffers
--[[
cmd[[
autocmd BufEnter * if tabpagenr('$') == 1 && winnr('$') == 1 && exists('b:NERDTree') && b:NERDTree.isTabTree() |
\ quit | endif

View file

@ -1,17 +1,18 @@
local m = require("utility.mappings")
local vim = require("vim")
local cmd = vim.cmd
-- Define dap mappings (:help dap-mapping)
local prefix = ":lua require('dap')."
Keymap("n", "<F5>", prefix .. "continue()<CR>")
Keymap("n", "<F10>", prefix .. "step_over()<CR>")
Keymap("n", "<F11>", prefix .. "step_into()<CR>")
Keymap("n", "<leader><F11>", prefix .. "step_out()<CR>")
Keymap("n", "<F9>", prefix .. "toggle_breakpoint()<CR>")
Keymap("n", "<leader><F9>", prefix .. "set_breakpoint(vim.fn.input('Breakpoint condition: '))<CR>")
Keymap("n", "<F8>", prefix .. "set_breakpoint(nil, nil, vim.fn.input('Log point message: '))<CR>")
Keymap("n", "<leader>di", prefix .. "repl.open()<CR>")
Keymap("n", "<leader>dl", prefix .. "run_last()<CR>")
m.keymap("n", "<F5>", prefix .. "continue()<CR>")
m.keymap("n", "<F10>", prefix .. "step_over()<CR>")
m.keymap("n", "<F11>", prefix .. "step_into()<CR>")
m.keymap("n", "<leader><F11>", prefix .. "step_out()<CR>")
m.keymap("n", "<F9>", prefix .. "toggle_breakpoint()<CR>")
m.keymap("n", "<leader><F9>", prefix .. "set_breakpoint(vim.fn.input('Breakpoint condition: '))<CR>")
m.keymap("n", "<F8>", prefix .. "set_breakpoint(nil, nil, vim.fn.input('Log point message: '))<CR>")
m.keymap("n", "<leader>di", prefix .. "repl.open()<CR>")
m.keymap("n", "<leader>dl", prefix .. "run_last()<CR>")
-- Setup dap for python (requires nvim-dap-python plugin)
require('dap-python').setup('/usr/bin/python') -- Path to python with `debugpy` library installed
@ -19,6 +20,6 @@ require('dap-python').test_runner = 'pytest' -- Use pytest to run unit tests
-- Python mappings
local pyprefix = ":lua require('dap-python')."
Keymap("n", "<leader>dptm", pyprefix .. "test_method()<CR>")
Keymap("n", "<leader>dptc", pyprefix .. "test_class()<CR>")
Keymap("v", "<leader>ds", "<ESC>" .. pyprefix .. "debug_selection()<CR>")
m.keymap("n", "<leader>dptm", pyprefix .. "test_method()<CR>")
m.keymap("n", "<leader>dptc", pyprefix .. "test_class()<CR>")
m.keymap("v", "<leader>ds", "<ESC>" .. pyprefix .. "debug_selection()<CR>")

View file

@ -0,0 +1,29 @@
local vim = require("vim")
local api = vim.api
local cmd = vim.cmd
local M = {}
-- Define a keymap
function M.keymap(mode, shortcut, command, options)
-- Assume silent, noremap unless specified otherwise
local opts = {noremap=true, silent=true}
if options then opts = vim.tbl_extend("force", opts, options) end
api.nvim_set_keymap(mode, shortcut, command, opts)
end
-- Define an abbreviation
function M.abbrev(mode, input, result, reabbrev)
-- Assume noreabbrev unless specified otherwise
reabbrev = reabbrev or false
local command
if reabbrev then
command = mode .. "abbrev"
else
command = mode .. "noreabbrev"
end
cmd(command .. " " .. input .. " " .. result)
end
return M