Created
March 11, 2024 15:53
-
-
Save LeonardoMor/c895b96a3834152b1686b4c10054a6f5 to your computer and use it in GitHub Desktop.
Mappings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require("nvchad.mappings") | |
local fn = vim.fn | |
local api = vim.api | |
local defer_fn = vim.defer_fn | |
local map = vim.keymap.set | |
map("n", "<leader>fm", function() | |
require("conform").format({ | |
async = true, | |
timeout_ms = 5000, | |
lsp_fallback = true, | |
}) | |
end, { expr = true, desc = "LSP format" }) | |
map("n", "<F8>", "<cmd>Outline<CR>", { desc = "LSP toggle outline" }) | |
map("n", "<leader>ds", "<cmd>lua require('neogen').generate()<CR>", { desc = "LSP generate docstring" }) | |
map("n", "<C-d>", "<C-d>zz", { desc = "General scroll down half page and center the cursor vertically" }) | |
map("n", "<C-u>", "<C-u>zz", { desc = "General Scroll up half page and center the cursor vertically" }) | |
map( | |
"n", | |
"<leader>s", | |
[[:%s/\<<C-r><C-w>\>/<C-r><C-w>/gI<Left><Left><Left>]], | |
{ desc = "General replace word under cursor" } | |
) | |
map("n", "J", "mzJ`z", { desc = "General append the next line to the current but don't move the cursor" }) | |
map("n", "n", "nzzzv", { desc = "General next search result and center the cursor vertically" }) | |
map("n", "N", "Nzzzv", { desc = "General previous search result and center the cursor vertically" }) | |
map({ "n", "v" }, "<leader>y", '"+y', { desc = "General copy to clipboard" }) | |
map({ "n", "v" }, "<leader>Y", '"+Y', { desc = "General copy line to clipboard" }) | |
map({ "i", "n", "v" }, "<S-Insert>", '"+p', { desc = "General paste from clipboard" }) | |
map("n", "<leader>tr", function() | |
require("base46").toggle_transparency() | |
end, { desc = "General toggle transparency", expr = true }) | |
map({ "i", "n", "v" }, "jl", "<Esc>", { desc = "General quick escape" }) | |
map("v", "J", ":m '>+1<CR>gv=gv", { desc = "General move current selection down" }) | |
map("v", "K", ":m '<-2<CR>gv=gv", { desc = "General move current selection up" }) | |
map("n", "<A-j>", "<cmd>cnext<CR>zz", { desc = "Quickfix next item" }) | |
map("n", "<A-j>", "<cmd>cprev<CR>zz", { desc = "Quickfix previous item" }) | |
map("n", "<C-Left>", "<cmd>NvimTmuxNavigateLeft<CR>", { desc = "Tmux window left" }) | |
map("n", "<C-Right>", "<cmd>NvimTmuxNavigateRight<CR>", { desc = "Tmux window right" }) | |
map("n", "<C-Down>", "<cmd>NvimTmuxNavigateDown<CR>", { desc = "Tmux window down" }) | |
map("n", "<C-Up>", "<cmd>NvimTmuxNavigateUp<CR>", { desc = "Tmux window up" }) | |
-- Executes a normal key presses. Used to fallback to the normal behavior of the keys in the keymap | |
local function fallback(key) | |
api.nvim_feedkeys(api.nvim_replace_termcodes("<C-g>u" .. key .. "<C-g>u", true, true, true), "n", true) | |
end | |
-- See if there are Codeium completions. If there are, return them | |
local function getCodeiumCompletions() | |
local status, completions = pcall(function() | |
return api.nvim_eval("b:_codeium_completions.items[b:_codeium_completions.index].completionParts[0].text") | |
end) | |
if not status then | |
return nil | |
end | |
return completions | |
end | |
local function getCodeiumNextWord() | |
local full_completion = getCodeiumCompletions() | |
if not full_completion then | |
return fallback("<Right>") | |
end | |
local word_delimeters = " %(%)%[%]%{%}',\"`|%.%%" | |
local nextword_pattern = "[" .. word_delimeters .. "]*[^" .. word_delimeters .. "]+" | |
return full_completion:match(nextword_pattern) | |
end | |
local function getCodeiumNextLine() | |
local full_completion = getCodeiumCompletions() | |
if not full_completion then | |
return fallback("<C-f>") | |
end | |
return fn.split(full_completion, [[[\n]\zs]])[1] .. "\n" | |
end | |
-- Options for the Codeium mappings | |
local function codeiumOpts(desc) | |
local codeium_opts = { | |
noremap = true, | |
silent = true, | |
expr = true, | |
} | |
codeium_opts.desc = desc | |
return codeium_opts | |
end | |
map("i", "<A-l>", function() | |
return fn["codeium#Accept"]() | |
end, codeiumOpts("Codeium accept suggestion")) | |
map("i", "<A-Bslash>", function() | |
return fn["codeium#Complete"]() | |
end, codeiumOpts("Codeium trigger suggestion")) | |
map("i", "<A-]>", function() | |
return fn["codeium#CycleCompletions"](1) | |
end, codeiumOpts("Codeium cycle completions forwards")) | |
map("i", "<A-[>", function() | |
return fn["codeium#CycleCompletions"](-1) | |
end, codeiumOpts("Codeium cycle completions backwards")) | |
map("i", "<A-BS>", function() | |
return fn["codeium#Clear"]() | |
end, codeiumOpts("Codeium clear completions")) | |
map("i", "<C-l>", getCodeiumNextWord, codeiumOpts("Codeium accept next word")) | |
map("i", "<C-f>", getCodeiumNextLine, codeiumOpts("Codeium accept next line")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment