From e8fb4e3614b08d8a578288eb0d298e1bbc3f6d6b Mon Sep 17 00:00:00 2001 From: ItsDrike Date: Sun, 5 Dec 2021 22:33:23 +0100 Subject: [PATCH] Move global functions to init.lua --- home/.config/nvim/init.lua | 26 +++- home/.config/nvim/lua/mappings.lua | 116 +++++++----------- .../.config/nvim/lua/plugins.d/commentary.lua | 4 +- home/.config/nvim/lua/plugins.d/nerdtree.lua | 2 +- home/.config/nvim/lua/plugins.lua | 10 +- 5 files changed, 73 insertions(+), 85 deletions(-) diff --git a/home/.config/nvim/init.lua b/home/.config/nvim/init.lua index 20ec5ab..31ccea0 100644 --- a/home/.config/nvim/init.lua +++ b/home/.config/nvim/init.lua @@ -1,7 +1,31 @@ +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 "base" require "theme" require "mappings" require "abbreviations" require "autocmd" require "plugins" - diff --git a/home/.config/nvim/lua/mappings.lua b/home/.config/nvim/lua/mappings.lua index 0a4d1c3..60e881a 100644 --- a/home/.config/nvim/lua/mappings.lua +++ b/home/.config/nvim/lua/mappings.lua @@ -1,113 +1,85 @@ local vim = require("vim") +local g = vim.g --- Make these function definitions global as they could be reused --- in plugin specific scripts or in other places. -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 - -- Perform the actual remap - vim.api.nvim_set_keymap(mode, shortcut, command, opts) -end -- This is a bit silly, but I don't like passing the mode argument --- to every keymap definition and this makes things a lot easier --- and a bit more vimscript-like -function nmap(shortcut, command, options) - keymap("n", shortcut, command, options) -end - -function imap(shortcut, command, options) - keymap("i", shortcut, command, options) -end - -function vmap(shortcut, command, options) - keymap("v", shortcut, command, options) -end - -function tmap(shortcut, command, options) - keymap("t", shortcut, command, options) -end - -function xmap(shortcut, command, options) - keymap("x", shortcut, command, options) -end +g.mapleader = "\\" -- Unmap arrow keys in normal mode to remove bad habits -nmap("", "") -nmap("", "") -nmap("", "") -nmap("", "") +Keymap("n", "", "") +Keymap("n", "", "") +Keymap("n", "", "") +Keymap("n", "", "") -- Tab navigation -nmap("", "gt") -nmap("", "gT") -nmap("", ":tabnew") -nmap("", ":tabmove +") -nmap("", ":tabmove -") -nmap("", ":tabp") -nmap("", ":tabn") +Keymap("n", "", "gt") +Keymap("n", "", "gT") +Keymap("n", "", ":tabnew") +Keymap("n", "", ":tabmove +") +Keymap("n", "", ":tabmove -") +Keymap("n", "", ":tabp") +Keymap("n", "", ":tabn") -- Set splits navigation to just ALT + hjkl -nmap("", "h") -nmap("", "j") -nmap("", "k") -nmap("", "l") +Keymap("n", "", "h") +Keymap("n", "", "j") +Keymap("n", "", "k") +Keymap("n", "", "l") -- Split size adjusting -nmap("", ":verical resize +3") -nmap("", ":vertical resize -3") -nmap("", ":resize +3") -nmap("", ":resize -3") +Keymap("n", "", ":vertical resize +3") +Keymap("n", "", ":vertical resize -3") +Keymap("n", "", ":resize +3") +Keymap("n", "", ":resize -3") -- Define some common shortcuts -nmap("", ":w") -imap("", ":wi") -nmap("", ":undo") -nmap("", ":redo") +Keymap("n", "", ":w") +Keymap("i", "", ":wi") +Keymap("n", "", ":undo") +Keymap("n", "", ":redo") -- Terminal -nmap("", ":split term://zsh:resize -7i") -nmap("", ":vnew term://zshi") -nmap("", ":tabnew term://zshi") -tmap("", "") +Keymap("n", "", ":split term://zsh:resize -7i") +Keymap("n", "", ":vnew term://zshi") +Keymap("n", "", ":tabnew term://zshi") +Keymap("t", "", "") -- Use space for folding/unfolding sections -nmap("", "za") -vmap("", "zf") +Keymap("n", "", "za") +Keymap("v", "", "zf") -- Use shift to quickly move 10 lines up/down -nmap("K", "10k") -nmap("J", "10j") +Keymap("n", "K", "10k") +Keymap("n", "J", "10j") -- Enable/Disable auto commenting -nmap("c", ":setlocal formatoptions-=cro") -nmap("C", ":setlocal formatoptions+=cro") +Keymap("n", "c", ":setlocal formatoptions-=cro") +Keymap("n", "C", ":setlocal formatoptions+=cro") -- Don't leave visual mode after indenting -vmap("<", "", ">gv") +Keymap("v", "<", "", ">gv") -- System clipboard copying -vmap("", '"+y') +Keymap("v", "", '"+y') -- Alias replace all -nmap("", ":%s//gI", {silent=false}) +Keymap("n", "", ":%s//gI", {silent=false}) -- Stop search highlight with Esc -nmap("", ":noh") +Keymap("n", "", ":noh") -- Start spell-check -nmap("s", ":setlocal spell! spelllang=en_us") +Keymap("n", "s", ":setlocal spell! spelllang=en_us") -- Run shell check -nmap("p", ":!shellckeck %") +Keymap("n", "p", ":!shellckeck %") -- Compile opened file (using custom script) -nmap("", ":w | !comp %") +Keymap("n", "", ":w | !comp %") -- Close all opened buffers -nmap("Q", ":bufdo bdelete") +Keymap("n", "Q", ":bufdo bdelete") -- Don't set the incredibely annoying mappings that trigger dynamic SQL -- completion with dbext and keeps on freezing vim whenever pressed with diff --git a/home/.config/nvim/lua/plugins.d/commentary.lua b/home/.config/nvim/lua/plugins.d/commentary.lua index e20acac..804de99 100644 --- a/home/.config/nvim/lua/plugins.d/commentary.lua +++ b/home/.config/nvim/lua/plugins.d/commentary.lua @@ -3,7 +3,7 @@ local cmd = vim.cmd cmd[[Plug 'tpope/vim-commentary']] -nmap("", ":Commentary") -vmap("", ":Commentary") +Keymap("n", "", ":Commentary") +Keymap("v", "", ":Commentary") cmd[[autocmd FileType apache setlocal commentstring=#\ %s]] diff --git a/home/.config/nvim/lua/plugins.d/nerdtree.lua b/home/.config/nvim/lua/plugins.d/nerdtree.lua index bc83d36..a4fbca2 100644 --- a/home/.config/nvim/lua/plugins.d/nerdtree.lua +++ b/home/.config/nvim/lua/plugins.d/nerdtree.lua @@ -11,7 +11,7 @@ Plug 'tiagofumo/vim-nerdtree-syntax-highlight' -- Implement manual NERDTreeToggle, but use NERDTreeFind for openning. -- This makes NERDTree open with the current file pre-selected -nmap("", "g:NERDTree.IsOpen() ? ':NERDTreeClose' : @% == '' ? ':NERDTree' : ':NERDTreeFind'", {expr=true}) +Keymap("n", "", "g:NERDTree.IsOpen() ? ':NERDTreeClose' : @% == '' ? ':NERDTree' : ':NERDTreeFind'", {expr=true}) g.NERDTreeShowHidden = 1 g.NERDTreeMinimalUI = 1 diff --git a/home/.config/nvim/lua/plugins.lua b/home/.config/nvim/lua/plugins.lua index 38389e1..ed43964 100644 --- a/home/.config/nvim/lua/plugins.lua +++ b/home/.config/nvim/lua/plugins.lua @@ -19,18 +19,10 @@ if fn.empty(fn.glob(plug_install_path)) > 0 then cmd[[autocmd VimEnter * UpdateRemotePlugins]] end --- Load an arbitrary .vim or .lua file -local function load_file(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 - -- Load a file containing Plug call(s) and plugin settings local function load_plugin_file(plugin_file) local plugin_path = plugin_files_dir .. "/" .. plugin_file - load_file(plugin_path) + LoadFile(plugin_path) end -- Load a single given plugin using a Plug call