keybindings.lua 7.6 KB

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