keybindings.lua 6.1 KB

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