summaryrefslogtreecommitdiffstats
path: root/.config/nvim/lua/core/keymaps.lua
blob: 318cfc7faf217bb24c738e75b5813326bb5b37d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
local map = require("helpers.keys").map

-- https://stackoverflow.com/questions/4256697/vim-search-and-highlight-but-do-not-jump
-- search & highlight but do not jump
map("n", "*", "<cmd>keepjumps normal! mi*`i<CR>", "search forwards")
map("n", "#", "<cmd>keepjumps normal! mi#`i<CR>", "search backwards")

-- save file as sudo on files that require root permission
map("c", "w!!", 'execute "silent! write !sudo tee % >/dev/null" <bar> edit!<CR>', "save file as sudo", { silent = false })

-- replace ex mode with gq (format lines)
map("n", "Q", "gq", "format lines")

-- set formatprg to sentences, for prose
map("n", "<Leader>fp", ":set formatprg=~/.local/bin/sentences<CR>", "switch to sentences formatprg", { silent = false })

-- replace all is aliased to S.
map("n", "<leader>S", ":%s//g<Left><Left>", "replace all", { silent = false })
map("v", "<leader>S", ":s//g<Left><Left>", "replace all", { silent = false })

-- jump to buffer
map("n", "<Leader>b", "<cmd>ls<cr>:b<space>", "jump to buffer")

-- up and down are more logical with g..
map("n", "k", '(v:count == 0 ? "gk" : "k")', "up", { expr = true })
map("n", "j", '(v:count == 0 ? "gj" : "j")', "down", { expr = true })
map("i", "<Up>", "<Esc>gka")
map("i", "<Down>", "<Esc>gja")

-- space used to toggle folds, now it's x (because x is d)
map("n", "<Space>", '"_x')

-- separate cut and delete
map("n", "x", "d")
map("x", "x", "d")
map("n", "xx", "dd")
map("n", "X", "D")

-- change into pwd of current buffer
map("n", "<Leader>cd", "<cmd>lcd %:p:h<CR>:pwd<CR>", "cd to current buffer's pwd")

-- call CreatePaper on word below cursor
map("n", "<leader>np", 'gewi[[/papers/<ESC>Ea]]<ESC>bb:call CreatePaper(expand("<cword>"))<CR>',
    "vimwiki: new paper on word under cursor")

-- link paper
map("n", "<leader>lp", "gewi[[/papers/<ESC>Ea]]<ESC>", "vimwiki: link wrap word under cursor")

-- call CreateReference on word below cursor
map("n", "<leader>nr", '<cmd>call CreateReference(expand("<cword>"))<CR>', "vimwiki: new reference word under cursor")

-- nr2char(10) is, of course, a newline character
map("n", "<leader>yp",
    "<cmd>call setreg('+', expand('%:h') . '/' . expand('%:t') . ':' . line('.') . nr2char(10) .  getline('.'))<CR>",
    "yank the current line contents, filename and line number to clipboard")

-- create a new note
map("n", "<leader>nn", "<cmd>call CreateNote()<CR>", "vimwiki: new note for slipbox")

-- :%% to get current file path
map('c', '%%', "getcmdtype() == ':' ? expand('%:h').'/' : '%%'", "get current file path", { expr = true, silent = false })

-- reselect visual selection after indent
map('v', '>', '>gv')
map('v', '<', '<gv')

-- keep cursor position after visual yank
map('v', 'y', 'myy`y')
map('v', 'Y', 'myY`y')

-- ` is more useful than '
map('n', "'", '`')

-- keep cursor in the middle while doing search
map("n", "n", "nzzzv")
map("n", "N", "Nzzzv")


-- switch between light and dark modes
map("n", "<leader>ut", function()
    if vim.o.background == "dark" then
        vim.o.background = "light"
    else
        vim.o.background = "dark"
    end
end, "toggle between light and dark background")