Move global functions to init.lua

This commit is contained in:
ItsDrike 2021-12-05 22:33:23 +01:00
parent 11baef383b
commit e8fb4e3614
No known key found for this signature in database
GPG key ID: FB8CA11A2CF3A843
5 changed files with 73 additions and 85 deletions

View file

@ -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"

View file

@ -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("<Down>", "<nop>")
nmap("<Left>", "<nop>")
nmap("<Right>", "<nop>")
nmap("<Up>", "<nop>")
Keymap("n", "<Down>", "<nop>")
Keymap("n", "<Left>", "<nop>")
Keymap("n", "<Right>", "<nop>")
Keymap("n", "<Up>", "<nop>")
-- Tab navigation
nmap("<Tab>", "gt")
nmap("<S-Tab>", "gT")
nmap("<A-t>", ":tabnew<CR>")
nmap("<A-2>", ":tabmove +<CR>")
nmap("<A-1>", ":tabmove -<CR>")
nmap("<A-p>", ":tabp<CR>")
nmap("<A-n>", ":tabn<CR>")
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>")
-- Set splits navigation to just ALT + hjkl
nmap("<C-h>", "<C-w>h")
nmap("<C-j>", "<C-w>j")
nmap("<C-k>", "<C-w>k")
nmap("<C-l>", "<C-w>l")
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")
-- Split size adjusting
nmap("<C-Right>", ":verical resize +3<CR>")
nmap("<C-Left>", ":vertical resize -3<CR>")
nmap("<C-Up>", ":resize +3<CR>")
nmap("<C-Down>", ":resize -3<CR>")
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>")
-- Define some common shortcuts
nmap("<C-s>", ":w<CR>")
imap("<C-s>", "<Esc>:w<CR>i")
nmap("<C-z>", ":undo<CR>")
nmap("<C-y>", ":redo<CR>")
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>")
-- Terminal
nmap("<C-t>", ":split term://zsh<CR>:resize -7<CR>i")
nmap("<C-A-t>", ":vnew term://zsh<CR>i")
nmap("<A-T>", ":tabnew term://zsh<CR>i")
tmap("<Esc>", "<C-\\><C-n>")
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>")
-- Use space for folding/unfolding sections
nmap("<space>", "za")
vmap("<space>", "zf")
Keymap("n", "<space>", "za")
Keymap("v", "<space>", "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("<leader>c", ":setlocal formatoptions-=cro<CR>")
nmap("<leader>C", ":setlocal formatoptions+=cro<CR>")
Keymap("n", "<leader>c", ":setlocal formatoptions-=cro<CR>")
Keymap("n", "<leader>C", ":setlocal formatoptions+=cro<CR>")
-- Don't leave visual mode after indenting
vmap("<", "<gv")
vmap(">", ">gv")
Keymap("v", "<", "<gv")
Keymap("v", ">", ">gv")
-- System clipboard copying
vmap("<C-c>", '"+y')
Keymap("v", "<C-c>", '"+y')
-- Alias replace all
nmap("<A-s>", ":%s//gI<Left><Left><Left>", {silent=false})
Keymap("n", "<A-s>", ":%s//gI<Left><Left><Left>", {silent=false})
-- Stop search highlight with Esc
nmap("<esc>", ":noh<CR>")
Keymap("n", "<esc>", ":noh<CR>")
-- Start spell-check
nmap("<leader>s", ":setlocal spell! spelllang=en_us<CR>")
Keymap("n", "<leader>s", ":setlocal spell! spelllang=en_us<CR>")
-- Run shell check
nmap("<leader>p", ":!shellckeck %<CR>")
Keymap("n", "<leader>p", ":!shellckeck %<CR>")
-- Compile opened file (using custom script)
nmap("<A-c>", ":w | !comp <c-r>%<CR>")
Keymap("n", "<A-c>", ":w | !comp <c-r>%<CR>")
-- Close all opened buffers
nmap("<leader>Q", ":bufdo bdelete<CR>")
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

@ -3,7 +3,7 @@ local cmd = vim.cmd
cmd[[Plug 'tpope/vim-commentary']]
nmap("<A-/>", ":Commentary<CR>")
vmap("<A-/>", ":Commentary<CR>")
Keymap("n", "<A-/>", ":Commentary<CR>")
Keymap("v", "<A-/>", ":Commentary<CR>")
cmd[[autocmd FileType apache setlocal commentstring=#\ %s]]

View file

@ -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("<C-n>", "g:NERDTree.IsOpen() ? ':NERDTreeClose<CR>' : @% == '' ? ':NERDTree<CR>' : ':NERDTreeFind<CR>'", {expr=true})
Keymap("n", "<C-n>", "g:NERDTree.IsOpen() ? ':NERDTreeClose<CR>' : @% == '' ? ':NERDTree<CR>' : ':NERDTreeFind<CR>'", {expr=true})
g.NERDTreeShowHidden = 1
g.NERDTreeMinimalUI = 1

View file

@ -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