keybindings.lua 7.7 KB

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