neovim 从 0 开始配置优化

简介

neovim 是从 vim 的一个分支 fork 出来的,从性能等方面来讲都比 vim 强很多,所以从 vim 转到 neovim 了。

本文涉及的 neovim 配置都在 https://git.zeekling.cn/linux/nvim-config/src/branch/master 里面。

快速使用方法:

git clone https://git.zeekling.cn/linux/nvim-config.git ~/.config/nvim
# 或者
git clone ssh://git@git.zeekling.cn:222/linux/nvim-config.git ~/.config/nvim

打开 nvim 就可以自动安装插件了。

快捷键

快捷键 用途 分类
<space>bp 跳转到上一个 buffer Buffer
<space>bp 跳转到下一个 buffer Buffer
<space>ds 在左边显示目录数 Dir
<space>dc 关闭左边的目录树 Dir
<space>mks 预览 Markdown Markdown
<space>mkc 关闭预览 Markdown Markdown

帮助

快捷键记不住的时候可以按<space> 弹出帮助的分类,按照分类查找快捷键。

从 0 开始配置

初始化插件管理器

init.lua 为 neovim 的默认配置,一般在~/.config/nvim 下面。在文件里面写如下面内容,关键的一行为:require("lazynvim-init"),表示需要加载插件 lazynvim-init,用于初始化 lazy 插件管理器。

vim.g.mapleader = " "
vim.o.background = "light"
local vim = vim

require("lazynvim-init")

在新建~/.config/nvim/lua/lazynvim-init.lua 文件,写入下面内容,核心内容是初始化 lazy 插件管理器。require("lazy").setup("plugins") 主要是保证 lazy 插件加载~/.config/nvim/lua/plugins 下面的 lua 文件作为插件的配置。

vim.wo.number = true
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
	vim.fn.system({
		"git",
		"clone",
		"--filter=blob:none",
		"git@github.com:folke/lazy.nvim.git",
		"--branch=stable", -- latest stable release
		lazypath,
	})
end
-- 
-- 2. 将 lazypath 设置为运行时路径
	-- rtp(runtime path)
	-- nvim进行路径搜索的时候,除已有的路径,还会从prepend的路径中查找
-- 否则,下面 require("lazy") 是找不到的
vim.opt.rtp:prepend(lazypath)

-- 3. 加载lazy.nvim模块
require("lazy").setup("plugins")

插件配置

必选插件

~/.config/nvim/lua/plugins/plugin-base.lua 文件里面配置了个人认为必选的插件。

return {
  {
    "nathom/filetype.nvim",
    lazy = true,
    event = "User FileOpened",
    config = function()
        require("filetype").setup({
            overrides = {
                extensions = {
                    h = "cpp",
                },
            }
        })
    end
  },
  {
    "HiPhish/nvim-ts-rainbow2",
    -- Bracket pair rainbow colorize
    lazy = true,
    event = { "User FileOpened" },
  },
  {
    "romgrk/nvim-treesitter-context",
    lazy = true,
    event = { "User FileOpened" },
    config = function()
        require("treesitter-context").setup({
            enable = true,
            throttle = true,
            max_lines = 0,
            patterns = {
                default = {
                    "class",
                    "function",
                    "method",
                },
            },
        })
    end,
  },
  {
    "windwp/nvim-spectre",
    lazy = true,
    cmd = { "Spectre" },
    config = function()
        require("spectre").setup()
    end,
  },
  {
    "andymass/vim-matchup",
    -- Highlight, jump between pairs like if..else
    lazy = true,
    event = { "User FileOpened" },
    config = function()
        vim.g.matchup_matchparen_offscreen = { method = "popup" }
        lvim.builtin.treesitter.matchup.enable = true
    end,
  },
  {
    "rcarriga/nvim-notify",
    lazy = true,
    event = "VeryLazy",
    config = function()
        local notify = require("notify")
        notify.setup({
            -- "fade", "slide", "fade_in_slide_out", "static"
            stages = "static",
            on_open = nil,
            on_close = nil,
            timeout = 3000,
            fps = 1,
            render = "default",
            background_colour = "Normal",
            max_width = math.floor(vim.api.nvim_win_get_width(0) / 2),
            max_height = math.floor(vim.api.nvim_win_get_height(0) / 4),
            -- minimum_width = 50,
            -- ERROR > WARN > INFO > DEBUG > TRACE
            level = "TRACE",
        })

        vim.notify = notify
    end,
  },
  {
    "folke/noice.nvim",
    enabled = ENABLE_NOICE,
    lazy = true,
    event = { "BufRead", "BufNewFile" },
    dependencies = { "rcarriga/nvim-notify", "MunifTanjim/nui.nvim" },
    config = function()
        require("noice").setup({
            lsp = {
                progress = {
                    enabled = false,
                },
            },
            presets = {
                bottom_search = false,
                command_palette = true,
                long_message_to_split = true,
                inc_rename = false,
                lsp_doc_border = true,
            },
            messages = {
                enabled = true,
                view = "notify",
                view_error = "notify",
                view_warn = "notify",
                view_history = "messages",
                view_search = "virtualtext",
            },
            health = {
                checker = false,
            },
        })
    end,
  },
  { "lukas-reineke/cmp-under-comparator", lazy = true },
  {
    "ray-x/cmp-treesitter",
    lazy = true,
  },
  {
    'nvim-lualine/lualine.nvim',
    config = function()
       require('lualine').setup()
    end
  },
  {
    "nvim-neo-tree/neo-tree.nvim",
    branch = "v3.x",
    dependencies = {
      "nvim-lua/plenary.nvim",
      "nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
      "MunifTanjim/nui.nvim",
      -- "3rd/image.nvim", -- Optional image support in preview window: See `# Preview Mode` for more information
    }
  },
  { 
   "tiagovla/scope.nvim",
   lazy = false
  },
  {
        "akinsho/bufferline.nvim",
        tag = "v3.*",
        config = function()
            vim.g.termguicolors = true
 
            require("bufferline").setup()
        end,
    },
    {
      'nvimdev/dashboard-nvim',
      event = 'VimEnter',
      config = function()
        require('dashboard').setup {
         -- config
       }
      end,
     dependencies = { {'nvim-tree/nvim-web-devicons'}}
  },
  {
    "azabiong/vim-highlighter",
  },
  {"stevearc/dressing.nvim", opts = {}}

}

Markdown

个人使用 Markdown 比较多,所以配置了 Markdown 的插件,在~/.config/nvim/lua/plugins/markdown.lua 文件里面配置下面:

return {
    "iamcco/markdown-preview.nvim",
    cmd = { "MarkdownPreviewToggle", "MarkdownPreview", "MarkdownPreviewStop" },
    ft = { "markdown" },
    build = function()
        vim.fn["mkdp#util#install"]()
    end,
    lazy = true;
}

自动补全

在~/.config/nvim/lua/plugins/auto-complete.lua 文件里面配置了自动补全的配置:

return {
      {
        "hrsh7th/nvim-cmp",
        dependencies = {
            "hrsh7th/cmp-buffer",
            "hrsh7th/cmp-path",
            "hrsh7th/cmp-nvim-lsp",
            "L3MON4D3/LuaSnip",
            "saadparwaiz1/cmp_luasnip",
        },
        config = function()
            local cmp = require("cmp")
 
            cmp.setup({
                snippet = {
                    expand = function(args)
                        require("luasnip").lsp_expand(args.body)
                    end,
                },
                mapping = cmp.mapping.preset.insert({
                    ['<C-b>'] = cmp.mapping.scroll_docs(-4),
                    ['<C-f>'] = cmp.mapping.scroll_docs(4),
                    ['<C-Space>'] = cmp.mapping.complete(),
                    ['<C-e>'] = cmp.mapping.abort(),
                    ['<CR>'] = cmp.mapping.confirm({ select = true }),
                }),
                sources = cmp.config.sources({
                    { name = 'nvim_lsp' },
                    { name = 'luasnip' },
                }, {
                    { name = 'buffer' },
                    { name = "path" },
                }),
            })
        end,
    }, 
    {
      "neovim/nvim-lspconfig"
    }
}

快捷键以及提示

当插件过多之后,发现快捷键记不住,所以插件 folke/which-key.nvim 很有必要。配置如下:

return {
    {
        "folke/which-key.nvim",
        config = function()
            vim.o.timeout = true
            vim.o.timeoutlen = 300
 
            local wk = require("which-key")
            -- 快捷键在这里定义
	    wk.register({
               ["<leader>"] = {
	          b = {
                    name="Buffer",
	              p={"<cmd>BufferLineCyclePrev<cr>", "Go to pre buffer"},
	              n={"<cmd>BufferLineCycleNext<cr>", "Go to next buffer"},
	              d={"<cmd>bd<cr>", "delete current buffer"}
	          },
                 d = {
                   name="dirTree",
                   s={"<cmd>NvimTreeToggle<cr>", "show dir tree on left"},
                   c={"<cmd>NvimTreeClose<cr>", "hidden dir tree on left"}
	        },
	         m = {
                   name="Markdown",
	              k={
                        name="Markdown",
		        s={"<cmd>MarkdownPreview<cr>", "MarkdownPreview"},
		        c={"<cmd>MarkdownPreviewStop<cr>", "MarkdownPreviewStop"}
	              }
	         },
		 s = {
                   name="Search"
		 }
           }
        }, {silent = true, noremap = true})
        wk.setup()
        end,
    },
}

使用空格可以唤出帮助。

其他

其他配置可以参考:https://git.zeekling.cn/linux/nvim-config/src/branch/master/lua/plugins

  • Vim

    Vim 是类 UNIX 系统文本编辑器 Vi 的加强版本,加入了更多特性来帮助编辑源代码。Vim 的部分增强功能包括文件比较(vimdiff)、语法高亮、全面的帮助系统、本地脚本(Vimscript)和便于选择的可视化模式。

    28 引用 • 66 回帖

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...