keybindings.lua 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. -- Mode
  2. -- normal_mode = "n",
  3. -- insert_mode = "i",
  4. -- visual_mode = "v",
  5. -- visual_block_mode = "x",
  6. -- term_mode = "t",
  7. -- command_mode = "c",
  8. -- leader key 为空
  9. local uConfig = require("uConfig")
  10. local keys = uConfig.keys
  11. vim.g.mapleader = " "
  12. vim.g.maplocalleader = " "
  13. -- MAC 设置alt 键
  14. alt_send_esc = true
  15. local opt = {
  16. noremap = true,
  17. silent = true,
  18. }
  19. local keymap = vim.keymap
  20. keymap.set("i", "jj", "<ESC>")
  21. -- 本地变量
  22. local map = vim.api.nvim_set_keymap
  23. -- 取消 s 默认功能
  24. map("n", "s", "", opt)
  25. map("n", "sv", ":vsp<CR>", opt)
  26. map("n", "sh", ":sp<CR>", opt)
  27. map("n", "sc", "<C-w>c", opt)
  28. map("n", "so", "<C-w>o", opt)
  29. -- 切换窗口
  30. map("n", "<A-Right>", "<C-w>l", opt)
  31. map("n", "<A-Down>", "<C-w>j", opt)
  32. map("n", "<A-Up>", "<C-w>k", opt)
  33. map("n", "<A-Left>", "<C-w>h", opt)
  34. -- 返回dashboard 主页
  35. map("n", "<leader>h", ":Dashboard<CR>", opt)
  36. -- gitsigns
  37. map("n", "git", ":Gitsigns toggle_current_line_blame<CR>", opt)
  38. -- lazygit
  39. map("n", "<leader>lg", ":LazyGit<CR>", opt)
  40. -- terminal 相关
  41. map("n", "<leader>t", ":FloatermToggle<CR>", opt)
  42. -- 开启/关闭 全屏窗口
  43. map("n", "<leader>s", ":MaximizerToggle<CR>", opt)
  44. -- 开始错误列表
  45. map("n", "<leader>i", ":TroubleToggle<CR>", opt)
  46. -- 代码缩进
  47. map("v", "<", "<gv", opt)
  48. map("v", ">", ">gv", opt)
  49. map("v", "J", ":move '>+1<CR>gv-gv>'", opt)
  50. map("v", "K", ":move '<-2<CR>gv-gv>'", opt)
  51. -- 保存配置
  52. map("n", "<leader>w", ":w<CR>", opt)
  53. map("n", "<leader>wq", ":wqa!<CR>", opt)
  54. -- 退出
  55. map("n", "qq", ":q!<CR>", opt)
  56. map("n", "<leader>q", ":qa!<CR>", opt)
  57. -- 上下滚动浏览
  58. map("n", "<A-j>", "5j", opt)
  59. map("n", "<A-k>", "5k", opt)
  60. map("v", "<A-j>", "5j", opt)
  61. map("v", "<A-k>", "5k", opt)
  62. -- hop 相关
  63. map("n", "<leader>hw", ":HopWord<CR>", opt)
  64. map("n", "<leader>hww", ":HopWordMW<CR>", opt)
  65. -- Telescope 查找文件
  66. -- 文件名查找
  67. map("n", "<leader>f", ":Telescope find_files<CR>", opt)
  68. -- 全局搜索 关键词key 查找
  69. map("n", "<leader>k", ":Telescope live_grep<CR>", opt)
  70. -- git 文件
  71. map("n", "<leader>g", ":Telescope git_files<CR>", opt)
  72. -- project
  73. map("n", "<leader>p", ":Telescope projects<CR>", opt)
  74. -- packer update
  75. map("n", "<leader>u", ":PackerUpdate<CR>", opt)
  76. -- prettier
  77. map("n", "<leader>cf", ":Prettier<CR>", opt)
  78. -- 插件快捷键
  79. local pluginKeys = {}
  80. local lsp = uConfig.lsp
  81. pluginKeys.mapLSP = function(mapbuf)
  82. -- rename
  83. --[[
  84. Lspsaga 替换 rn
  85. mapbuf("n", "<leader>rn", "<cmd>Lspsaga rename<CR>", opt)
  86. --]]
  87. mapbuf("n", lsp.rename, "<cmd>lua vim.lsp.buf.rename()<CR>")
  88. -- code action
  89. --[[
  90. Lspsaga 替换 ca
  91. mapbuf("n", "<leader>ca", "<cmd>Lspsaga code_action<CR>", opt)
  92. --]]
  93. mapbuf("n", lsp.code_action, "<cmd>lua vim.lsp.buf.code_action()<CR>")
  94. -- go xx
  95. --[[
  96. mapbuf('n', 'gd', '<cmd>Lspsaga preview_definition<CR>', opt)
  97. mapbuf("n", "gd", "<cmd>lua vim.lsp.buf.definition()<CR>", opt)
  98. --]]
  99. mapbuf("n", lsp.definition, function()
  100. require("telescope.builtin").lsp_definitions({
  101. initial_mode = "normal",
  102. -- ignore_filename = false,
  103. })
  104. end)
  105. --[[
  106. mapbuf("n", "gh", "<cmd>Lspsaga hover_doc<cr>", opt)
  107. Lspsaga 替换 gh
  108. --]]
  109. mapbuf("n", lsp.hover, "<cmd>lua vim.lsp.buf.hover()<CR>")
  110. --[[
  111. Lspsaga 替换 gr
  112. mapbuf("n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opt)
  113. mapbuf("n", "gr", "<cmd>Lspsaga lsp_finder<CR>", opt)
  114. --]]
  115. mapbuf(
  116. "n",
  117. lsp.references,
  118. "<cmd>lua require'telescope.builtin'.lsp_references(require('telescope.themes').get_ivy())<CR>"
  119. )
  120. if vim.fn.has("nvim-0.8") == 1 then
  121. mapbuf("n", lsp.format, "<cmd>lua vim.lsp.buf.format({ async = true })<CR>")
  122. else
  123. mapbuf("n", lsp.format, "<cmd>lua vim.lsp.buf.formatting()<CR>")
  124. end
  125. --[[
  126. Lspsaga 替换 gp, gj, gk
  127. mapbuf("n", "gp", "<cmd>lua vim.diagnostic.open_float()<CR>", opt)
  128. mapbuf("n", "gj", "<cmd>lua vim.diagnostic.goto_next()<CR>", opt)
  129. mapbuf("n", "gk", "<cmd>lua vim.diagnostic.goto_prev()<CR>", opt)
  130. --]]
  131. -- diagnostic
  132. -- mapbuf("n", "gp", "<cmd>Lspsaga show_line_diagnostics<CR>", opt)
  133. -- mapbuf("n", "gj", "<cmd>Lspsaga diagnostic_jump_next<cr>", opt)
  134. -- mapbuf("n", "gk", "<cmd>Lspsaga diagnostic_jump_prev<cr>", opt)
  135. mapbuf("n", lsp.open_flow, "<cmd>lua vim.diagnostic.open_float()<CR>")
  136. mapbuf("n", lsp.goto_next, "<cmd>lua vim.diagnostic.goto_next()<CR>")
  137. mapbuf("n", lsp.goto_prev, "<cmd>lua vim.diagnostic.goto_prev()<CR>")
  138. -- 未用
  139. -- mapbuf("n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opt)
  140. -- mapbuf("n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opt)
  141. -- mapbuf('n', '<leader>q', '<cmd>lua vim.diagnostic.setloclist()<CR>', opt)
  142. -- mapbuf("n", "<C-k>", "<cmd>lua vim.lsp.buf.signature_help()<CR>", opt)
  143. -- mapbuf('n', '<space>wa', '<cmd>lua vim.lsp.buf.add_workspace_folder()<CR>', opt)
  144. -- mapbuf('n', '<space>wr', '<cmd>lua vim.lsp.buf.remove_workspace_folder()<CR>', opt)
  145. -- mapbuf('n', '<space>wl', '<cmd>lua print(vim.inspect(vim.lsp.buf.list_workspace_folders()))<CR>', opt)
  146. -- mapbuf('n', '<space>D', '<cmd>lua vim.lsp.buf.type_definition()<CR>', opt)
  147. end
  148. -- nvim-tree
  149. map("n", "<leader>e", ":NvimTreeToggle<CR>", opt)
  150. -- 列表快捷键
  151. -- lsp 快捷键
  152. pluginKeys.lspList = function(bufnr)
  153. -- bufmap(bufnr, "n", "<leader>rn", "<cmd>lua vim.lsp.buf.rename()<CR>", opt)
  154. bufmap(bufnr, "n", "<leader>rn", "<cmd>Lspsaga rename<CR>", opt)
  155. -- bufmap(bufnr, "n", "<leader>ca", "<cmd>lua vim.lsp.buf.cade_action()<CR>", opt)
  156. bufmap(bufnr, "n", "<leader>ca", "<cmd>Lspsaga cade_action<CR>", opt)
  157. -- go to
  158. -- bufmap(bufnr, "n", "gd", "<Cmd>lua vim.lsp.buf.definition()<CR>", opt)
  159. bufmap(bufnr, "n", "gd", "<cmd>Lspsaga goto_definition<CR>", opt)
  160. -- bufmap(bufnr, "n", "gd", "<cmd>Lspsaga peek_definition<CR>", opt)
  161. bufmap(bufnr, "n", "gt", "<cmd>Lspsaga goto_type_definition<CR>", opt)
  162. bufmap(bufnr, "n", "gD", "<cmd>lua vim.lsp.buf.declaration()<CR>", opt)
  163. -- bufmap(bufnr, "n", "gh", "<cmd>lua vim.lsp.buf.hover()<CR>", opt)
  164. bufmap(bufnr, "n", "gh", "<cmd>Lspsaga hover_doc<CR>", opt)
  165. bufmap(bufnr, "n", "gi", "<cmd>lua vim.lsp.buf.implementation()<CR>", opt)
  166. -- bufmap(bufnr, "n", "gr", "<cmd>lua vim.lsp.buf.references()<CR>", opt)
  167. bufmap(bufnr, "n", "gr", "<cmd>Lspsaga lsp_finder<CR>", opt)
  168. -- diagnostic
  169. -- bufmap(bufnr, "n", "go", "<cmd>lua vim.diagnostic.open_float()<CR>", opt)
  170. bufmap(bufnr, "n", "go", "<cmd>Lspsaga show_line_diagnostics<CR>", opt)
  171. -- bufmap(bufnr, "n", "gn", "<cmd>lua vim.diagnostic.goto_next()<CR>", opt)
  172. bufmap(bufnr, "n", "gn", "<cmd>Lspsaga diagnostic_jump_next<CR>", opt)
  173. -- bufmap(bufnr, "n", "gp", "<cmd>lua vim.diagnostic.goto_prev()<CR>", opt)
  174. bufmap(bufnr, "n", "gp", "<cmd>Lspsaga diagnostic_jump_prev<CR>", opt)
  175. bufmap(bufnr, "n", "<leader>f", "<cmd>lua vim.lsp.buf.format({ async = true })<CR>", opt)
  176. bufmap(bufnr, "n", [[<M-\>]], "<cmd>Lspsaga term_toggle<CR>", opt)
  177. bufmap(bufnr, "t", [[<M-\>]], "<cmd>Lspsaga term_toggle<CR>", opt)
  178. end
  179. -- nvim-cmp 自动补全
  180. pluginKeys.cmp = function(cmp)
  181. return {
  182. -- 出现补全
  183. ["<A-.>"] = cmp.mapping(cmp.mapping.complete(), { "i", "c" }),
  184. -- 取消
  185. ["<A-,>"] = cmp.mapping({
  186. i = cmp.mapping.abort(),
  187. c = cmp.mapping.close(),
  188. }),
  189. -- 上一个
  190. ["<C-k>"] = cmp.mapping.select_prev_item(),
  191. -- 下一个
  192. ["<C-j>"] = cmp.mapping.select_next_item(),
  193. -- 确认
  194. ["<CR>"] = cmp.mapping.confirm({
  195. select = true,
  196. behavior = cmp.ConfirmBehavior.Replace,
  197. }),
  198. -- 如果窗口内容太多,可以滚动
  199. ["<C-u>"] = cmp.mapping(cmp.mapping.scroll_docs(-4), { "i", "c" }),
  200. ["<C-d>"] = cmp.mapping(cmp.mapping.scroll_docs(4), { "i", "c" }),
  201. }
  202. end
  203. return pluginKeys