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

# neovim 


标 题:《neovim 从0开始配置优化
作 者:zeekling
提 示:转载请注明文章转载自个人博客:浪浪山旁那个村

评论

  1. Linux操作系统下面的一个比较好用的编辑器,简单编辑点东西还是挺方便的。

  2. 这个主要功能是什么东西呀?

取消