diff --git a/.github/workflows/format-check.yml b/.github/workflows/format-check.yml
new file mode 100644
index 00000000..5cd723bc
--- /dev/null
+++ b/.github/workflows/format-check.yml
@@ -0,0 +1,25 @@
+name: Format Check
+on:
+ pull_request:
+ paths:
+ - '**.lua'
+ push:
+ branches:
+ - main
+ paths:
+ - '**.lua'
+
+jobs:
+ format-check:
+ runs-on: ubuntu-latest
+ steps:
+ - uses: actions/checkout@v3
+ - name: Install StyLua
+ uses: JohnnyMorganz/stylua-action@v4
+ with:
+ token: ${{ secrets.GITHUB_TOKEN }}
+ version: latest
+ args: false
+ - name: Run stylua
+ run: |
+ stylua --check .
diff --git a/.gitignore b/.gitignore
index 75dbbbba..f8e46c0a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,5 @@ en.utf-8.add.spl
.netrwhist
*.log
.DS_Store
+lazy-lock.json
+.env*
diff --git a/.stylua.toml b/.stylua.toml
index 4b134160..92bda2a1 100644
--- a/.stylua.toml
+++ b/.stylua.toml
@@ -1,4 +1,6 @@
# config for stylua, ref: https://github.com/JohnnyMorganz/StyLua
+syntax = "Lua51"
+column_width = 100
line_endings = "Unix"
indent_type = "Spaces"
indent_width = 2
diff --git a/README.md b/README.md
index 7116a590..c836712f 100644
--- a/README.md
+++ b/README.md
@@ -9,11 +9,8 @@
-
-
-
-
+
@@ -21,9 +18,6 @@
-
-
-
@@ -33,6 +27,9 @@
+
+
+
@@ -62,28 +59,26 @@ and how to set up on different platforms (Linux, macOS, and Windows).
+ Language server protocol (LSP) support via [nvim-lspconfig](https://github.com/neovim/nvim-lspconfig).
+ Git integration via [vim-fugitive](https://github.com/tpope/vim-fugitive).
+ Better escaping from insert mode via [better-escape.vim](https://github.com/nvim-zh/better-escape.vim).
-+ Ultra-fast project-wide fuzzy searching via [LeaderF](https://github.com/Yggdroot/LeaderF).
++ Ultra-fast project-wide fuzzy searching via [fzf-lua](https://github.com/ibhagwan/fzf-lua).
+ Faster code commenting via [vim-commentary](https://github.com/tpope/vim-commentary).
+ Faster matching pair insertion and jump via [nvim-autopairs](https://github.com/windwp/nvim-autopairs).
+ Smarter and faster matching pair management (add, replace or delete) via [vim-sandwich](https://github.com/machakann/vim-sandwich).
-+ Fast buffer jump via [hop.nvim](https://github.com/phaazon/hop.nvim).
++ Fast buffer jump via [hop.nvim](https://github.com/smoka7/hop.nvim).
+ Powerful snippet insertion via [Ultisnips](https://github.com/SirVer/ultisnips).
+ Beautiful statusline via [lualine.nvim](https://github.com/nvim-lualine/lualine.nvim).
+ File tree explorer via [nvim-tree.lua](https://github.com/nvim-tree/nvim-tree.lua).
+ Better quickfix list with [nvim-bqf](https://github.com/kevinhwang91/nvim-bqf).
+ Show search index and count with [nvim-hlslens](https://github.com/kevinhwang91/nvim-hlslens).
-+ Command line auto-completion via [wilder.nvim](https://github.com/gelguy/wilder.nvim).
+ User-defined mapping hint via [which-key.nvim](https://github.com/folke/which-key.nvim).
+ Asynchronous code execution via [asyncrun.vim](https://github.com/skywind3000/asyncrun.vim).
+ Code highlighting via [nvim-treesitter](https://github.com/nvim-treesitter/nvim-treesitter).
+ Code editing using true nvim inside browser via [firenvim](https://github.com/glacambre/firenvim).
+ Beautiful colorscheme via [sainnhe/gruvbox-material](https://github.com/sainnhe/gruvbox-material) and other colorschemes.
-+ Markdown writing and previewing via [vim-markdown](https://github.com/preservim/vim-markdown) and [markdown-preview.nvim](https://github.com/iamcco/markdown-preview.nvim).
++ Markdown previewing via [render-markdown.nvim](https://github.com/MeanderingProgrammer/render-markdown.nvim)
+ LaTeX editing and previewing via [vimtex](https://github.com/lervag/vimtex)
+ Animated GUI style notification via [nvim-notify](https://github.com/rcarriga/nvim-notify).
+ Tags navigation via [vista](https://github.com/liuchengxu/vista.vim).
-+ Code formatting via [Neoformat](https://github.com/sbdchd/neoformat).
-+ Undo management via [vim-mundo](https://github.com/simnalamburt/vim-mundo)
++ Code folding with [nvim-ufo](https://github.com/kevinhwang91/nvim-ufo) and [statuscol.nvim](https://github.com/luukvbaal/statuscol.nvim)
+ ......
# UI Demo
@@ -96,10 +91,10 @@ For more UI demos, check [here](https://github.com/jdhao/nvim-config/issues/15).
-## File fuzzy finding using LeaderF
+## File fuzzy finding using fzf-lua
-
+
## Code autocompletion with nvim-cmp
@@ -114,12 +109,6 @@ For more UI demos, check [here](https://github.com/jdhao/nvim-config/issues/15).
-## Command-line autocompletion with wilder.nvim
-
-
-
-
-
## Tags
@@ -140,6 +129,12 @@ Go to a string starting with `se`
+## code folding with nvim-ufo and statuscol.nvim
+
+
+
+
+
# Shortcuts
Some of the shortcuts I use frequently are listed here. In the following shortcuts, `` represents ASCII character `,`.
@@ -155,7 +150,6 @@ Some of the shortcuts I use frequently are listed here. In the following shortcu
| `v` | Normal | Linux/macOS/Win | Reselect last pasted text |
| `ev` | Normal | Linux/macOS/Win | Edit Nvim config in a new tabpage |
| `sv` | Normal | Linux/macOS/Win | Reload Nvim config |
-| `st` | Normal | Linux/macOS/Win | Show highlight group for cursor text |
| `q` | Normal | Linux/macOS/Win | Quit current window |
| `Q` | Normal | Linux/macOS/Win | Quit all window and close Nvim |
| `w` | Normal | Linux/macOS/Win | Save current buffer content |
@@ -165,12 +159,14 @@ Some of the shortcuts I use frequently are listed here. In the following shortcu
| `t` | Normal | Linux/macOS/Win | Toggle tag window (show project tags in the right window) |
| `gs` | Normal | Linux/macOS/Win | Show Git status result |
| `gw` | Normal | Linux/macOS/Win | Run Git add for current file |
-| `gd` | Normal | Linux/macOS/Win | Run git diff for current file |
| `gc` | Normal | Linux/macOS/Win | Run git commit |
| `gpl` | Normal | Linux/macOS/Win | Run git pull |
| `gpu` | Normal | Linux/macOS/Win | Run git push |
-| `gl` | Normal/Visual | Linux/macOS/Win | Get perm link for current/visually-select lines
-| `gb` | Normal | macOS | Browse current git repo in browser
+| `gbd` | Normal | Linux/macOS/Win | Delete a branch |
+| `gbn` | Normal | Linux/macOS/Win | Create a new branch |
+| `gl` | Normal/Visual | Linux/macOS/Win | Get perm link for current/visually-select lines |
+| `gbr` | Normal | macOS | Browse current git repo in browser |
+| `gb` | Visual | macOS | Blame current line |
| `` | Normal | Linux/macOS/Win | Compile&run current source file (for C++, LaTeX, Lua, Python) |
| `` | Normal | Linux/macOS/Win | Toggle spell checking |
| `` | Normal | Linux/macOS/Win | Toggle paste mode |
@@ -180,22 +176,23 @@ Some of the shortcuts I use frequently are listed here. In the following shortcu
| `{operator}iB` | Normal | Linux/macOS/Win | Operate in the whole buffer, `{operator}` can be `v`, `y`, `c`, `d` etc. |
| `Alt-k` | Normal | Linux/macOS/Win | Move current line or selected lines up |
| `Alt-j` | Normal | Linux/macOS/Win | Move current line or selected lines down |
-| `Alt-m` | Normal | macOS/Win | Markdown previewing in system browser |
-| `Alt-Shift-m` | Normal | macOS/Win | Stopping Markdown previewing in system browser |
-| `ob` | Normal/Visual | macOS/Win | Open link under cursor or search visual selection |
| `ctrl-u` | Insert | Linux/macOS/Win | Turn word under cursor to upper case |
| `ctrl-t` | Insert | Linux/macOS/Win | Turn word under cursor to title case |
| `jk` | Insert | Linux/macOS/Win | Return to Normal mode without lagging |
+| `ZR` | Normal | Linux/macOS/Win | Restart nvim without quitting |
+
# Custom commands
In addition to commands provided by various plugins, I have also created some custom commands for personal use.
-| command | description | example |
-|------------|-------------------------------------------------------------------------|--------------------------------|
-| `Redir` | capture command output to a tabpage for easier inspection. | `Redir hi` |
-| `Edit` | edit multiple files at the same time, supports globing | `Edit *.vim` |
-| `Datetime` | print current date and time or convert Unix time stamp to date and time | `Datetime 12345` or `Datetime` |
+| command | description | example |
+|--------------|-------------------------------------------------------------------------|--------------------------------|
+| `Redir` | capture command output to a tabpage for easier inspection. | `Redir hi` |
+| `Edit` | edit multiple files at the same time, supports globing | `Edit *.vim` |
+| `Datetime` | print current date and time or convert Unix time stamp to date and time | `Datetime 12345` or `Datetime` |
+| `JSONFormat` | format a JSON file | `JSONFormat` |
+| `CopyPath` | copy current file path to clipboard | `CopyPath relative` |
# Contributing
@@ -209,10 +206,4 @@ If you still have an issue, [open a new issue](https://github.com/jdhao/nvim-con
# Further readings
Some of the resources that I find helpful in mastering Nvim is documented [here](docs/nvim_resources.md).
-You may also be interested in my posts on configuring Nvim:
-
-+ My nvim notes can be found [here](https://jdhao.github.io/categories/Nvim/)
-+ [Using Neovim for Three years](https://jdhao.github.io/2021/12/31/using_nvim_after_three_years/)
-+ [Config nvim on Linux for Python development](https://jdhao.github.io/2018/12/24/centos_nvim_install_use_guide_en/)
-+ [Nvim config on Windows 10](https://jdhao.github.io/2018/11/15/neovim_configuration_windows/)
-+ [Nvim-qt config on Windows 10](https://jdhao.github.io/2019/01/17/nvim_qt_settings_on_windows/)
+You may also be interested in my posts on configuring Nvim [here](https://jdhao.github.io/categories/Nvim/).
diff --git a/after/ftplugin/go.lua b/after/ftplugin/go.lua
new file mode 100644
index 00000000..d28b7db3
--- /dev/null
+++ b/after/ftplugin/go.lua
@@ -0,0 +1,20 @@
+local opt = vim.opt
+
+-- General tab settings
+opt.tabstop = 4 -- Number of visual spaces per TAB
+opt.softtabstop = 4 -- Number of spaces in tab when editing
+opt.shiftwidth = 4 -- Number of spaces to use for autoindent
+opt.expandtab = false -- Expand tab to spaces so that tabs are spaces
+
+-- gofumpt adds more rule to gofmt, and is compatible with gofmt,
+-- so also https://github.com/mvdan/gofumpt
+vim.keymap.set("n", "f", function()
+ vim.cmd([[silent !gofumpt -w %]])
+end, { buffer = true, silent = true })
+
+vim.keymap.set("n", "", function()
+ vim.cmd([[!go run %]])
+end, {
+ buffer = true,
+ silent = true,
+})
diff --git a/after/ftplugin/help.lua b/after/ftplugin/help.lua
new file mode 100644
index 00000000..2422dc65
--- /dev/null
+++ b/after/ftplugin/help.lua
@@ -0,0 +1,9 @@
+local win_width = vim.api.nvim_win_get_width(0)
+
+-- do not change layout if the screen is not wide enough
+if win_width < 200 then
+ return
+end
+
+-- L means to put window to leftmost
+vim.cmd.wincmd([[L]])
diff --git a/after/ftplugin/json.lua b/after/ftplugin/json.lua
new file mode 100644
index 00000000..3bcf7051
--- /dev/null
+++ b/after/ftplugin/json.lua
@@ -0,0 +1,4 @@
+vim.keymap.set({ "n", "v" }, "f", ":JSONFormat", {
+ buffer = true,
+ silent = true,
+})
diff --git a/after/ftplugin/json.vim b/after/ftplugin/json.vim
deleted file mode 100644
index 90db3ec7..00000000
--- a/after/ftplugin/json.vim
+++ /dev/null
@@ -1,6 +0,0 @@
-" let the initial folding state be that all folds are closed.
-set foldlevel=0
-
-" Use nvim-treesitter for folding
-set foldmethod=expr
-set foldexpr=nvim_treesitter#foldexpr()
diff --git a/after/ftplugin/lua.lua b/after/ftplugin/lua.lua
new file mode 100644
index 00000000..ce4f1806
--- /dev/null
+++ b/after/ftplugin/lua.lua
@@ -0,0 +1,11 @@
+-- Disable inserting comment leader after hitting o/O/
+vim.opt_local.formatoptions:remove { "o", "r" }
+
+vim.keymap.set("n", "", "luafile %", {
+ buffer = true,
+ silent = true,
+})
+vim.keymap.set("n", "f", "silent !stylua %", {
+ buffer = true,
+ silent = true,
+})
diff --git a/after/ftplugin/lua.vim b/after/ftplugin/lua.vim
deleted file mode 100644
index 910c31da..00000000
--- a/after/ftplugin/lua.vim
+++ /dev/null
@@ -1,9 +0,0 @@
-" Disable inserting comment leader after hitting o or O or
-set formatoptions-=o
-set formatoptions-=r
-
-nnoremap :luafile %
-
-" Use nvim-treesitter for folding
-set foldmethod=expr
-set foldexpr=nvim_treesitter#foldexpr()
diff --git a/after/ftplugin/man.lua b/after/ftplugin/man.lua
new file mode 100644
index 00000000..b4806d17
--- /dev/null
+++ b/after/ftplugin/man.lua
@@ -0,0 +1,4 @@
+-- q to quit quickly
+vim.keymap.set("n", "q", "q", {
+ desc = "quit nvim",
+})
diff --git a/after/ftplugin/markdown.lua b/after/ftplugin/markdown.lua
new file mode 100644
index 00000000..6fb7463c
--- /dev/null
+++ b/after/ftplugin/markdown.lua
@@ -0,0 +1,107 @@
+local function add_reference_at_end(label, url, title)
+ vim.schedule(function()
+ local bufnr = vim.api.nvim_get_current_buf()
+ local line_count = vim.api.nvim_buf_line_count(bufnr)
+
+ -- Prepare reference definition
+ local ref_def = "[" .. label .. "]: " .. url
+ if title and title ~= "" then
+ ref_def = ref_def .. ' "' .. title .. '"'
+ end
+
+ -- Check if references section exists
+ local buffer_lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
+
+ local has_ref_section = false
+ for _, line in ipairs(buffer_lines) do
+ if line:match("^%s*[%s]*$") then
+ has_ref_section = true
+ break
+ end
+ end
+
+ local lines_to_add = {}
+ -- Add references header if it doesn't exist
+ if not has_ref_section then
+ if #lines_to_add == 0 then
+ table.insert(lines_to_add, "")
+ end
+ table.insert(lines_to_add, "")
+ end
+
+ table.insert(lines_to_add, ref_def)
+
+ -- Insert at buffer end
+ vim.api.nvim_buf_set_lines(bufnr, line_count, line_count, false, lines_to_add)
+ end)
+end
+
+local function get_ref_link_labels()
+ local labels = {}
+ local seen = {} -- To avoid duplicates
+ local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
+
+ for _, line in ipairs(lines) do
+ -- Pattern explanation:
+ -- %[.-%] matches [link text] (non-greedy)
+ -- %[(.-)%] matches [label] and captures the label content
+ local start_pos = 1
+ while start_pos <= #line do
+ local match_start, match_end, label = string.find(line, "%[.-%]%[(.-)%]", start_pos)
+ if not match_start then
+ break
+ end
+
+ -- Only add unique labels
+ if label and label ~= "" and not seen[label] then
+ table.insert(labels, label)
+ seen[label] = true
+ end
+
+ start_pos = match_end + 1
+ end
+ end
+
+ return labels
+end
+
+local function count_consecutive_spaces(str)
+ -- Remove leading spaces first
+ local trimmed = str:match("^%s*(.*)")
+ local count = 0
+
+ -- Count each sequence of one or more consecutive spaces
+ for spaces in trimmed:gmatch("%s+") do
+ count = count + 1
+ end
+ return count
+end
+
+vim.api.nvim_buf_create_user_command(0, "AddRef", function(opts)
+ local args = vim.split(opts.args, " ", { trimempty = true })
+
+ if #args < 2 then
+ vim.print("Usage: :AddRef ")
+ return
+ end
+
+ local label = args[1]
+ local url = args[2]
+
+ add_reference_at_end(label, url, "")
+end, {
+ desc = "Add reference link at buffer end",
+ nargs = "+",
+ complete = function(arg_lead, cmdline, curpos)
+ -- vim.print(string.format("arg_lead: '%s', cmdline: '%s', curpos: %d", arg_lead, cmdline, curpos))
+
+ -- only complete the first argument
+ if count_consecutive_spaces(cmdline) > 1 then
+ -- we are now starting the second argument, so no completion anymore
+ return {}
+ end
+
+ local ref_link_labels = get_ref_link_labels()
+ return ref_link_labels
+ end,
+})
diff --git a/after/ftplugin/markdown.vim b/after/ftplugin/markdown.vim
index efea5e5a..9e4f2920 100644
--- a/after/ftplugin/markdown.vim
+++ b/after/ftplugin/markdown.vim
@@ -47,7 +47,7 @@ function! AddListSymbol(type, ...) abort
endfunction
" Add hard line breaks for Markdown
-nnoremap \ :set operatorfunc=AddLineBreakg@
+" nnoremap \ :set operatorfunc=AddLineBreakg@
xnoremap \ : call AddLineBreak(visualmode(), 1)
function! AddLineBreak(type, ...) abort
diff --git a/after/ftplugin/python.lua b/after/ftplugin/python.lua
new file mode 100644
index 00000000..9314539a
--- /dev/null
+++ b/after/ftplugin/python.lua
@@ -0,0 +1,75 @@
+local utils = require("utils")
+local opt = vim.opt
+
+opt.wrap = false
+opt.sidescroll = 5
+opt.sidescrolloff = 2
+opt.colorcolumn = "100"
+
+opt.tabstop = 4 -- Number of visual spaces per TAB
+opt.softtabstop = 4 -- Number of spaces in tab when editing
+opt.shiftwidth = 4 -- Number of spaces to use for autoindent
+opt.expandtab = true -- Expand tab to spaces so that tabs are spaces
+
+-- when we run `:compiler ruff`, then followed by `:make`,
+-- Nvim will run ruff in the current directory. By default, `--preview` option is used.
+-- The following option is used to customize the option passed to ruff.
+vim.g.ruff_makeprg_params = ""
+
+local get_proj_root = function()
+ local project_marker = { ".git", "pyproject.toml" }
+ local project_root = vim.fs.root(0, project_marker)
+
+ return project_root
+end
+
+local get_py_env = function()
+ local project_root = get_proj_root()
+ if project_root == nil then
+ return
+ end
+
+ local venv_name = utils.get_virtual_env()
+
+ if venv_name ~= "" then
+ return "plain_venv"
+ end
+
+ -- check if this is uv-managed project
+ local uv_lock_path = vim.fs.joinpath(project_root, "uv.lock")
+ if vim.fn.filereadable(uv_lock_path) == 1 then
+ return "uv"
+ end
+
+ return ""
+end
+
+local py_env = get_py_env()
+
+if vim.fn.exists(":AsyncRun") == 2 then
+ local py_cmd = "python"
+
+ if py_env == "uv" then
+ py_cmd = "uv run python"
+ end
+
+ local rhs = string.format(":AsyncRun %s -u %%", py_cmd)
+
+ vim.keymap.set("n", "", rhs, {
+ buffer = true,
+ silent = true,
+ })
+end
+
+-- format current file
+
+local py_fmt_cmd = "!black"
+if py_env == "uv" then
+ py_fmt_cmd = "!uv run black"
+end
+
+local rhs = string.format("silent %s %%", py_fmt_cmd)
+vim.keymap.set("n", "f", rhs, {
+ buffer = true,
+ silent = true,
+})
diff --git a/after/ftplugin/python.vim b/after/ftplugin/python.vim
deleted file mode 100644
index a9f625c4..00000000
--- a/after/ftplugin/python.vim
+++ /dev/null
@@ -1,18 +0,0 @@
-if exists(':AsyncRun')
- nnoremap :AsyncRun python -u "%"
-endif
-
-" Do not wrap Python source code.
-set nowrap
-set sidescroll=5
-set sidescrolloff=2
-set colorcolumn=100
-
-set tabstop=4 " number of visual spaces per TAB
-set softtabstop=4 " number of spaces in tab when editing
-set shiftwidth=4 " number of spaces to use for autoindent
-set expandtab " expand tab to spaces so that tabs are spaces
-
-" Use nvim-treesitter for folding
-set foldmethod=expr
-set foldexpr=nvim_treesitter#foldexpr()
diff --git a/after/lsp/clangd.lua b/after/lsp/clangd.lua
new file mode 100644
index 00000000..3edff2b3
--- /dev/null
+++ b/after/lsp/clangd.lua
@@ -0,0 +1,4 @@
+---@type vim.lsp.Config
+return {
+ filetypes = { "c", "cpp", "cc" },
+}
diff --git a/after/lsp/gopls.lua b/after/lsp/gopls.lua
new file mode 100644
index 00000000..3a2cb7de
--- /dev/null
+++ b/after/lsp/gopls.lua
@@ -0,0 +1,19 @@
+-- settings for gopls can be found in https://go.dev/gopls/settings
+---@type vim.lsp.Config
+return {
+ settings = {
+ gopls = {
+ usePlaceholders = true,
+ analyses = {
+ unusedparams = true,
+ },
+ staticcheck = true,
+ gofumpt = true,
+ -- inlayHints settings, see https://go.dev/gopls/inlayHints
+ hints = {
+ compositeLiteralFields = true,
+ parameterNames = true,
+ },
+ },
+ },
+}
diff --git a/after/lsp/lua_ls.lua b/after/lsp/lua_ls.lua
new file mode 100644
index 00000000..ad7512a0
--- /dev/null
+++ b/after/lsp/lua_ls.lua
@@ -0,0 +1,21 @@
+-- settings for lua-language-server can be found on https://luals.github.io/wiki/settings/
+---@type vim.lsp.Config
+return {
+ ---@type lspconfig.settings.lua_ls
+ settings = {
+ Lua = {
+ runtime = {
+ -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
+ version = "LuaJIT",
+ },
+ hint = {
+ enable = true,
+ },
+ workspace = {
+ library = {
+ vim.env.VIMRUNTIME .. "/lua",
+ },
+ },
+ },
+ },
+}
diff --git a/after/lsp/pyright.lua b/after/lsp/pyright.lua
new file mode 100644
index 00000000..c6ba1d3d
--- /dev/null
+++ b/after/lsp/pyright.lua
@@ -0,0 +1,53 @@
+-- For what diagnostic is enabled in which type checking mode, check doc:
+-- https://github.com/microsoft/pyright/blob/main/docs/configuration.md#diagnostic-settings-defaults
+-- Currently, the pyright also has some issues displaying hover documentation:
+-- https://www.reddit.com/r/neovim/comments/1gdv1rc/what_is_causeing_the_lsp_hover_docs_to_looks_like/
+
+local new_capability = {
+ -- this will remove some of the diagnostics that duplicates those from ruff, idea taken and adapted from
+ -- here: https://github.com/astral-sh/ruff-lsp/issues/384#issuecomment-1989619482
+ textDocument = {
+ publishDiagnostics = {
+ tagSupport = {
+ valueSet = { 2 },
+ },
+ },
+ hover = {
+ contentFormat = { "plaintext" },
+ dynamicRegistration = true,
+ },
+ },
+}
+
+---@type vim.lsp.Config
+return {
+ -- cmd = { "delance-langserver", "--stdio" },
+ ---@type lspconfig.settings.pyright
+ settings = {
+ pyright = {
+ -- disable import sorting and use Ruff for this
+ disableOrganizeImports = true,
+ disableTaggedHints = false,
+ },
+ python = {
+ analysis = {
+ autoSearchPaths = true,
+ diagnosticMode = "workspace",
+ typeCheckingMode = "standard",
+ useLibraryCodeForTypes = true,
+ -- we can this setting below to redefine some diagnostics
+ diagnosticSeverityOverrides = {
+ deprecateTypingAliases = false,
+ },
+ -- inlay hint settings are provided by pylance?
+ inlayHints = {
+ callArgumentNames = "partial",
+ functionReturnTypes = true,
+ pytestParameters = true,
+ variableTypes = true,
+ },
+ },
+ },
+ },
+ capabilities = new_capability,
+}
diff --git a/after/lsp/ruff.lua b/after/lsp/ruff.lua
new file mode 100644
index 00000000..523c7361
--- /dev/null
+++ b/after/lsp/ruff.lua
@@ -0,0 +1,9 @@
+---@type vim.lsp.Config
+return {
+ init_options = {
+ -- the settings can be found here: https://docs.astral.sh/ruff/editors/settings/
+ settings = {
+ organizeImports = true,
+ },
+ },
+}
diff --git a/autoload/utils.vim b/autoload/utils.vim
index a004bd58..a157d25e 100644
--- a/autoload/utils.vim
+++ b/autoload/utils.vim
@@ -10,23 +10,6 @@ function! s:Single_quote(str) abort
return "'" . substitute(copy(a:str), "'", "''", 'g') . "'"
endfunction
-" Check the syntax group in the current cursor position, see
-" https://stackoverflow.com/q/9464844/6064933 and
-" https://jordanelver.co.uk/blog/2015/05/27/working-with-vim-colorschemes/
-function! utils#SynGroup() abort
- if !exists('*synstack')
- return
- endif
- echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
-endfunction
-
-" Check if a colorscheme exists in runtimepath.
-" The following two functions are inspired by https://stackoverflow.com/a/5703164/6064933.
-function! utils#HasColorscheme(name) abort
- let l:pat = printf('colors/%s.vim', a:name)
- return !empty(globpath(&runtimepath, l:pat))
-endfunction
-
" Custom fold expr, adapted from https://vi.stackexchange.com/a/9094/15292
function! utils#VimFolds(lnum) abort
" get content of current line and the line below
@@ -111,21 +94,6 @@ function! utils#MoveSelection(direction) abort
endfunction
-function! utils#Get_titlestr() abort
- let l:title_str = ''
- if g:is_linux
- let l:title_str = hostname() . ' '
- endif
-
- let l:buf_path = expand('%:p:~')
- let l:title_str = l:title_str . l:buf_path . ' '
- if &buflisted && l:buf_path != ""
- let l:title_str = l:title_str . strftime('%Y-%m-%d %H:%M:%S%z', getftime(expand('%')))
- endif
-
- return l:title_str
-endfunction
-
" Output current time or unix timestamp in human-readable format.
function! utils#iso_time(timestamp) abort
" Get current datetime
@@ -146,27 +114,6 @@ function! utils#iso_time(timestamp) abort
endfunction
-" Check if we are inside a Git repo.
-function! utils#Inside_git_repo() abort
- let res = system('git rev-parse --is-inside-work-tree')
- if match(res, 'true') == -1
- return v:false
- else
- " Manually trigger a special user autocmd InGitRepo (used lazyloading.
- doautocmd User InGitRepo
- return v:true
- endif
-endfunction
-
-function! utils#GetGitBranch()
- let l:res = systemlist('git rev-parse --abbrev-ref HEAD')[0]
- if match(l:res, 'fatal') != -1
- return ''
- else
- return l:res
- endif
-endfunction
-
" Redirect command output to a register for later processing.
" Ref: https://stackoverflow.com/q/2573021/6064933 and https://unix.stackexchange.com/q/8101/221410 .
function! utils#CaptureCommandOutput(command) abort
diff --git a/docs/README.md b/docs/README.md
index 774d9196..d041655f 100644
--- a/docs/README.md
+++ b/docs/README.md
@@ -118,16 +118,16 @@ The version of Git on the Linux system may be too old so that plugins may break.
Check [here](https://jdhao.github.io/2021/03/27/upgrade_git_on_linux/) on how to install and set up the latest version of Git.
For Windows, install [Git for Windows](https://git-scm.com/download/win) and make sure you can run `git` from command line.
-### ctags
+### universal-ctags
In order to use tags related plugins such as [vista.vim](https://github.com/liuchengxu/vista.vim), we need to install a ctags distribution.
Universal-ctags is preferred.
To install it on Linux, we need to build it from source. See [here](https://askubuntu.com/a/836521/768311) for the details.
-To install ctags on macOS, use [Homebrew](https://github.com/universal-ctags/homebrew-universal-ctags):
+To install ctags on macOS, use [Homebrew](https://formulae.brew.sh/formula/universal-ctags):
```bash
-brew install ctags
+brew install universal-ctags
```
To install it Windows, use [chocolatey](https://chocolatey.org/) or [scoop](https://scoop.sh/)
@@ -166,35 +166,9 @@ Set their PATH properly and make sure you can run `pylint`, `flake8` and `vint`
There are various ways to install Nvim depending on your system.
This config is only maintained for [the latest nvim stable release](https://github.com/neovim/neovim/releases/tag/stable).
-### Linux
+Please check the official doc on how to install Neovim on different systems: https://github.com/neovim/neovim/blob/master/INSTALL.md
-You can directly download the binary release from [here](https://github.com/neovim/neovim/releases/download/stable/nvim-linux64.tar.gz).
-You can also use the system package manager to install nvim,
-but that is not reliable since the latest version may not be available.
-
-### Windows
-
-You may download from [nvim release](https://github.com/neovim/neovim/releases/download/stable/nvim-win64.zip) from GitHub and manually extract it.
-
-Another way to install Nvim on Windows is via chocolatey or scoop:
-
-```
-choco install neovim
-
-# via scoop
-# scoop bucket add versions
-# scoop install neovim
-```
-
-### macOS
-
-It is recommended to install neovim via [Homebrew](https://brew.sh/) on macOS. Simply run the following command:
-
-```bash
-brew install neovim
-```
-
-After installing Nvim, we need to set the path to nvim correctly.
+After installing Nvim, we need to set the PATH to nvim correctly.
**Make sure that you can run `nvim` from the command line after all these setups**.
## Setting up Nvim
diff --git a/docs/nvim_setup_linux.sh b/docs/nvim_setup_linux.sh
index ed207a45..9d0ec3b6 100755
--- a/docs/nvim_setup_linux.sh
+++ b/docs/nvim_setup_linux.sh
@@ -221,7 +221,7 @@ fi
NVIM_DIR=$HOME/tools/nvim
NVIM_SRC_NAME=$HOME/packages/nvim-linux64.tar.gz
NVIM_CONFIG_DIR=$HOME/.config/nvim
-NVIM_LINK="https://github.com/neovim/neovim/releases/download/stable/nvim-linux64.tar.gz"
+NVIM_LINK="https://github.com/neovim/neovim/releases/download/stable/nvim-linux-x86_64.tar.gz"
if [[ ! -f "$NVIM_DIR/bin/nvim" ]]; then
echo "Installing Nvim"
echo "Creating nvim directory under tools directory"
diff --git a/ginit.vim b/ginit.vim
index 5c3d0ff3..484510d9 100644
--- a/ginit.vim
+++ b/ginit.vim
@@ -64,7 +64,7 @@ endif
" config for neovide "
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
if exists("g:neovide")
- set guifont=Hack\ NF:h10
+ set background=dark
let g:neovide_transparency = 1.0
let g:neovide_cursor_animation_length = 0.1
let g:neovide_cursor_trail_size=0.3
diff --git a/init.lua b/init.lua
index baa4ae65..2a99d778 100644
--- a/init.lua
+++ b/init.lua
@@ -9,42 +9,33 @@
-- Blog: https://jdhao.github.io/
-- GitHub: https://github.com/jdhao
-- StackOverflow: https://stackoverflow.com/users/6064933/jdhao
+local utils = require("utils")
+
vim.loader.enable()
-local version = vim.version
-
--- check if we have the latest stable version of nvim
-local expected_ver = "0.10.0"
-local ev = version.parse(expected_ver)
-local actual_ver = version()
-
-local result = version.cmp(ev, {actual_ver.major, actual_ver.minor, actual_ver.patch})
-
-if result ~= 0 then
- local _ver = string.format("%s.%s.%s", actual_ver.major, actual_ver.minor, actual_ver.patch)
- local msg = string.format("Expect nvim %s, but got %s instead. Use at your own risk!", expected_ver, _ver)
- vim.api.nvim_err_writeln(msg)
-end
-
-local core_conf_files = {
- "globals.lua", -- some global settings
- "options.vim", -- setting options in nvim
- "autocommands.vim", -- various autocommands
- "mappings.lua", -- all the user-defined mappings
- "plugins.vim", -- all the plugins installed and their configurations
- "colorschemes.lua", -- colorscheme settings
-}
-
-local viml_conf_dir = vim.fn.stdpath("config") .. "/viml_conf"
--- source all the core config files
-for _, file_name in ipairs(core_conf_files) do
- if vim.endswith(file_name, 'vim') then
- local path = string.format("%s/%s", viml_conf_dir, file_name)
- local source_cmd = "source " .. path
- vim.cmd(source_cmd)
- else
- local module_name, _ = string.gsub(file_name, "%.lua", "")
- package.loaded[module_name] = nil
- require(module_name)
- end
-end
+local expected_version = "0.12.2"
+utils.is_compatible_version(expected_version)
+
+-- some global settings
+require("globals")
+
+-- setting options in nvim
+require("options")
+
+-- various autocommands
+require("custom-autocmd")
+
+-- all the user-defined mappings
+require("mappings")
+
+-- all the plugins installed and their configurations
+require("plugin_specs")
+
+-- This is done after plugin_specs, since lsp-config is loaded in that step
+require("lsp_conf")
+
+-- diagnostic related config
+require("diagnostic-conf")
+
+-- colorscheme settings
+require("ui")
diff --git a/lua/colorschemes.lua b/lua/colorschemes.lua
deleted file mode 100644
index 9618b4d5..00000000
--- a/lua/colorschemes.lua
+++ /dev/null
@@ -1,83 +0,0 @@
---- This module will load a random colorscheme on nvim startup process.
-
-local utils = require("utils")
-
-local M = {}
-
--- Colorscheme to its directory name mapping, because colorscheme repo name is not necessarily
--- the same as the colorscheme name itself.
-M.colorscheme_conf = {
- onedark = function()
- vim.cmd([[colorscheme onedark]])
- end,
- edge = function()
- vim.g.edge_enable_italic = 1
- vim.g.edge_better_performance = 1
-
- vim.cmd([[colorscheme edge]])
- end,
- sonokai = function()
- vim.g.sonokai_enable_italic = 1
- vim.g.sonokai_better_performance = 1
-
- vim.cmd([[colorscheme sonokai]])
- end,
- gruvbox_material = function()
- -- foreground option can be material, mix, or original
- vim.g.gruvbox_material_foreground = "material"
- --background option can be hard, medium, soft
- vim.g.gruvbox_material_background = "soft"
- vim.g.gruvbox_material_enable_italic = 1
- vim.g.gruvbox_material_better_performance = 1
-
- vim.cmd([[colorscheme gruvbox-material]])
- end,
- everforest = function()
- vim.g.everforest_enable_italic = 1
- vim.g.everforest_better_performance = 1
-
- vim.cmd([[colorscheme everforest]])
- end,
- nightfox = function()
- vim.cmd([[colorscheme nordfox]])
- end,
- catppuccin = function()
- -- available option: latte, frappe, macchiato, mocha
- vim.g.catppuccin_flavour = "frappe"
- require("catppuccin").setup()
-
- vim.cmd([[colorscheme catppuccin]])
- end,
- onedarkpro = function()
- -- set colorscheme after options
- vim.cmd('colorscheme onedark_vivid')
- end,
- material = function()
- vim.g.material_style = "oceanic"
- vim.cmd('colorscheme material')
- end,
-}
-
---- Use a random colorscheme from the pre-defined list of colorschemes.
-M.rand_colorscheme = function()
- local colorscheme = utils.rand_element(vim.tbl_keys(M.colorscheme_conf))
-
- if not vim.tbl_contains(vim.tbl_keys(M.colorscheme_conf), colorscheme) then
- local msg = "Invalid colorscheme: " .. colorscheme
- vim.notify(msg, vim.log.levels.ERROR, { title = "nvim-config" })
-
- return
- end
-
- -- Load the colorscheme and its settings
- M.colorscheme_conf[colorscheme]()
-
- if vim.g.logging_level == "debug" then
- local msg = "Colorscheme: " .. colorscheme
-
- vim.notify(msg, vim.log.levels.DEBUG, { title = "nvim-config" })
- end
-end
-
--- Load a random colorscheme
-M.rand_colorscheme()
diff --git a/lua/config/blink-cmp.lua b/lua/config/blink-cmp.lua
new file mode 100644
index 00000000..ac83c630
--- /dev/null
+++ b/lua/config/blink-cmp.lua
@@ -0,0 +1,100 @@
+require("blink.cmp").setup {
+ -- 'default' (recommended) for mappings similar to built-in completions (C-y to accept)
+ -- 'super-tab' for mappings similar to vscode (tab to accept)
+ -- 'enter' for enter to accept
+ -- 'none' for no mappings
+ --
+ keymap = {
+ preset = "default",
+ [""] = { "select_next", "fallback" },
+ [""] = { "select_prev", "fallback" },
+ [""] = { "select_and_accept", "fallback" },
+ [""] = { "scroll_documentation_up", "fallback" },
+ [""] = { "scroll_documentation_down", "fallback" },
+ },
+
+ appearance = {
+ -- 'mono' (default) for 'Nerd Font Mono' or 'normal' for 'Nerd Font'
+ -- Adjusts spacing to ensure icons are aligned
+ nerd_font_variant = "mono",
+ },
+
+ -- (Default) Only show the documentation popup when manually triggered
+ completion = {
+ documentation = {
+ auto_show = true,
+ },
+ },
+
+ -- Default list of enabled providers defined so that you can extend it
+ -- elsewhere in your config, without redefining it, due to `opts_extend`
+ sources = {
+ default = { "lsp", "path", "buffer", "omni" },
+ providers = {
+ -- Use the thesaurus source
+ thesaurus = {
+ name = "blink-cmp-words",
+ module = "blink-cmp-words.thesaurus",
+ -- All available options
+ opts = {
+ -- A score offset applied to returned items.
+ -- By default the highest score is 0 (item 1 has a score of -1, item 2 of -2 etc..).
+ score_offset = 0,
+
+ -- Default pointers define the lexical relations listed under each definition,
+ -- see Pointer Symbols below.
+ -- Default is as below ("antonyms", "similar to" and "also see").
+ definition_pointers = { "!", "&", "^" },
+
+ -- The pointers that are considered similar words when using the thesaurus,
+ -- see Pointer Symbols below.
+ -- Default is as below ("similar to", "also see" }
+ similarity_pointers = { "&", "^" },
+
+ -- The depth of similar words to recurse when collecting synonyms. 1 is similar words,
+ -- 2 is similar words of similar words, etc. Increasing this may slow results.
+ similarity_depth = 2,
+ },
+ },
+
+ -- Use the dictionary source
+ dictionary = {
+ name = "blink-cmp-words",
+ module = "blink-cmp-words.dictionary",
+ -- All available options
+ opts = {
+ -- The number of characters required to trigger completion.
+ -- Set this higher if completion is slow, 3 is default.
+ dictionary_search_threshold = 3,
+
+ -- See above
+ score_offset = 0,
+
+ -- See above
+ definition_pointers = { "!", "&", "^" },
+ },
+ },
+ },
+ per_filetype = {
+ text = { "dictionary" },
+ markdown = { "dictionary" },
+ },
+ },
+
+ -- (Default) Rust fuzzy matcher for typo resistance and significantly better performance
+ -- You may use a lua implementation instead by using `implementation = "lua"` or fallback to the lua implementation,
+ -- when the Rust fuzzy matcher is not available, by using `implementation = "prefer_rust"`
+ --
+ -- See the fuzzy documentation for more information
+ fuzzy = { implementation = "prefer_rust_with_warning" },
+ cmdline = {
+ completion = {
+ menu = {
+ auto_show = true,
+ },
+ },
+ keymap = {
+ [""] = { "select_and_accept", "fallback" },
+ },
+ },
+}
diff --git a/lua/config/bufferline.lua b/lua/config/bufferline.lua
index 6fa27cee..747fe185 100644
--- a/lua/config/bufferline.lua
+++ b/lua/config/bufferline.lua
@@ -1,6 +1,6 @@
require("bufferline").setup {
options = {
- numbers = "buffer_id",
+ numbers = "none",
close_command = "bdelete! %d",
right_mouse_command = nil,
left_mouse_command = "buffer %d",
@@ -44,3 +44,7 @@ require("bufferline").setup {
sort_by = "id",
},
}
+
+vim.keymap.set("n", "bp", "BufferLinePick", {
+ desc = "pick a buffer",
+})
diff --git a/lua/config/dashboard-nvim.lua b/lua/config/dashboard-nvim.lua
deleted file mode 100644
index 9017d961..00000000
--- a/lua/config/dashboard-nvim.lua
+++ /dev/null
@@ -1,75 +0,0 @@
-local api = vim.api
-local keymap = vim.keymap
-local dashboard = require("dashboard")
-
-local conf = {}
-conf.header = {
- " ",
- " ",
- " ",
- " ███╗ ██╗ ███████╗ ██████╗ ██╗ ██╗ ██╗ ███╗ ███╗",
- " ████╗ ██║ ██╔════╝██╔═══██╗ ██║ ██║ ██║ ████╗ ████║",
- " ██╔██╗ ██║ █████╗ ██║ ██║ ██║ ██║ ██║ ██╔████╔██║",
- " ██║╚██╗██║ ██╔══╝ ██║ ██║ ╚██╗ ██╔╝ ██║ ██║╚██╔╝██║",
- " ██║ ╚████║ ███████╗╚██████╔╝ ╚████╔╝ ██║ ██║ ╚═╝ ██║",
- " ╚═╝ ╚═══╝ ╚══════╝ ╚═════╝ ╚═══╝ ╚═╝ ╚═╝ ╚═╝",
- " ",
- " ",
- " ",
- " ",
-}
-
-conf.center = {
- {
- icon = " ",
- desc = "Find File ",
- action = "Leaderf file --popup",
- key = " f f",
- },
- {
- icon = " ",
- desc = "Recently opened files ",
- action = "Leaderf mru --popup",
- key = " f r",
- },
- {
- icon = " ",
- desc = "Project grep ",
- action = "Leaderf rg --popup",
- key = " f g",
- },
- {
- icon = " ",
- desc = "Open Nvim config ",
- action = "tabnew $MYVIMRC | tcd %:p:h",
- key = " e v",
- },
- {
- icon = " ",
- desc = "New file ",
- action = "enew",
- key = "e",
- },
- {
- icon = " ",
- desc = "Quit Nvim ",
- -- desc = "Quit Nvim ",
- action = "qa",
- key = "q",
- },
-}
-
-dashboard.setup({
- theme = 'doom',
- shortcut_type = 'number',
- config = conf
-})
-
-api.nvim_create_autocmd("FileType", {
- pattern = "dashboard",
- group = api.nvim_create_augroup("dashboard_enter", { clear = true }),
- callback = function ()
- keymap.set("n", "q", ":qa", { buffer = true, silent = true })
- keymap.set("n", "e", ":enew", { buffer = true, silent = true })
- end
-})
diff --git a/lua/config/diffview.lua b/lua/config/diffview.lua
new file mode 100644
index 00000000..97d2c573
--- /dev/null
+++ b/lua/config/diffview.lua
@@ -0,0 +1,51 @@
+local ok, diffview = pcall(require, "diffview")
+if not ok then
+ return
+end
+
+local actions = require("diffview.actions")
+local prefix_conflicts = "gC"
+
+diffview.setup {
+ enhanced_diff_hl = true,
+ view = {
+ default = {
+ disable_diagnostics = true,
+ },
+ merge_tool = {
+ layout = "diff3_mixed",
+ },
+ },
+ file_history_panel = {
+ win_config = {
+ type = "split",
+ position = "bottom",
+ height = 10,
+ },
+ },
+ keymaps = {
+ view = {
+ {
+ "n",
+ prefix_conflicts .. "t",
+ actions.conflict_choose("theirs"),
+ { desc = "Conflict choose theirs" },
+ },
+ {
+ "n",
+ prefix_conflicts .. "o",
+ actions.conflict_choose("ours"),
+ { desc = "Conflict choose ours" },
+ },
+ {
+ "n",
+ prefix_conflicts .. "a",
+ actions.conflict_choose("all"),
+ { desc = "Conflict choose both" },
+ },
+
+ { "n", "]C", actions.next_conflict, { desc = "Next conflict" } },
+ { "n", "[C", actions.prev_conflict, { desc = "Previous conflict" } },
+ },
+ },
+}
diff --git a/lua/config/fugitive.lua b/lua/config/fugitive.lua
index b9c45e2a..306d71ba 100644
--- a/lua/config/fugitive.lua
+++ b/lua/config/fugitive.lua
@@ -1,11 +1,27 @@
local keymap = vim.keymap
-keymap.set("n", "gs", "Git", { desc = "Git status" })
-keymap.set("n", "gw", "Gwrite", { desc = "Git add" })
-keymap.set("n", "gc", "Git commit", { desc = "Git commit" })
-keymap.set("n", "gd", "Gdiffsplit", { desc = "Git diff" })
-keymap.set("n", "gpl", "Git pull", { desc = "Git pull" })
-keymap.set("n", "gpu", "15 split|term git push", { desc = "Git push" })
+keymap.set("n", "gs", "Git", { desc = "Git: show status" })
+keymap.set("n", "gw", "Gwrite", { desc = "Git: add file" })
+keymap.set("n", "gc", "Git commit", { desc = "Git: commit changes" })
+keymap.set("n", "gpl", "Git pull", { desc = "Git: pull changes" })
+keymap.set("n", "gpu", "15 split|term git push", { desc = "Git: push changes" })
+keymap.set("v", "gb", ":Git blame", { desc = "Git: blame selected line" })
-- convert git to Git in command line mode
-vim.fn['utils#Cabbrev']('git', 'Git')
+vim.fn["utils#Cabbrev"]("git", "Git")
+
+keymap.set("n", "gbn", function()
+ vim.ui.input({ prompt = "Enter a new branch name" }, function(user_input)
+ if user_input == nil or user_input == "" then
+ return
+ end
+
+ local cmd_str = string.format("G checkout -b %s", user_input)
+ vim.cmd(cmd_str)
+ end)
+end, {
+ desc = "Git: create new branch",
+})
+
+keymap.set("n", "gf", ":Git fetch ", { desc = "Git: prune branches" })
+keymap.set("n", "gbd", ":Git branch -D ", { desc = "Git: delete branch" })
diff --git a/lua/config/fzf-lua.lua b/lua/config/fzf-lua.lua
new file mode 100644
index 00000000..50cdcb7e
--- /dev/null
+++ b/lua/config/fzf-lua.lua
@@ -0,0 +1,43 @@
+require("fzf-lua").setup {
+ defaults = {
+ file_icons = "mini",
+ },
+ winopts = {
+ row = 0.5,
+ height = 0.7,
+ },
+ files = {
+ previewer = false,
+ git_icons = true,
+ -- using .gitignore is usually good, but still we may want to include some files,
+ -- you can create a file `.rgignore` to "unignore" those files, e.g., `.env` files.
+ -- see also https://github.com/BurntSushi/ripgrep/discussions/2512
+ -- and https://www.reddit.com/r/linuxquestions/comments/zycvud/ripgrep_respect_gitignore_but_show_env_files/
+ no_ignore = false,
+ },
+ grep = {
+ RIPGREP_CONFIG_PATH = vim.env.RIPGREP_CONFIG_PATH,
+ },
+}
+
+vim.keymap.set("n", "ff", "FzfLua files", { desc = "Fuzzy find files" })
+vim.keymap.set("n", "fg", "FzfLua live_grep_native", { desc = "Fuzzy grep files" })
+vim.keymap.set(
+ "n",
+ "fh",
+ "FzfLua helptags",
+ { desc = "Fuzzy grep tags in help files" }
+)
+vim.keymap.set("n", "ft", "FzfLua btags", { desc = "Fuzzy search buffer tags" })
+vim.keymap.set(
+ "n",
+ "fb",
+ "FzfLua buffers",
+ { desc = "Fuzzy search opened buffers" }
+)
+vim.keymap.set(
+ "n",
+ "fr",
+ "FzfLua oldfiles",
+ { desc = "Fuzzy search opened files history" }
+)
diff --git a/lua/config/git-linker.lua b/lua/config/git-linker.lua
index 39f5ba21..cbb42420 100644
--- a/lua/config/git-linker.lua
+++ b/lua/config/git-linker.lua
@@ -5,16 +5,26 @@ gitlinker.setup {
callbacks = {
["dev.azure.com"] = function(url_data)
vim.print(url_data)
- local url = require"gitlinker.hosts".get_base_https_url(url_data)
+ local url = require("gitlinker.hosts").get_base_https_url(url_data)
if url_data.lstart then
if url_data.lend == nil then
url_data.lend = url_data.lstart
end
- url = url .. "?path=/" .. url_data.file .. "&version=GC" .. url_data.rev .. "&line=" .. url_data.lstart .. "&lineEnd=" .. url_data.lend .. "&lineStartColumn=1" .. "&lineEndColumn=120"
+ url = url
+ .. "?path=/"
+ .. url_data.file
+ .. "&version=GC"
+ .. url_data.rev
+ .. "&line="
+ .. url_data.lstart
+ .. "&lineEnd="
+ .. url_data.lend
+ .. "&lineStartColumn=1"
+ .. "&lineEndColumn=120"
end
return url
- end
+ end,
},
mappings = nil,
}
@@ -24,14 +34,14 @@ keymap.set({ "n", "v" }, "gl", function()
gitlinker.get_buf_range_url(mode)
end, {
silent = true,
- desc = "get git permlink",
+ desc = "Git: get permlink",
})
-keymap.set("n", "gb", function()
- gitlinker.get_repo_url({
- action_callback = gitlinker.actions.open_in_browser
- })
+keymap.set("n", "gbr", function()
+ gitlinker.get_repo_url {
+ action_callback = gitlinker.actions.open_in_browser,
+ }
end, {
silent = true,
- desc = "browse repo in browser",
+ desc = "Git: browse repo in browser",
})
diff --git a/lua/config/gitsigns.lua b/lua/config/gitsigns.lua
index f272dcc9..c7216b7e 100644
--- a/lua/config/gitsigns.lua
+++ b/lua/config/gitsigns.lua
@@ -8,7 +8,7 @@ gs.setup {
topdelete = { text = "‾" },
changedelete = { text = "│" },
},
- word_diff = true,
+ word_diff = false,
on_attach = function(bufnr)
local function map(mode, l, r, opts)
opts = opts or {}
@@ -38,20 +38,20 @@ gs.setup {
end, { expr = true, desc = "previous hunk" })
-- Actions
- map("n", "hp", gs.preview_hunk)
+ map("n", "hp", gs.preview_hunk, { desc = "preview hunk" })
map("n", "hb", function()
gs.blame_line { full = true }
- end)
+ end, { desc = "blame hunk" })
end,
}
-vim.api.nvim_create_autocmd('ColorScheme', {
+vim.api.nvim_create_autocmd("ColorScheme", {
pattern = "*",
callback = function()
- vim.cmd [[
+ vim.cmd([[
hi GitSignsChangeInline gui=reverse
hi GitSignsAddInline gui=reverse
hi GitSignsDeleteInline gui=reverse
- ]]
- end
+ ]])
+ end,
})
diff --git a/lua/config/glance.lua b/lua/config/glance.lua
new file mode 100644
index 00000000..daaef828
--- /dev/null
+++ b/lua/config/glance.lua
@@ -0,0 +1,12 @@
+local glance = require("glance")
+
+glance.setup {
+ height = 25,
+ border = {
+ enable = true,
+ },
+}
+
+vim.keymap.set("n", "gd", "Glance definitions")
+vim.keymap.set("n", "gr", "Glance references")
+vim.keymap.set("n", "gi", "Glance implementations")
diff --git a/lua/config/gx.lua b/lua/config/gx.lua
new file mode 100644
index 00000000..e8109adc
--- /dev/null
+++ b/lua/config/gx.lua
@@ -0,0 +1,21 @@
+---@diagnostic disable-next-line: missing-fields
+require("gx").setup {
+ handlers = {
+ jira = {
+ name = "jira", -- set name of handler
+ handle = function(mode, line, _)
+ local ticket = require("gx.helper").find(line, mode, "(%a%a%a+%-%d+)")
+
+ local dotenv = require("dotenv")
+ local env_path = vim.fs.joinpath(vim.fn.stdpath("config"), ".env")
+ dotenv.load_dotenv(env_path)
+ local company_name = dotenv.get("COMPANY_NAME")
+
+ if ticket and #ticket < 20 then
+ local ticket_link = string.format("http://jira.%s/browse/%s", company_name, ticket)
+ return ticket_link
+ end
+ end,
+ },
+ },
+}
diff --git a/lua/config/hlslens.lua b/lua/config/hlslens.lua
index 430bd230..e216a645 100644
--- a/lua/config/hlslens.lua
+++ b/lua/config/hlslens.lua
@@ -14,9 +14,9 @@ local activate_hlslens = function(direction)
-- Deal with the case that there is no such pattern in current buffer.
if not status then
- local start_idx, _ = string.find(msg, 'E486', 1, true)
+ local start_idx, _ = string.find(msg, "E486", 1, true)
local msg_part = string.sub(msg, start_idx)
- api.nvim_err_writeln(msg_part)
+ api.nvim_echo({ { msg_part } }, true, { err = true })
return
end
@@ -35,15 +35,48 @@ keymap.set("n", "N", "", {
end,
})
+local check_cursor_word = function()
+ local cursor_word = vim.fn.expand("")
+ local result = cursor_word == ""
+ if result then
+ local msg = "E348: No string under cursor"
+ api.nvim_echo({ { msg } }, true, { err = true })
+ end
+
+ return result, cursor_word
+end
+
keymap.set("n", "*", "", {
callback = function()
- vim.fn.execute("normal! *N")
+ local cursor_word_empty, cursor_word = check_cursor_word()
+ if cursor_word_empty then
+ return
+ end
+
+ local cmd = string.format([[normal! /\v<%s>]], cursor_word)
+
+ -- In order to say that we are pressing Enter key, instead of typing literally the character,
+ -- we need to replace special notation with their internal representation.
+ local escaped_enter = vim.api.nvim_replace_termcodes("", true, false, true)
+
+ -- character `N` is used to keep the cursor when pressing `*`
+ local full_cmd = cmd .. escaped_enter .. "N"
+ vim.fn.execute(full_cmd)
hlslens.start()
end,
})
keymap.set("n", "#", "", {
callback = function()
- vim.fn.execute("normal! #N")
+ local cursor_word_empty, cursor_word = check_cursor_word()
+ if cursor_word_empty then
+ return
+ end
+
+ local cmd = string.format([[normal! ?\v<%s>]], cursor_word)
+ local escaped_enter = vim.api.nvim_replace_termcodes("", true, false, true)
+
+ local full_cmd = cmd .. escaped_enter .. "N"
+ vim.fn.execute(full_cmd)
hlslens.start()
end,
})
diff --git a/lua/config/indent-blankline.lua b/lua/config/indent-blankline.lua
deleted file mode 100644
index 9b210d7d..00000000
--- a/lua/config/indent-blankline.lua
+++ /dev/null
@@ -1,36 +0,0 @@
-local api = vim.api
-
-local exclude_ft = { "help", "git", "markdown", "snippets", "text", "gitconfig", "alpha", "dashboard" }
-
-require("ibl").setup {
- indent = {
- -- -- U+2502 may also be a good choice, it will be on the middle of cursor.
- -- -- U+250A is also a good choice
- char = "▏",
- },
- scope = {
- show_start = false,
- show_end = false,
- },
- exclude = {
- filetypes = exclude_ft,
- buftypes = { "terminal" },
- },
-}
-
-local gid = api.nvim_create_augroup("indent_blankline", { clear = true })
-api.nvim_create_autocmd("InsertEnter", {
- pattern = "*",
- group = gid,
- command = "IBLDisable",
-})
-
-api.nvim_create_autocmd("InsertLeave", {
- pattern = "*",
- group = gid,
- callback = function()
- if not vim.tbl_contains(exclude_ft, vim.bo.filetype) then
- vim.cmd([[IBLEnable]])
- end
- end,
-})
diff --git a/lua/config/iswap.lua b/lua/config/iswap.lua
new file mode 100644
index 00000000..cf29e1d7
--- /dev/null
+++ b/lua/config/iswap.lua
@@ -0,0 +1,5 @@
+require("iswap").setup {
+ move_cursor = true,
+}
+vim.keymap.set("n", "gs<", "ISwapWithLeft")
+vim.keymap.set("n", "gs>", "ISwapWithRight")
diff --git a/lua/config/lightbulb.lua b/lua/config/lightbulb.lua
new file mode 100644
index 00000000..8f49746b
--- /dev/null
+++ b/lua/config/lightbulb.lua
@@ -0,0 +1,20 @@
+---@diagnostic disable: missing-fields
+require("nvim-lightbulb").setup {
+ autocmd = {
+ enabled = true,
+ updatetime = -1,
+ },
+ ---@diagnostic disable-next-line: unused-local
+ filter = function(client_name, result)
+ -- Ruff always sends these two actions even if there are no action to take,
+ -- so it is better to just ignore this to avoid noise. See also discussion below:
+ -- https://github.com/astral-sh/ruff-lsp/issues/91
+ local ignored_kinds = { "source.fixAll.ruff", "source.organizeImports.ruff" }
+
+ if vim.tbl_contains(ignored_kinds, result.kind) then
+ return false
+ end
+
+ return true
+ end,
+}
diff --git a/lua/config/live-command.lua b/lua/config/live-command.lua
new file mode 100644
index 00000000..4680c8ee
--- /dev/null
+++ b/lua/config/live-command.lua
@@ -0,0 +1,9 @@
+require("live-command").setup {
+ enable_highlighting = true,
+ inline_highlighting = true,
+ commands = {
+ Norm = { cmd = "norm" },
+ },
+}
+
+vim.cmd("cnoreabbrev norm Norm")
diff --git a/lua/config/lsp.lua b/lua/config/lsp.lua
deleted file mode 100644
index aac7a90c..00000000
--- a/lua/config/lsp.lua
+++ /dev/null
@@ -1,273 +0,0 @@
-local fn = vim.fn
-local api = vim.api
-local keymap = vim.keymap
-local lsp = vim.lsp
-local diagnostic = vim.diagnostic
-
-local utils = require("utils")
-
--- set quickfix list from diagnostics in a certain buffer, not the whole workspace
-local set_qflist = function(buf_num, severity)
- local diagnostics = nil
- diagnostics = diagnostic.get(buf_num, { severity = severity })
-
- local qf_items = diagnostic.toqflist(diagnostics)
- vim.fn.setqflist({}, ' ', { title = 'Diagnostics', items = qf_items })
-
- -- open quickfix by default
- vim.cmd[[copen]]
-end
-
-local custom_attach = function(client, bufnr)
- -- Mappings.
- local map = function(mode, l, r, opts)
- opts = opts or {}
- opts.silent = true
- opts.buffer = bufnr
- keymap.set(mode, l, r, opts)
- end
-
- map("n", "gd", vim.lsp.buf.definition, { desc = "go to definition" })
- map("n", "", vim.lsp.buf.definition)
- map("n", "K", vim.lsp.buf.hover)
- map("n", "", vim.lsp.buf.signature_help)
- map("n", "rn", vim.lsp.buf.rename, { desc = "varialbe rename" })
- map("n", "gr", vim.lsp.buf.references, { desc = "show references" })
- map("n", "[d", diagnostic.goto_prev, { desc = "previous diagnostic" })
- map("n", "]d", diagnostic.goto_next, { desc = "next diagnostic" })
- -- this puts diagnostics from opened files to quickfix
- map("n", "qw", diagnostic.setqflist, { desc = "put window diagnostics to qf" })
- -- this puts diagnostics from current buffer to quickfix
- map("n", "qb", function() set_qflist(bufnr) end, { desc = "put buffer diagnostics to qf" })
- map("n", "ca", vim.lsp.buf.code_action, { desc = "LSP code action" })
- map("n", "wa", vim.lsp.buf.add_workspace_folder, { desc = "add workspace folder" })
- map("n", "wr", vim.lsp.buf.remove_workspace_folder, { desc = "remove workspace folder" })
- map("n", "wl", function()
- inspect(vim.lsp.buf.list_workspace_folders())
- end, { desc = "list workspace folder" })
-
- -- Set some key bindings conditional on server capabilities
- if client.server_capabilities.documentFormattingProvider then
- map("n", "f", vim.lsp.buf.format, { desc = "format code" })
- end
-
- api.nvim_create_autocmd("CursorHold", {
- buffer = bufnr,
- callback = function()
- local float_opts = {
- focusable = false,
- close_events = { "BufLeave", "CursorMoved", "InsertEnter", "FocusLost" },
- border = "rounded",
- source = "always", -- show source in diagnostic popup window
- prefix = " ",
- }
-
- if not vim.b.diagnostics_pos then
- vim.b.diagnostics_pos = { nil, nil }
- end
-
- local cursor_pos = api.nvim_win_get_cursor(0)
- if (cursor_pos[1] ~= vim.b.diagnostics_pos[1] or cursor_pos[2] ~= vim.b.diagnostics_pos[2])
- and #diagnostic.get() > 0
- then
- diagnostic.open_float(nil, float_opts)
- end
-
- vim.b.diagnostics_pos = cursor_pos
- end,
- })
-
- -- The blow command will highlight the current variable and its usages in the buffer.
- if client.server_capabilities.documentHighlightProvider then
- vim.cmd([[
- hi! link LspReferenceRead Visual
- hi! link LspReferenceText Visual
- hi! link LspReferenceWrite Visual
- ]])
-
- local gid = api.nvim_create_augroup("lsp_document_highlight", { clear = true })
- api.nvim_create_autocmd("CursorHold" , {
- group = gid,
- buffer = bufnr,
- callback = function ()
- lsp.buf.document_highlight()
- end
- })
-
- api.nvim_create_autocmd("CursorMoved" , {
- group = gid,
- buffer = bufnr,
- callback = function ()
- lsp.buf.clear_references()
- end
- })
- end
-
- if vim.g.logging_level == "debug" then
- local msg = string.format("Language server %s started!", client.name)
- vim.notify(msg, vim.log.levels.DEBUG, { title = "Nvim-config" })
- end
-end
-
-local capabilities = require('cmp_nvim_lsp').default_capabilities()
-
-local lspconfig = require("lspconfig")
-
-if utils.executable("pylsp") then
- local venv_path = os.getenv('VIRTUAL_ENV')
- local py_path = nil
- -- decide which python executable to use for mypy
- if venv_path ~= nil then
- py_path = venv_path .. "/bin/python3"
- else
- py_path = vim.g.python3_host_prog
- end
-
- lspconfig.pylsp.setup {
- on_attach = custom_attach,
- settings = {
- pylsp = {
- plugins = {
- -- formatter options
- black = { enabled = true },
- autopep8 = { enabled = false },
- yapf = { enabled = false },
- -- linter options
- pylint = { enabled = true, executable = "pylint" },
- ruff = { enabled = false },
- pyflakes = { enabled = false },
- pycodestyle = { enabled = false },
- -- type checker
- pylsp_mypy = {
- enabled = true,
- overrides = { "--python-executable", py_path, true },
- report_progress = true,
- live_mode = false
- },
- -- auto-completion options
- jedi_completion = { fuzzy = true },
- -- import sorting
- isort = { enabled = true },
- },
- },
- },
- flags = {
- debounce_text_changes = 200,
- },
- capabilities = capabilities,
- }
-else
- vim.notify("pylsp not found!", vim.log.levels.WARN, { title = "Nvim-config" })
-end
-
--- if utils.executable('pyright') then
--- lspconfig.pyright.setup{
--- on_attach = custom_attach,
--- capabilities = capabilities
--- }
--- else
--- vim.notify("pyright not found!", vim.log.levels.WARN, {title = 'Nvim-config'})
--- end
-
-if utils.executable("ltex-ls") then
- lspconfig.ltex.setup {
- on_attach = custom_attach,
- cmd = { "ltex-ls" },
- filetypes = { "text", "plaintex", "tex", "markdown" },
- settings = {
- ltex = {
- language = "en"
- },
- },
- flags = { debounce_text_changes = 300 },
-}
-end
-
-if utils.executable("clangd") then
- lspconfig.clangd.setup {
- on_attach = custom_attach,
- capabilities = capabilities,
- filetypes = { "c", "cpp", "cc" },
- flags = {
- debounce_text_changes = 500,
- },
- }
-end
-
--- set up vim-language-server
-if utils.executable("vim-language-server") then
- lspconfig.vimls.setup {
- on_attach = custom_attach,
- flags = {
- debounce_text_changes = 500,
- },
- capabilities = capabilities,
- }
-else
- vim.notify("vim-language-server not found!", vim.log.levels.WARN, { title = "Nvim-config" })
-end
-
--- set up bash-language-server
-if utils.executable("bash-language-server") then
- lspconfig.bashls.setup {
- on_attach = custom_attach,
- capabilities = capabilities,
- }
-end
-
-if utils.executable("lua-language-server") then
- -- settings for lua-language-server can be found on https://github.com/LuaLS/lua-language-server/wiki/Settings .
- lspconfig.lua_ls.setup {
- on_attach = custom_attach,
- settings = {
- Lua = {
- runtime = {
- -- Tell the language server which version of Lua you're using (most likely LuaJIT in the case of Neovim)
- version = "LuaJIT",
- },
- diagnostics = {
- -- Get the language server to recognize the `vim` global
- globals = { "vim" },
- },
- workspace = {
- -- Make the server aware of Neovim runtime files,
- -- see also https://github.com/LuaLS/lua-language-server/wiki/Libraries#link-to-workspace .
- -- Lua-dev.nvim also has similar settings for lua ls, https://github.com/folke/neodev.nvim/blob/main/lua/neodev/luals.lua .
- library = {
- vim.env.VIMRUNTIME,
- fn.stdpath("config"),
- },
- maxPreload = 2000,
- preloadFileSize = 50000,
- },
- },
- },
- capabilities = capabilities,
- }
-end
-
--- Change diagnostic signs.
-fn.sign_define("DiagnosticSignError", { text = '🆇', texthl = "DiagnosticSignError" })
-fn.sign_define("DiagnosticSignWarn", { text = '⚠️', texthl = "DiagnosticSignWarn" })
-fn.sign_define("DiagnosticSignInfo", { text = 'ℹ️', texthl = "DiagnosticSignInfo" })
-fn.sign_define("DiagnosticSignHint", { text = '', texthl = "DiagnosticSignHint" })
-
--- global config for diagnostic
-diagnostic.config {
- underline = false,
- virtual_text = false,
- signs = true,
- severity_sort = true,
-}
-
--- lsp.handlers["textDocument/publishDiagnostics"] = lsp.with(lsp.diagnostic.on_publish_diagnostics, {
--- underline = false,
--- virtual_text = false,
--- signs = true,
--- update_in_insert = false,
--- })
-
--- Change border of documentation hover window, See https://github.com/neovim/neovim/pull/13998.
-lsp.handlers["textDocument/hover"] = lsp.with(vim.lsp.handlers.hover, {
- border = "rounded",
-})
diff --git a/lua/config/lualine.lua b/lua/config/lualine.lua
new file mode 100644
index 00000000..8aed3c76
--- /dev/null
+++ b/lua/config/lualine.lua
@@ -0,0 +1,311 @@
+local utils = require("utils")
+local fn = vim.fn
+
+-- cache for git states
+local git_status_cache = {
+ fetch_success = false,
+ behind_count = 0,
+ ahead_count = 0,
+}
+
+local on_exit_fetch = function(result)
+ if result.code == 0 then
+ git_status_cache.fetch_success = true
+ end
+end
+
+local function handle_numeric_result(cache_key)
+ return function(result)
+ if result.code == 0 then
+ git_status_cache[cache_key] = tonumber(result.stdout:match("(%d+)")) or 0
+ else
+ -- when the git command fails, it usually means there are some changes in your branch. For example, you
+ -- on branchA, for this one, you have upstream branch. Then you changed to branchB, and there is no upstream
+ -- branch, the git rev-list command will error out. In this case, we should clear the cache
+ -- vim.print("Error running git command", result)
+ git_status_cache[cache_key] = 0
+ end
+ end
+end
+
+local async_cmd = function(cmd_str, on_exit)
+ local cmd = vim.tbl_filter(function(element)
+ return element ~= ""
+ end, vim.split(cmd_str, " "))
+
+ vim.system(cmd, { text = true }, on_exit)
+end
+
+local async_git_status_update = function()
+ -- Fetch the latest changes from the remote repository (replace 'origin' if needed)
+ async_cmd("git fetch origin", on_exit_fetch)
+ if not git_status_cache.fetch_success then
+ return
+ end
+
+ -- Get the number of commits behind
+ -- the @{upstream} notation is inspired by post: https://www.reddit.com/r/neovim/comments/t48x5i/git_branch_aheadbehind_info_status_line_component/
+ -- note that here we should use double dots instead of triple dots
+ local behind_cmd_str = "git rev-list --count HEAD..@{upstream}"
+ async_cmd(behind_cmd_str, handle_numeric_result("behind_count"))
+
+ -- Get the number of commits ahead
+ local ahead_cmd_str = "git rev-list --count @{upstream}..HEAD"
+ async_cmd(ahead_cmd_str, handle_numeric_result("ahead_count"))
+end
+
+local function get_git_ahead_behind_info()
+ async_git_status_update()
+
+ local status = git_status_cache
+ if not status then
+ return ""
+ end
+
+ local msg = ""
+
+ if type(status.ahead_count) == "number" and status.ahead_count > 0 then
+ local ahead_str = string.format("↑[%d] ", status.ahead_count)
+ msg = msg .. ahead_str
+ end
+
+ if type(status.behind_count) == "number" and status.behind_count > 0 then
+ local behind_str = string.format("↓[%d] ", status.behind_count)
+ msg = msg .. behind_str
+ end
+
+ return msg
+end
+
+local function spell()
+ if vim.o.spell then
+ return string.format("[SPELL]")
+ end
+
+ return ""
+end
+
+--- show indicator for Chinese IME
+local function ime_state()
+ if vim.g.is_mac then
+ -- ref: https://github.com/vim-airline/vim-airline/blob/master/autoload/airline/extensions/xkblayout.vim#L11
+ local layout = fn.libcall(vim.g.XkbSwitchLib, "Xkb_Switch_getXkbLayout", "")
+
+ -- We can use `xkbswitch -g` on the command line to get current mode.
+ -- mode for macOS builtin pinyin IME: com.apple.inputmethod.SCIM.ITABC
+ -- mode for Rime: im.rime.inputmethod.Squirrel.Rime
+ local res = fn.match(layout, [[\v(Squirrel\.Rime|SCIM.ITABC)]])
+ if res ~= -1 then
+ return "[CN]"
+ end
+ end
+
+ return ""
+end
+
+local function trailing_space()
+ if not vim.o.modifiable then
+ return ""
+ end
+
+ local line_num = nil
+
+ for i = 1, fn.line("$") do
+ local linetext = fn.getline(i)
+ -- To prevent invalid escape error, we wrap the regex string with `[[]]`.
+ local idx = fn.match(linetext, [[\v\s+$]])
+
+ if idx ~= -1 then
+ line_num = i
+ break
+ end
+ end
+
+ local msg = ""
+ if line_num ~= nil then
+ msg = string.format("[%d]trailing", line_num)
+ end
+
+ return msg
+end
+
+local function mixed_indent()
+ if not vim.o.modifiable then
+ return ""
+ end
+
+ local space_pat = [[\v^ +]]
+ local tab_pat = [[\v^\t+]]
+ local space_indent = fn.search(space_pat, "nwc")
+ local tab_indent = fn.search(tab_pat, "nwc")
+ local mixed = (space_indent > 0 and tab_indent > 0)
+ local mixed_same_line
+ if not mixed then
+ mixed_same_line = fn.search([[\v^(\t+ | +\t)]], "nwc")
+ mixed = mixed_same_line > 0
+ end
+ if not mixed then
+ return ""
+ end
+ if mixed_same_line ~= nil and mixed_same_line > 0 then
+ return "MI:" .. mixed_same_line
+ end
+ local space_indent_cnt = fn.searchcount({ pattern = space_pat, max_count = 1e3 }).total
+ local tab_indent_cnt = fn.searchcount({ pattern = tab_pat, max_count = 1e3 }).total
+ if space_indent_cnt > tab_indent_cnt then
+ return "MI:" .. tab_indent
+ else
+ return "MI:" .. space_indent
+ end
+end
+
+local diff = function()
+ local git_status = vim.b.gitsigns_status_dict
+ if git_status == nil then
+ return
+ end
+
+ local modify_num = git_status.changed
+ local remove_num = git_status.removed
+ local add_num = git_status.added
+
+ local info = { added = add_num, modified = modify_num, removed = remove_num }
+ -- vim.print(info)
+ return info
+end
+
+local virtual_env = function()
+ local venv_name = utils.get_virtual_env()
+
+ if venv_name ~= "" then
+ return string.format(" (%s)", venv_name)
+ else
+ return ""
+ end
+end
+
+local get_active_lsp = function()
+ local msg = "🚫"
+ local buf_ft = vim.api.nvim_get_option_value("filetype", {})
+ local clients = vim.lsp.get_clients { bufnr = 0 }
+ if next(clients) == nil then
+ return msg
+ end
+
+ for _, client in ipairs(clients) do
+ ---@diagnostic disable-next-line: undefined-field
+ local filetypes = client.config.filetypes
+ if filetypes and vim.fn.index(filetypes, buf_ft) ~= -1 then
+ return client.name
+ end
+ end
+ return msg
+end
+
+require("lualine").setup {
+ options = {
+ icons_enabled = true,
+ theme = "auto",
+ component_separators = { left = "|", right = "|" },
+ section_separators = "",
+ disabled_filetypes = {},
+ always_divide_middle = true,
+ refresh = {
+ statusline = 1000,
+ },
+ },
+ sections = {
+ lualine_a = {
+ {
+ "filename",
+ symbols = {
+ readonly = "[🔒]",
+ },
+ },
+ },
+ lualine_b = {
+ {
+ "branch",
+ fmt = function(name, _)
+ -- truncate branch name in case the name is too long
+ return string.sub(name, 1, 20)
+ end,
+ color = { gui = "italic,bold" },
+ },
+ {
+ get_git_ahead_behind_info,
+ color = { fg = "#E0C479" },
+ },
+ {
+ "diff",
+ source = diff,
+ },
+ {
+ virtual_env,
+ color = { fg = "black", bg = "#F1CA81" },
+ },
+ },
+ lualine_c = {
+ {
+ "%S",
+ color = { gui = "bold", fg = "cyan" },
+ },
+ {
+ spell,
+ color = { fg = "black", bg = "#a7c080" },
+ },
+ },
+ lualine_x = {
+ {
+ get_active_lsp,
+ icon = "📡",
+ },
+ {
+ "diagnostics",
+ sources = { "nvim_diagnostic" },
+ symbols = { error = "🆇 ", warn = "⚠️ ", info = "ℹ️ ", hint = " " },
+ },
+ {
+ trailing_space,
+ color = "WarningMsg",
+ },
+ {
+ mixed_indent,
+ color = "WarningMsg",
+ },
+ },
+ lualine_y = {
+ {
+ "encoding",
+ fmt = string.upper,
+ },
+ {
+ "fileformat",
+ symbols = {
+ unix = "unix",
+ dos = "win",
+ mac = "mac",
+ },
+ },
+ "filetype",
+ {
+ ime_state,
+ color = { fg = "black", bg = "#f46868" },
+ },
+ },
+ lualine_z = {
+ "location",
+ "progress",
+ },
+ },
+ inactive_sections = {
+ lualine_a = {},
+ lualine_b = {},
+ lualine_c = { "filename" },
+ lualine_x = { "location" },
+ lualine_y = {},
+ lualine_z = {},
+ },
+ tabline = {},
+ extensions = { "quickfix", "fugitive", "nvim-tree" },
+}
diff --git a/lua/config/nvim-cmp.lua b/lua/config/nvim-cmp.lua
index a402f1c7..b5fc78eb 100644
--- a/lua/config/nvim-cmp.lua
+++ b/lua/config/nvim-cmp.lua
@@ -1,6 +1,15 @@
-- Setup nvim-cmp.
local cmp = require("cmp")
-local lspkind = require("lspkind")
+
+-- The extentions needed by nvim-cmp should be loaded beforehand
+require("cmp_nvim_lsp")
+require("cmp_path")
+require("cmp_buffer")
+require("cmp_omni")
+require("cmp_nvim_ultisnips")
+require("cmp_cmdline")
+
+local MiniIcons = require("mini.icons")
cmp.setup {
snippet = {
@@ -35,7 +44,6 @@ cmp.setup {
{ name = "ultisnips" }, -- For ultisnips user.
{ name = "path" }, -- for path completion
{ name = "buffer", keyword_length = 2 }, -- for buffer word completion
- { name = "emoji", insert = true }, -- emoji completion
},
completion = {
keyword_length = 1,
@@ -44,19 +52,14 @@ cmp.setup {
view = {
entries = "custom",
},
+ -- solution taken from https://github.com/echasnovski/mini.nvim/issues/1007#issuecomment-2258929830
formatting = {
- format = lspkind.cmp_format {
- mode = "symbol_text",
- menu = {
- nvim_lsp = "[LSP]",
- ultisnips = "[US]",
- nvim_lua = "[Lua]",
- path = "[Path]",
- buffer = "[Buffer]",
- emoji = "[Emoji]",
- omni = "[Omni]",
- },
- },
+ format = function(_, vim_item)
+ local icon, hl = MiniIcons.get("lsp", vim_item.kind)
+ vim_item.kind = icon .. " " .. vim_item.kind
+ vim_item.kind_hl_group = hl
+ return vim_item
+ end,
},
}
@@ -69,6 +72,23 @@ cmp.setup.filetype("tex", {
},
})
+cmp.setup.cmdline("/", {
+ mapping = cmp.mapping.preset.cmdline(),
+ sources = {
+ { name = "buffer" },
+ },
+})
+
+cmp.setup.cmdline(":", {
+ mapping = cmp.mapping.preset.cmdline(),
+ sources = cmp.config.sources({
+ { name = "path" },
+ }, {
+ { name = "cmdline" },
+ }),
+ matching = { disallow_symbol_nonprefix_matching = false },
+})
+
-- see https://github.com/hrsh7th/nvim-cmp/wiki/Menu-Appearance#how-to-add-visual-studio-code-dark-theme-colors-to-the-menu
vim.cmd([[
highlight! link CmpItemMenu Comment
diff --git a/lua/config/nvim-lint.lua b/lua/config/nvim-lint.lua
new file mode 100644
index 00000000..6b416dd7
--- /dev/null
+++ b/lua/config/nvim-lint.lua
@@ -0,0 +1,16 @@
+local nvim_lint = require("lint")
+
+nvim_lint.linters_by_ft = {
+ -- to use vale, install it: `brew install vale`
+ -- configure it via: https://vale.sh/generator, put config in `.vale.ini` in project root.
+ -- to install cspell and typos: `brew install cspell typos-cli`
+ -- markdown = { "vale", "cspell", "typos" },
+}
+
+vim.api.nvim_create_autocmd({ "InsertLeave" }, {
+ callback = function()
+ -- try_lint without arguments runs the linters defined in `linters_by_ft`
+ -- for the current filetype
+ nvim_lint.try_lint()
+ end,
+})
diff --git a/lua/config/nvim-statuscol.lua b/lua/config/nvim-statuscol.lua
new file mode 100644
index 00000000..3b860479
--- /dev/null
+++ b/lua/config/nvim-statuscol.lua
@@ -0,0 +1,23 @@
+local builtin = require("statuscol.builtin")
+local ffi = require("statuscol.ffidef")
+local C = ffi.C
+
+-- only show fold level up to this level
+local fold_level_limit = 3
+local function foldfunc(args)
+ local foldinfo = C.fold_info(args.wp, args.lnum)
+ if foldinfo.level > fold_level_limit then
+ return " "
+ end
+
+ return builtin.foldfunc(args)
+end
+
+require("statuscol").setup {
+ relculright = false,
+ segments = {
+ { text = { "%s" }, click = "v:lua.ScSa" },
+ { text = { builtin.lnumfunc, " " }, click = "v:lua.ScLa" },
+ { text = { foldfunc, " " }, condition = { true, builtin.not_empty }, click = "v:lua.ScFa" },
+ },
+}
diff --git a/lua/config/nvim-tree.lua b/lua/config/nvim-tree.lua
index 6bf653d5..c9196b03 100644
--- a/lua/config/nvim-tree.lua
+++ b/lua/config/nvim-tree.lua
@@ -40,10 +40,6 @@ nvim_tree.setup {
update_cwd = false,
ignore_list = {},
},
- system_open = {
- cmd = "",
- args = {},
- },
diagnostics = {
enable = false,
show_on_dirs = false,
diff --git a/lua/config/nvim_hop.lua b/lua/config/nvim_hop.lua
index 11aa60dc..205543d5 100644
--- a/lua/config/nvim_hop.lua
+++ b/lua/config/nvim_hop.lua
@@ -4,7 +4,7 @@ hop.setup {
case_insensitive = true,
char2_fallback_key = "",
quit_key = "",
- match_mappings = { "zh_sc" }
+ match_mappings = { "zh_sc" },
}
keymap.set({ "n", "v", "o" }, "f", "", {
@@ -24,5 +24,5 @@ vim.api.nvim_create_autocmd("ColorScheme", {
hi HopNextKey1 cterm=bold ctermfg=176 gui=bold guibg=#ff00ff guifg=#ffffff
hi HopNextKey2 cterm=bold ctermfg=176 gui=bold guibg=#ff00ff guifg=#ffffff
]])
- end
+ end,
})
diff --git a/lua/config/nvim_ufo.lua b/lua/config/nvim_ufo.lua
new file mode 100644
index 00000000..bc1ae60d
--- /dev/null
+++ b/lua/config/nvim_ufo.lua
@@ -0,0 +1,45 @@
+local handler = function(virtText, lnum, endLnum, width, truncate)
+ local newVirtText = {}
+ local foldedLines = endLnum - lnum
+ local suffix = (" %d"):format(foldedLines)
+ local sufWidth = vim.fn.strdisplaywidth(suffix)
+ local targetWidth = width - sufWidth
+ local curWidth = 0
+
+ for _, chunk in ipairs(virtText) do
+ local chunkText = chunk[1]
+ local chunkWidth = vim.fn.strdisplaywidth(chunkText)
+ if targetWidth > curWidth + chunkWidth then
+ table.insert(newVirtText, chunk)
+ else
+ chunkText = truncate(chunkText, targetWidth - curWidth)
+ local hlGroup = chunk[2]
+ table.insert(newVirtText, { chunkText, hlGroup })
+ chunkWidth = vim.fn.strdisplaywidth(chunkText)
+ -- str width returned from truncate() may less than 2nd argument, need padding
+ if curWidth + chunkWidth < targetWidth then
+ suffix = suffix .. (" "):rep(targetWidth - curWidth - chunkWidth)
+ end
+ break
+ end
+ curWidth = curWidth + chunkWidth
+ end
+ local rAlignAppndx =
+ math.max(math.min(vim.opt.textwidth["_value"], width - 1) - curWidth - sufWidth, 0)
+ suffix = (" "):rep(rAlignAppndx) .. suffix
+ table.insert(newVirtText, { suffix, "MoreMsg" })
+ return newVirtText
+end
+
+require("ufo").setup {
+ fold_virt_text_handler = handler,
+}
+
+vim.keymap.set("n", "zR", require("ufo").openAllFolds)
+vim.keymap.set("n", "zM", require("ufo").closeAllFolds)
+vim.keymap.set("n", "zr", require("ufo").openFoldsExceptKinds)
+vim.keymap.set("n", "K", function()
+ local _ = require("ufo").peekFoldedLinesUnderCursor()
+end, {
+ desc = "Preview folded maps",
+})
diff --git a/lua/config/snacks.lua b/lua/config/snacks.lua
new file mode 100644
index 00000000..81c7dffb
--- /dev/null
+++ b/lua/config/snacks.lua
@@ -0,0 +1,12 @@
+require("snacks").setup {
+ -- more beautiful vim.ui.input
+ input = {
+ enabled = true,
+ win = {
+ relative = "cursor",
+ backdrop = true,
+ },
+ },
+ -- more beautiful vim.ui.select
+ picker = { enabled = true },
+}
diff --git a/lua/config/statusline.lua b/lua/config/statusline.lua
deleted file mode 100644
index 0563ba64..00000000
--- a/lua/config/statusline.lua
+++ /dev/null
@@ -1,197 +0,0 @@
-local fn = vim.fn
-
-local function spell()
- if vim.o.spell then
- return string.format("[SPELL]")
- end
-
- return ""
-end
-
---- show indicator for Chinese IME
-local function ime_state()
- if vim.g.is_mac then
- -- ref: https://github.com/vim-airline/vim-airline/blob/master/autoload/airline/extensions/xkblayout.vim#L11
- local layout = fn.libcall(vim.g.XkbSwitchLib, "Xkb_Switch_getXkbLayout", "")
-
- -- We can use `xkbswitch -g` on the command line to get current mode.
- -- mode for macOS builtin pinyin IME: com.apple.inputmethod.SCIM.ITABC
- -- mode for Rime: im.rime.inputmethod.Squirrel.Rime
- local res = fn.match(layout, [[\v(Squirrel\.Rime|SCIM.ITABC)]])
- if res ~= -1 then
- return "[CN]"
- end
- end
-
- return ""
-end
-
-local function trailing_space()
- if not vim.o.modifiable then
- return ""
- end
-
- local line_num = nil
-
- for i = 1, fn.line("$") do
- local linetext = fn.getline(i)
- -- To prevent invalid escape error, we wrap the regex string with `[[]]`.
- local idx = fn.match(linetext, [[\v\s+$]])
-
- if idx ~= -1 then
- line_num = i
- break
- end
- end
-
- local msg = ""
- if line_num ~= nil then
- msg = string.format("[%d]trailing", line_num)
- end
-
- return msg
-end
-
-local function mixed_indent()
- if not vim.o.modifiable then
- return ""
- end
-
- local space_pat = [[\v^ +]]
- local tab_pat = [[\v^\t+]]
- local space_indent = fn.search(space_pat, "nwc")
- local tab_indent = fn.search(tab_pat, "nwc")
- local mixed = (space_indent > 0 and tab_indent > 0)
- local mixed_same_line
- if not mixed then
- mixed_same_line = fn.search([[\v^(\t+ | +\t)]], "nwc")
- mixed = mixed_same_line > 0
- end
- if not mixed then
- return ""
- end
- if mixed_same_line ~= nil and mixed_same_line > 0 then
- return "MI:" .. mixed_same_line
- end
- local space_indent_cnt = fn.searchcount({ pattern = space_pat, max_count = 1e3 }).total
- local tab_indent_cnt = fn.searchcount({ pattern = tab_pat, max_count = 1e3 }).total
- if space_indent_cnt > tab_indent_cnt then
- return "MI:" .. tab_indent
- else
- return "MI:" .. space_indent
- end
-end
-
-local diff = function()
- local git_status = vim.b.gitsigns_status_dict
- if git_status == nil then
- return
- end
-
- local modify_num = git_status.changed
- local remove_num = git_status.removed
- local add_num = git_status.added
-
- local info = { added = add_num, modified = modify_num, removed = remove_num }
- -- vim.print(info)
- return info
-end
-
-local virtual_env = function()
- -- only show virtual env for Python
- if vim.bo.filetype ~= 'python' then
- return ""
- end
-
- local conda_env = os.getenv('CONDA_DEFAULT_ENV')
- local venv_path = os.getenv('VIRTUAL_ENV')
-
- if venv_path == nil then
- if conda_env == nil then
- return ""
- else
- return string.format(" %s (conda)", conda_env)
- end
- else
- local venv_name = vim.fn.fnamemodify(venv_path, ':t')
- return string.format(" %s (venv)", venv_name)
- end
-end
-
-require("lualine").setup {
- options = {
- icons_enabled = true,
- theme = "auto",
- -- component_separators = { left = "", right = "" },
- -- section_separators = { left = "", right = "" },
- section_separators = "",
- component_separators = "",
- disabled_filetypes = {},
- always_divide_middle = true,
- },
- sections = {
- lualine_a = { "mode" },
- lualine_b = {
- "branch",
- {
- "diff",
- source = diff,
- },
- {
- virtual_env,
- color = { fg = 'black', bg = "#F1CA81" }
- }
- },
- lualine_c = {
- "filename",
- {
- ime_state,
- color = { fg = "black", bg = "#f46868" },
- },
- {
- spell,
- color = { fg = "black", bg = "#a7c080" },
- },
- {
- "diagnostics",
- sources = { "nvim_diagnostic" },
- symbols = {error = '🆇 ', warn = '⚠️ ', info = 'ℹ️ ', hint = ' '},
- },
- },
- lualine_x = {
- "encoding",
- {
- "fileformat",
- symbols = {
- unix = "unix",
- dos = "win",
- mac = "mac",
- },
- },
- "filetype",
- },
- lualine_y = {
- "location",
- },
- lualine_z = {
- {
- trailing_space,
- color = "WarningMsg",
- },
- {
- mixed_indent,
- color = "WarningMsg",
- },
- },
- },
- inactive_sections = {
- lualine_a = {},
- lualine_b = {},
- lualine_c = { "filename" },
- lualine_x = { "location" },
- lualine_y = {},
- lualine_z = {},
- },
- tabline = {},
- extensions = { "quickfix", "fugitive", "nvim-tree" },
-}
diff --git a/lua/config/treesitter-textobjects.lua b/lua/config/treesitter-textobjects.lua
new file mode 100644
index 00000000..051e8289
--- /dev/null
+++ b/lua/config/treesitter-textobjects.lua
@@ -0,0 +1,50 @@
+-- configuration
+require("nvim-treesitter-textobjects").setup {
+ select = {
+ -- Automatically jump forward to textobj, similar to targets.vim
+ lookahead = true,
+ -- You can choose the select mode (default is charwise 'v')
+ --
+ -- Can also be a function which gets passed a table with the keys
+ -- * query_string: eg '@function.inner'
+ -- * method: eg 'v' or 'o'
+ -- and should return the mode ('v', 'V', or '') or a table
+ -- mapping query_strings to modes.
+ selection_modes = {
+ ["@function.inner"] = "V", -- linewise
+ ["@function.outer"] = "V", -- linewise
+ ["@class.outer"] = "V", -- linewise
+ ["@class.inner"] = "V", -- linewise
+ ["@parameter.outer"] = "v", -- charwise
+ },
+ -- If you set this to `true` (default is `false`) then any textobject is
+ -- extended to include preceding or succeeding whitespace. Succeeding
+ -- whitespace has priority in order to act similarly to eg the built-in
+ -- `ap`.
+ --
+ -- Can also be a function which gets passed a table with the keys
+ -- * query_string: eg '@function.inner'
+ -- * selection_mode: eg 'v'
+ -- and should return true of false
+ include_surrounding_whitespace = false,
+ },
+}
+
+-- keymaps
+-- You can use the capture groups defined in `textobjects.scm`
+vim.keymap.set({ "x", "o" }, "af", function()
+ require("nvim-treesitter-textobjects.select").select_textobject("@function.outer", "textobjects")
+end)
+vim.keymap.set({ "x", "o" }, "if", function()
+ require("nvim-treesitter-textobjects.select").select_textobject("@function.inner", "textobjects")
+end)
+vim.keymap.set({ "x", "o" }, "ac", function()
+ require("nvim-treesitter-textobjects.select").select_textobject("@class.outer", "textobjects")
+end)
+vim.keymap.set({ "x", "o" }, "ic", function()
+ require("nvim-treesitter-textobjects.select").select_textobject("@class.inner", "textobjects")
+end)
+-- You can also use captures from other query groups like `locals.scm`
+vim.keymap.set({ "x", "o" }, "as", function()
+ require("nvim-treesitter-textobjects.select").select_textobject("@local.scope", "locals")
+end)
diff --git a/lua/config/treesitter.lua b/lua/config/treesitter.lua
index 41118594..52f2f2e8 100644
--- a/lua/config/treesitter.lua
+++ b/lua/config/treesitter.lua
@@ -1,8 +1,61 @@
-require("nvim-treesitter.configs").setup {
- ensure_installed = { "python", "cpp", "lua", "vim", "json", "toml" },
- ignore_install = {}, -- List of parsers to ignore installing
- highlight = {
- enable = true, -- false will disable the whole extension
- disable = { 'help' }, -- list of language that will be disabled
- },
+-- a list of filetypes to install treesitter parsers and queries
+local nvim_treesitter = require("nvim-treesitter")
+
+local ensure_installed = {
+ "cpp",
+ "diff",
+ "go",
+ "gomod",
+ "gosum",
+ "javascript",
+ "json",
+ "lua",
+ "markdown",
+ "python",
+ "sh",
+ "toml",
+ "typescript",
+ "vim",
+ "yaml",
+ "zsh",
}
+
+vim.api.nvim_create_autocmd("FileType", {
+ pattern = ensure_installed,
+
+ callback = function(args)
+ local ft = vim.bo[args.buf].filetype
+ local lang = vim.treesitter.language.get_lang(ft)
+ if lang == nil then
+ return
+ end
+
+ -- check if parser is available
+ local is_parser_available = vim.treesitter.language.add(lang)
+ if not is_parser_available then
+ local available_langs = vim.g.ts_available or nvim_treesitter.get_available()
+ if not vim.g.ts_available then
+ vim.g.ts_available = available_langs
+ end
+
+ if vim.tbl_contains(available_langs, lang) then
+ -- install treesitter parsers and queries
+ local install_msg = string.format("Installing parsers and queries for %s", lang)
+ vim.print(install_msg)
+ require("nvim-treesitter").install(lang)
+ end
+ end
+
+ if vim.treesitter.language.add(lang) then
+ -- start treesitter highlighting
+ vim.treesitter.start(args.buf, lang)
+
+ -- the following two statements will enable treesitter folding
+ -- vim.wo[0][0].foldexpr = "v:lua.vim.treesitter.foldexpr()"
+ -- vim.wo[0][0].foldmethod = "expr"
+
+ -- enable treesitter-based indentation
+ -- vim.bo.indentexpr = "v:lua.require'nvim-treesitter'.indentexpr()"
+ end
+ end,
+})
diff --git a/lua/config/which-key.lua b/lua/config/which-key.lua
index 10c0c413..136895a4 100644
--- a/lua/config/which-key.lua
+++ b/lua/config/which-key.lua
@@ -1,60 +1,6 @@
require("which-key").setup {
- plugins = {
- marks = true, -- shows a list of your marks on ' and `
- registers = true, -- shows your registers on " in NORMAL or in INSERT mode
- spelling = {
- enabled = true, -- enabling this will show WhichKey when pressing z= to select spelling suggestions
- suggestions = 9, -- how many suggestions should be shown in the list?
- },
- -- the presets plugin, adds help for a bunch of default keybindings in Neovim
- -- No actual key bindings are created
- presets = {
- operators = true, -- adds help for operators like d, y, ... and registers them for motion / text object completion
- motions = true, -- adds help for motions
- text_objects = true, -- help for text objects triggered after entering an operator
- windows = true, -- default bindings on
- nav = true, -- misc bindings to work with windows
- z = true, -- bindings for folds, spelling and others prefixed with z
- g = true, -- bindings for prefixed with g
- },
- },
- -- add operators that will trigger motion and text object completion
- -- to enable all native operators, set the preset / operators plugin above
- operators = { gc = "Comments" },
- key_labels = {
- -- override the label used to display some keys. It doesn't effect WK in any other way.
- -- For example:
- -- [""] = "SPC",
- -- [""] = "RET",
- -- [""] = "TAB",
- },
+ preset = "modern",
icons = {
- breadcrumb = "»", -- symbol used in the command line area that shows your active key combo
- separator = "➜", -- symbol used between a key and it's label
- group = "+", -- symbol prepended to a group
- },
- window = {
- border = "none", -- none, single, double, shadow
- position = "bottom", -- bottom, top
- margin = { 0, 0, 0, 0 }, -- extra window margin [top, right, bottom, left]
- padding = { 1, 0, 1, 0 }, -- extra window padding [top, right, bottom, left]
- },
- layout = {
- height = { min = 1, max = 25 }, -- min and max height of the columns
- width = { min = 20, max = 50 }, -- min and max width of the columns
- spacing = 1, -- spacing between columns
- align = "center", -- align columns left, center or right
- },
- ignore_missing = false, -- enable this to hide mappings for which you didn't specify a label
- hidden = { "", "", "", "", "call", "lua", "^:", "^ " }, -- hide mapping boilerplate
- show_help = true, -- show help message on the command line when the popup is visible
- triggers = "auto", -- automatically setup triggers
- -- triggers = {""} -- or specify a list manually
-
- triggers_blacklist = {
- -- list of mode / prefixes that should never be hooked by WhichKey
- -- this is mostly relevant for key maps that start with a native binding
- -- most people should not need to change this
- n = { "o", "O" },
+ mappings = false,
},
}
diff --git a/lua/config/yanky.lua b/lua/config/yanky.lua
index 0f8eceba..2c7b134e 100644
--- a/lua/config/yanky.lua
+++ b/lua/config/yanky.lua
@@ -1,13 +1,17 @@
-require("yanky").setup({
- ring = {
- history_length = 50,
- storage = "memory",
- },
+require("yanky").setup {
preserve_cursor_position = {
enabled = false,
},
-})
+ highlight = {
+ on_put = true,
+ on_yank = false,
+ timer = 300,
+ },
+}
+
+vim.keymap.set({ "n", "x" }, "p", "(YankyPutAfter)")
+vim.keymap.set({ "n", "x" }, "P", "(YankyPutBefore)")
-- cycle through the yank history, only work after paste
-vim.keymap.set("n", "[y", "(YankyCycleForward)")
-vim.keymap.set("n", "]y", "(YankyCycleBackward)")
+vim.keymap.set("n", "[y", "(YankyPreviousEntry)")
+vim.keymap.set("n", "]y", "(YankyNextEntry)")
diff --git a/lua/config/zen-mode.lua b/lua/config/zen-mode.lua
deleted file mode 100644
index b6d52ce6..00000000
--- a/lua/config/zen-mode.lua
+++ /dev/null
@@ -1,15 +0,0 @@
-require("zen-mode").setup {
- window = {
- backdrop = 0.8, -- shade the backdrop of the Zen window. Set to 1 to keep the same as Normal
- width = 120,
- options = {
- -- signcolumn = "no", -- disable signcolumn
- -- number = false, -- disable number column
- -- relativenumber = false, -- disable relative numbers
- cursorline = false, -- disable cursorline
- cursorcolumn = false, -- disable cursor column
- foldcolumn = "0", -- disable fold column
- list = false, -- disable whitespace characters
- },
- },
-}
diff --git a/lua/custom-autocmd.lua b/lua/custom-autocmd.lua
index 21fa15ca..57d31a1d 100644
--- a/lua/custom-autocmd.lua
+++ b/lua/custom-autocmd.lua
@@ -22,7 +22,7 @@ api.nvim_create_autocmd({ "TextYankPost" }, {
pattern = "*",
group = yank_group,
callback = function()
- vim.highlight.on_yank { higroup = "YankColor", timeout = 300 }
+ vim.hl.on_yank { higroup = "YankColor", timeout = 300 }
end,
})
@@ -37,9 +37,10 @@ api.nvim_create_autocmd({ "CursorMoved" }, {
api.nvim_create_autocmd("TextYankPost", {
pattern = "*",
group = yank_group,
- callback = function(ev)
- if vim.v.event.operator == 'y' then
- vim.fn.setpos('.', vim.g.current_cursor_pos)
+ ---@diagnostic disable-next-line: unused-local
+ callback = function(context)
+ if vim.v.event.operator == "y" then
+ vim.fn.setpos(".", vim.g.current_cursor_pos)
end
end,
})
@@ -63,7 +64,11 @@ api.nvim_create_autocmd({ "FileChangedShellPost" }, {
pattern = "*",
group = "auto_read",
callback = function()
- vim.notify("File changed on disk. Buffer reloaded!", vim.log.levels.WARN, { title = "nvim-config" })
+ vim.notify(
+ "File changed on disk. Buffer reloaded!",
+ vim.log.levels.WARN,
+ { title = "nvim-config" }
+ )
end,
})
@@ -102,4 +107,202 @@ local function open_nvim_tree(data)
require("nvim-tree.api").tree.open()
end
-vim.api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree })
+api.nvim_create_autocmd({ "VimEnter" }, { callback = open_nvim_tree })
+
+-- Do not use smart case in command line mode, extracted from https://vi.stackexchange.com/a/16511/15292.
+api.nvim_create_augroup("dynamic_smartcase", { clear = true })
+api.nvim_create_autocmd("CmdLineEnter", {
+ group = "dynamic_smartcase",
+ pattern = ":",
+ callback = function()
+ vim.o.smartcase = false
+ end,
+})
+
+api.nvim_create_autocmd("CmdLineLeave", {
+ group = "dynamic_smartcase",
+ pattern = ":",
+ callback = function()
+ vim.o.smartcase = true
+ end,
+})
+
+api.nvim_create_autocmd("TermOpen", {
+ group = api.nvim_create_augroup("term_start", { clear = true }),
+ pattern = "*",
+ callback = function()
+ -- Do not use number and relative number for terminal inside nvim
+ vim.wo.relativenumber = false
+ vim.wo.number = false
+
+ -- Go to insert mode by default to start typing command
+ vim.cmd("startinsert")
+ end,
+})
+
+local number_toggle_group = api.nvim_create_augroup("numbertoggle", { clear = true })
+api.nvim_create_autocmd({ "BufEnter", "FocusGained", "InsertLeave", "WinEnter" }, {
+ pattern = "*",
+ group = number_toggle_group,
+ desc = "togger line number",
+ callback = function()
+ if vim.wo.number then
+ vim.wo.relativenumber = true
+ end
+ end,
+})
+
+api.nvim_create_autocmd({ "BufLeave", "FocusLost", "InsertEnter", "WinLeave" }, {
+ group = number_toggle_group,
+ desc = "togger line number",
+ callback = function()
+ if vim.wo.number then
+ vim.wo.relativenumber = false
+ end
+ end,
+})
+
+api.nvim_create_autocmd("ColorScheme", {
+ group = api.nvim_create_augroup("custom_highlight", { clear = true }),
+ pattern = "*",
+ desc = "Define or overrride some highlight groups",
+ callback = function()
+ -- For yank highlight
+ vim.api.nvim_set_hl(
+ 0,
+ "YankColor",
+ { fg = "#34495E", bg = "#2ECC71", ctermfg = 59, ctermbg = 41 }
+ )
+
+ -- For cursor colors, see option guicursor for more info
+ vim.api.nvim_set_hl(0, "Cursor", { fg = "black", bg = "#00c918", update = true })
+ vim.api.nvim_set_hl(0, "Cursor2", { fg = "None", bg = "yellow", update = true })
+
+ -- For floating windows border highlight
+ vim.api.nvim_set_hl(0, "FloatBorder", { bg = "None", fg = "LightGreen", update = true })
+
+ -- change the background color of floating window to None, so it blenders better
+ vim.api.nvim_set_hl(0, "NormalFloat", { bg = "None", update = true })
+
+ -- this is the highlight used by nvim-cmp for cmdline completion window border
+ vim.api.nvim_set_hl(0, "Pmenu", { bg = "None", update = true })
+
+ -- highlight for matching parentheses
+ vim.api.nvim_set_hl(0, "MatchParen", { bold = true, underline = true, update = true })
+
+ vim.api.nvim_set_hl(0, "IlluminatedWordWrite", { reverse = true, update = true })
+ vim.api.nvim_set_hl(0, "IlluminatedWordRead", { reverse = true, update = true })
+ vim.api.nvim_set_hl(0, "IlluminatedWordText", { reverse = true, update = true })
+ end,
+})
+
+api.nvim_create_autocmd("BufEnter", {
+ pattern = "*",
+ group = api.nvim_create_augroup("auto_close_win", { clear = true }),
+ desc = "Quit Nvim if we have only one window, and its filetype match our pattern",
+ ---@diagnostic disable-next-line: unused-local
+ callback = function(context)
+ local quit_filetypes = { "qf", "vista", "NvimTree" }
+
+ local should_quit = true
+ local tabwins = api.nvim_tabpage_list_wins(0)
+
+ for _, win in pairs(tabwins) do
+ local buf = api.nvim_win_get_buf(win)
+ local buf_type = vim.api.nvim_get_option_value("filetype", { buf = buf })
+
+ if not vim.tbl_contains(quit_filetypes, buf_type) then
+ should_quit = false
+ end
+ end
+
+ if should_quit then
+ vim.cmd("qall")
+ end
+ end,
+})
+
+api.nvim_create_autocmd({ "VimEnter", "DirChanged" }, {
+ group = api.nvim_create_augroup("git_repo_check", { clear = true }),
+ pattern = "*",
+ desc = "check if we are inside Git repo",
+ callback = function()
+ utils.inside_git_repo()
+ end,
+})
+
+-- ref: https://vi.stackexchange.com/a/169/15292
+api.nvim_create_autocmd("BufReadPre", {
+ group = api.nvim_create_augroup("large_file", { clear = true }),
+ pattern = "*",
+ desc = "optimize for large file",
+ callback = function(ev)
+ local file_size_limit = 524288 -- 0.5MB
+ local f = ev.file
+
+ if fn.getfsize(f) > file_size_limit or fn.getfsize(f) == -2 then
+ vim.o.eventignore = "all"
+
+ -- show ruler
+ vim.o.ruler = true
+
+ -- turning off relative number helps a lot
+ vim.wo.relativenumber = false
+ vim.wo.number = false
+
+ vim.bo.swapfile = false
+ vim.bo.bufhidden = "unload"
+ vim.bo.undolevels = -1
+ end
+ end,
+})
+
+-- check if current file is formatted
+local ft_to_command = {
+ python = { "black", "--check" },
+ lua = { "stylua", "--check" },
+}
+
+api.nvim_create_autocmd("BufWritePost", {
+ group = api.nvim_create_augroup("format check", { clear = true }),
+ pattern = { "*" },
+ desc = "Check if file needs reformat",
+ callback = function(ev)
+ local fpath = ev.file
+ local ft = vim.api.nvim_get_option_value("filetype", { buf = ev.buf })
+
+ local cmd_partial = ft_to_command[ft]
+ local cmd = nil
+ if cmd_partial ~= nil then
+ cmd = vim.deepcopy(cmd_partial)
+ table.insert(cmd, fpath)
+ else
+ return
+ end
+
+ local exe_path = cmd[1]
+ if not utils.executable(exe_path) then
+ vim.print(string.format("%s not found!", exe_path))
+ return
+ end
+
+ local result = vim.system(cmd, { text = true }):wait()
+ if result.code ~= 0 then
+ vim.notify("This file is not formatted!")
+ end
+ end,
+})
+
+api.nvim_create_autocmd({ "InsertLeave", "TextChanged" }, {
+ group = api.nvim_create_augroup("auto_save", { clear = true }),
+ pattern = { "*" },
+ desc = "Auto save current file",
+ callback = function(ev)
+ local is_readonly = vim.api.nvim_get_option_value("readonly", { buf = ev.buf })
+ local is_modifiable = vim.api.nvim_get_option_value("modifiable", { buf = ev.buf })
+
+ if not is_readonly and is_modifiable then
+ vim.cmd([[silent! update]])
+ end
+ end,
+})
diff --git a/lua/diagnostic-conf.lua b/lua/diagnostic-conf.lua
new file mode 100644
index 00000000..7763a610
--- /dev/null
+++ b/lua/diagnostic-conf.lua
@@ -0,0 +1,69 @@
+local diagnostic = vim.diagnostic
+local api = vim.api
+
+-- global config for diagnostic
+diagnostic.config {
+ underline = false,
+ virtual_text = false,
+ virtual_lines = false,
+ signs = {
+ text = {
+ [diagnostic.severity.ERROR] = "🆇",
+ [diagnostic.severity.WARN] = "⚠️",
+ [diagnostic.severity.INFO] = "ℹ️",
+ [diagnostic.severity.HINT] = "",
+ },
+ },
+ severity_sort = true,
+ float = {
+ source = true,
+ header = "Diagnostics:",
+ prefix = " ",
+ border = "single",
+ max_height = 10,
+ max_width = 130,
+ close_events = { "CursorMoved", "BufLeave", "WinLeave", "InsertEnter" },
+ },
+}
+
+-- set quickfix list from diagnostics in a certain buffer, not the whole workspace
+local set_qflist = function(buf_num, severity)
+ local diagnostics = nil
+ diagnostics = diagnostic.get(buf_num, { severity = severity })
+
+ local qf_items = diagnostic.toqflist(diagnostics)
+ vim.fn.setqflist({}, " ", { title = "Diagnostics", items = qf_items })
+
+ -- open quickfix by default
+ vim.cmd([[copen]])
+end
+
+-- this puts diagnostics from opened files to quickfix
+vim.keymap.set("n", "qw", diagnostic.setqflist, { desc = "put window diagnostics to qf" })
+
+-- this puts diagnostics from current buffer to quickfix
+vim.keymap.set("n", "qb", function()
+ set_qflist(0)
+end, { desc = "put buffer diagnostics to qf" })
+
+-- automatically show diagnostic in float win for current line
+api.nvim_create_autocmd("CursorHold", {
+ pattern = "*",
+ callback = function()
+ if #vim.diagnostic.get(0) == 0 then
+ return
+ end
+
+ if not vim.b.diagnostics_pos then
+ vim.b.diagnostics_pos = { nil, nil }
+ end
+
+ local cursor_pos = api.nvim_win_get_cursor(0)
+
+ if not vim.deep_equal(cursor_pos, vim.b.diagnostics_pos) then
+ diagnostic.open_float {}
+ end
+
+ vim.b.diagnostics_pos = cursor_pos
+ end,
+})
diff --git a/lua/dotenv.lua b/lua/dotenv.lua
new file mode 100644
index 00000000..a065dda0
--- /dev/null
+++ b/lua/dotenv.lua
@@ -0,0 +1,29 @@
+-- code from https://github.com/mcjkula/lua-dotenv/blob/main/env.lua
+local M = {}
+local env_vars = {}
+
+function M.get(key, default)
+ local value = env_vars[key] or os.getenv(key)
+ return value or default
+end
+
+function M.load_dotenv(file_path)
+ file_path = file_path or os.getenv("HOME") .. "/.config/.env"
+ local file, _ = io.open(file_path, "r")
+ if not file then
+ return
+ end
+
+ local content = file:read("*all")
+ file:close()
+
+ for line in content:gmatch("[^\r\n]+") do
+ local key, value = line:match("^([%w_]+)%s*=%s*(.+)$")
+ if key and value then
+ value = value:gsub("^[\"'](.-)[\"']$", "%1")
+ env_vars[key] = value
+ end
+ end
+end
+
+return M
diff --git a/lua/globals.lua b/lua/globals.lua
index b0af26ea..e76a6a63 100644
--- a/lua/globals.lua
+++ b/lua/globals.lua
@@ -1,11 +1,14 @@
-local fn = vim.fn
-local api = vim.api
-
-local utils = require('utils')
-
--- Inspect something
-function _G.inspect(item)
- vim.print(item)
+local utils = require("utils")
+
+local config_path = vim.fn.stdpath("config")
+local python3_path = vim.fs.joinpath(config_path, ".venv/bin/python3")
+if not vim.uv.fs_stat(python3_path) then
+ local msg = string.format(
+ "Python provider missing:\n create a virtual env under nvim config and install pynvim!"
+ )
+ vim.api.nvim_echo({ { msg } }, true, { err = true })
+else
+ vim.g.python3_host_prog = python3_path
end
------------------------------------------------------------------------
@@ -13,42 +16,31 @@ end
------------------------------------------------------------------------
vim.g.is_win = (utils.has("win32") or utils.has("win64")) and true or false
vim.g.is_linux = (utils.has("unix") and (not utils.has("macunix"))) and true or false
-vim.g.is_mac = utils.has("macunix") and true or false
+vim.g.is_mac = utils.has("macunix") and true or false
-vim.g.logging_level = "info"
+vim.g.logging_level = vim.log.levels.INFO
------------------------------------------------------------------------
-- builtin variables --
------------------------------------------------------------------------
-vim.g.loaded_perl_provider = 0 -- Disable perl provider
-vim.g.loaded_ruby_provider = 0 -- Disable ruby provider
-vim.g.loaded_node_provider = 0 -- Disable node provider
-vim.g.did_install_default_menus = 1 -- do not load menu
-
-if utils.executable('python3') then
- if vim.g.is_win then
- vim.g.python3_host_prog = fn.substitute(fn.exepath("python3"), ".exe$", '', 'g')
- else
- vim.g.python3_host_prog = fn.exepath("python3")
- end
-else
- api.nvim_err_writeln("Python3 executable not found! You must install Python3 and set its PATH correctly!")
- return
-end
+vim.g.loaded_perl_provider = 0 -- Disable perl provider
+vim.g.loaded_ruby_provider = 0 -- Disable ruby provider
+vim.g.loaded_node_provider = 0 -- Disable node provider
+vim.g.did_install_default_menus = 1 -- do not load menu
-- Custom mapping (see `:h mapleader` for more info)
-vim.g.mapleader = ','
+vim.g.mapleader = ","
-- Enable highlighting for lua HERE doc inside vim script
-vim.g.vimsyn_embed = 'l'
+vim.g.vimsyn_embed = "l"
-- Use English as main language
-vim.cmd [[language en_US.UTF-8]]
+vim.cmd([[language en_US.UTF-8]])
-- Disable loading certain plugins
-- Whether to load netrw by default, see https://github.com/bling/dotvim/issues/4
-vim.g.loaded_netrw = 1
+vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1
vim.g.netrw_liststyle = 3
if vim.g.is_win then
@@ -73,3 +65,6 @@ vim.g.loaded_matchparen = 1
-- Disable sql omni completion, it is broken.
vim.g.loaded_sql_completion = 1
+
+-- control how to show health check window
+vim.g.health = { style = nil }
diff --git a/lua/lsp_conf.lua b/lua/lsp_conf.lua
new file mode 100644
index 00000000..0474aab3
--- /dev/null
+++ b/lua/lsp_conf.lua
@@ -0,0 +1,194 @@
+local utils = require("utils")
+
+vim.api.nvim_create_autocmd("LspAttach", {
+ group = vim.api.nvim_create_augroup("lsp_buf_conf", { clear = true }),
+ callback = function(event_context)
+ local client = vim.lsp.get_client_by_id(event_context.data.client_id)
+ -- vim.print(client.name, client.server_capabilities)
+
+ if not client then
+ return
+ end
+
+ local bufnr = event_context.buf
+
+ -- Mappings.
+ local map = function(mode, l, r, opts)
+ opts = opts or {}
+ opts.silent = true
+ opts.buffer = bufnr
+ vim.keymap.set(mode, l, r, opts)
+ end
+
+ map("n", "gd", function()
+ vim.lsp.buf.definition {
+ on_list = function(options)
+ -- custom logic to avoid showing multiple definition when you use this style of code:
+ -- `local M.my_fn_name = function() ... end`.
+ -- See also post here: https://www.reddit.com/r/neovim/comments/19cvgtp/any_way_to_remove_redundant_definition_in_lua_file/
+
+ -- vim.print(options.items)
+ local unique_defs = {}
+ local def_loc_hash = {}
+
+ -- each item in options.items contain the location info for a definition provided by LSP server
+ for _, def_location in pairs(options.items) do
+ -- use filename and line number to uniquelly indentify a definition,
+ -- we do not expect/want multiple definition in single line!
+ local hash_key = def_location.filename .. def_location.lnum
+
+ if not def_loc_hash[hash_key] then
+ def_loc_hash[hash_key] = true
+ table.insert(unique_defs, def_location)
+ end
+ end
+
+ options.items = unique_defs
+
+ -- set the location list
+ ---@diagnostic disable-next-line: param-type-mismatch
+ vim.fn.setloclist(0, {}, " ", options)
+
+ -- open the location list when we have more than 1 definitions found,
+ -- otherwise, jump directly to the definition
+ if #options.items > 1 then
+ vim.cmd.lopen()
+ else
+ vim.cmd([[silent! lfirst]])
+ end
+ end,
+ }
+ end, { desc = "go to definition" })
+ map("n", "", vim.lsp.buf.definition)
+ map("n", "K", function()
+ vim.lsp.buf.hover {
+ border = "single",
+ max_height = 20,
+ max_width = 130,
+ close_events = { "CursorMoved", "BufLeave", "WinLeave", "LSPDetach" },
+ }
+ end)
+ map("n", "", vim.lsp.buf.signature_help)
+ map("n", "rn", vim.lsp.buf.rename, { desc = "varialbe rename" })
+ map("n", "ca", vim.lsp.buf.code_action, { desc = "LSP code action" })
+ map("n", "wa", vim.lsp.buf.add_workspace_folder, { desc = "add workspace folder" })
+ map("n", "wr", vim.lsp.buf.remove_workspace_folder, { desc = "remove workspace folder" })
+ map("n", "wl", function()
+ vim.print(vim.lsp.buf.list_workspace_folders())
+ end, { desc = "list workspace folder" })
+
+ -- Set some key bindings conditional on server capabilities
+ -- Disable ruff hover feature in favor of Pyright
+ if client.name == "ruff" then
+ client.server_capabilities.hoverProvider = false
+ end
+ end,
+ nested = true,
+ desc = "Configure buffer keymap and behavior based on LSP",
+})
+
+-- Enable lsp servers when they are available
+
+local capabilities = require("lsp_utils").get_default_capabilities()
+
+-- `*` will set default config for all lsp
+vim.lsp.config("*", {
+ capabilities = capabilities,
+ flags = {
+ debounce_text_changes = 500,
+ },
+})
+
+-- A mapping from lsp server name to the executable name
+local enabled_lsp_servers = {
+ pyright = "delance-langserver",
+ ruff = "ruff",
+ lua_ls = "lua-language-server",
+ -- clangd = "clangd",
+ vimls = "vim-language-server",
+ bashls = "bash-language-server",
+ yamlls = "yaml-language-server",
+ gopls = "gopls",
+ -- the server can be install via homebrew: brew install golangci-lint-langserver
+ -- golangci-lint also needs to be installed: https://github.com/golangci/golangci-lint
+ golangci_lint_ls = "golangci-lint-langserver",
+
+ -- to install codebook, run `brew install codebook-lsp`
+ -- codebook = "codebook-lsp"
+}
+
+for server_name, lsp_executable in pairs(enabled_lsp_servers) do
+ if utils.executable(lsp_executable) then
+ vim.lsp.enable(server_name)
+ else
+ local msg = string.format(
+ "Executable '%s' for server '%s' not found! Server will not be enabled",
+ lsp_executable,
+ server_name
+ )
+ vim.notify(msg, vim.log.levels.WARN, { title = "Nvim-config" })
+ end
+end
+
+-- LSP related command
+
+vim.api.nvim_create_user_command("LspInfo", "checkhealth vim.lsp", {
+ desc = "Show LSP Info",
+})
+
+vim.api.nvim_create_user_command("LspLog", function(_)
+ local log_path = vim.lsp.log.get_filename()
+
+ vim.cmd(string.format("edit %s", log_path))
+end, {
+ desc = "Show LSP log",
+})
+
+vim.api.nvim_create_user_command("LspRestart", "lsp restart", {
+ desc = "Restart LSP",
+})
+
+--- show LSP progress (works on Ghostty)
+vim.api.nvim_create_autocmd("LspProgress", {
+ callback = function(ev)
+ local value = ev.data.params.value
+ vim.api.nvim_echo({ { value.message or "done" } }, false, {
+ id = "lsp." .. ev.data.client_id,
+ kind = "progress",
+ source = "vim.lsp",
+ title = value.title,
+ status = value.kind ~= "end" and "running" or "success",
+ percent = value.percentage,
+ })
+ end,
+})
+
+-- this controls the LSP inlayHints behavior
+vim.g.lsp_inlay_hint_enabled = false
+
+local update_inlayhint = function(enable)
+ -- Some LSP server supports inlay hint, but disable this feature by default, so you may need to
+ -- enable inlay hint in the LSP server config.
+ vim.lsp.inlay_hint.enable(enable)
+end
+
+vim.api.nvim_create_user_command("LspInlayHints", function(context)
+ -- vim.print("context", context)
+ if context["args"] == "enable" then
+ vim.g.lsp_inlay_hint_enabled = true
+ end
+
+ if context["args"] == "disable" then
+ vim.g.lsp_inlay_hint_enabled = false
+ end
+
+ update_inlayhint(vim.g.lsp_inlay_hint_enabled)
+end, {
+ bang = false,
+ nargs = 1,
+ force = true,
+ desc = "Toggle LSP inlayHints",
+ complete = function()
+ return { "enable", "disable" }
+ end,
+})
diff --git a/lua/lsp_utils.lua b/lua/lsp_utils.lua
new file mode 100644
index 00000000..4345c3c9
--- /dev/null
+++ b/lua/lsp_utils.lua
@@ -0,0 +1,15 @@
+local M = {}
+
+M.get_default_capabilities = function()
+ local capabilities = vim.lsp.protocol.make_client_capabilities()
+
+ -- required by nvim-ufo
+ capabilities.textDocument.foldingRange = {
+ dynamicRegistration = false,
+ lineFoldingOnly = true,
+ }
+
+ return capabilities
+end
+
+return M
diff --git a/lua/mappings.lua b/lua/mappings.lua
index fb7091e2..60539674 100644
--- a/lua/mappings.lua
+++ b/lua/mappings.lua
@@ -1,5 +1,4 @@
local keymap = vim.keymap
-local api = vim.api
local uv = vim.uv
-- Save key strokes (now we do not need to press shift to enter command mode).
@@ -24,19 +23,6 @@ keymap.set("n", "q", "x", { silent = true, desc = "quit current
-- Quit all opened buffers
keymap.set("n", "Q", "qa!", { silent = true, desc = "quit nvim" })
--- Navigation in the location and quickfix list
-keymap.set("n", "[l", "lpreviouszv", { silent = true, desc = "previous location item" })
-keymap.set("n", "]l", "lnextzv", { silent = true, desc = "next location item" })
-
-keymap.set("n", "[L", "lfirstzv", { silent = true, desc = "first location item" })
-keymap.set("n", "]L", "llastzv", { silent = true, desc = "last location item" })
-
-keymap.set("n", "[q", "cpreviouszv", { silent = true, desc = "previous qf item" })
-keymap.set("n", "]q", "cnextzv", { silent = true, desc = "next qf item" })
-
-keymap.set("n", "[Q", "cfirstzv", { silent = true, desc = "first qf item" })
-keymap.set("n", "]Q", "clastzv", { silent = true, desc = "last qf item" })
-
-- Close location list or quickfix list if they are present, see https://superuser.com/q/355325/736190
keymap.set("n", [[\x]], "windo lclose cclose ", {
silent = true,
@@ -44,21 +30,33 @@ keymap.set("n", [[\x]], "windo lclose cclose ", {
})
-- Delete a buffer, without closing the window, see https://stackoverflow.com/q/4465095/6064933
-keymap.set("n", [[\d]], "bprevious bdelete #", {
+keymap.set("n", [[\db]], "bprevious bdelete #", {
silent = true,
- desc = "delete buffer",
+ desc = "Delete current buffer",
})
--- Insert a blank line below or above current line (do not move the cursor),
--- see https://stackoverflow.com/a/16136133/6064933
-keymap.set("n", "o", "printf('m`%so``', v:count1)", {
- expr = true,
- desc = "insert line below",
+keymap.set("n", [[\dB]], function()
+ local buf_ids = vim.api.nvim_list_bufs()
+ local cur_buf = vim.api.nvim_win_get_buf(0)
+
+ for _, buf_id in pairs(buf_ids) do
+ -- do not Delete unlisted buffers, which may lead to unexpected errors
+ if vim.api.nvim_get_option_value("buflisted", { buf = buf_id }) and buf_id ~= cur_buf then
+ vim.api.nvim_buf_delete(buf_id, { force = true })
+ end
+ end
+end, {
+ desc = "Delete other buffers",
})
-keymap.set("n", "O", "printf('m`%sO``', v:count1)", {
- expr = true,
- desc = "insert line above",
+keymap.set("n", [[\dt]], "tabclose", {
+ silent = true,
+ desc = "Delete current tab",
+})
+
+keymap.set("n", [[\dT]], "tabonly", {
+ silent = true,
+ desc = "Delete other tabs",
})
-- Move the cursor based on physical lines, not the actual lines.
@@ -80,21 +78,18 @@ keymap.set({ "n", "x" }, "L", "g_")
keymap.set("x", "<", "", ">gv")
--- Edit and reload nvim config file quickly
-keymap.set("n", "ev", "tabnew $MYVIMRC tcd %:h", {
- silent = true,
- desc = "open init.lua",
-})
-
+-- Restart nvim
keymap.set("n", "sv", function()
- vim.cmd([[
- update $MYVIMRC
- source $MYVIMRC
- ]])
- vim.notify("Nvim config successfully reloaded!", vim.log.levels.INFO, { title = "nvim-config" })
+ vim.print("Use ZR to restart nvim instead!")
+end)
+
+keymap.set("n", "ZR", function()
+ local current_buf_path = vim.fn.expand("%")
+ local restart_cmd = string.format("restart edit %s", current_buf_path)
+ vim.cmd(restart_cmd)
end, {
silent = true,
- desc = "reload init.lua",
+ desc = "Restart nvim",
})
-- Reselect the text that has just been pasted, see also https://stackoverflow.com/a/4317090/6064933.
@@ -104,7 +99,7 @@ keymap.set("n", "v", "printf('`[%s`]', getregtype()[0])", {
})
-- Always use very magic mode for searching
-keymap.set("n", "/", [[/\v]])
+-- keymap.set("n", "/", [[/\v]])
-- Search in selected region
-- xnoremap / :call feedkeys('/\%>'.(line("'<")-1).'l\%<'.(line("'>")+1)."l")
@@ -128,25 +123,47 @@ keymap.set("n", "cc", '"_cc')
keymap.set("x", "c", '"_c')
-- Remove trailing whitespace characters
-keymap.set("n", "", "