neovim 配置 (100%lua)

pre-acquisition

配置文件

入口文件

neovim 从 init.lua 文件加载配置。该文件位于 runtimepath 中。

runtimapath: 在 *nix 系统上为 ~/.config/nvim/lua,在 Windows 系统上为 ~/appdata/Local/nvim/lua)。

模块

Lua 模块通常位于您的 runtimepath 中的 lua/ 文件夹中这意味着您可以 require() 这些文件作为 Lua 模块

📂 ~/.config/nvim
├── 📂 lua  # 模块
│  ├── 🌑 myluamodule.lua
│  └── 📂 other_modules
│     ├── 🌑 anothermodule.lua
│     └── 🌑 init.lua
└── 🌑 init.lua  # 入口文件

动手做

1. 快捷键模块

  1. 创建 runtimepath :在 ~/.config/ 路径下创建 nvim/ 文件夹。
  2. runtimepath 中创建 lua/ 文件夹,init.lua 入口文件,在 lua/ 文件夹中创建 keymaps.lua 快捷键模块文件。
    📂 ~/.config/nvim
    ├── 📂 lua 
    │  └── 🌑 keymaps.lua
    └── 🌑 init.lua  # 入口文件
  3. 编辑 keymaps.lua 快捷键模块文件 ➤➤➤
  4. init.lua 入口文件中导入快捷键模块。
    require "keymaps"

2. 插件管理器

使用 Packer 作为插件管理器

packer.nvim: A use-package inspired plugin manager for Neovim. Uses native packages, supports Luarocks dependencies, written in Lua, allows for expressive config

  1. 安装

    Unix, Linux Installation

    git clone --depth 1 https://github.com/wbthomason/packer.nvim\
    ~/.local/share/nvim/site/pack/packer/start/packer.nvim
  2. lua/ 文件夹中创建并编辑 plugins.lua 文件。

    📂 ~/.config/nvim
    ├── 📂 lua 
    │  ├── 🌑 keymaps.lua
    │  └── 🌑 plugins.lua
    └── 🌑 init.lua  # 入口文件
    return require('packer').startup(function(use)
      -- packer插件本身
      use 'wbthomason/packer.nvim'
      -- 其他插件...
    end)
  3. init.lua 入口文件中导入插件管理器模块。

    require "keymaps"
    require "plugins.lua"
  4. neovim 中使用 :PackerSync 命令安装插件。此步骤需要网络能正常访问github

3. 主题模块

  1. 安装主题 catppuccin
    编辑 plugins.lua 文件

    return require('packer').startup(function(use)
      -- packer插件本身
      use 'wbthomason/packer.nvim'
      -- catppuccin 主题
      use { "catppuccin/nvim", as = "catppuccin" }
    end)
  2. lua/ 文件夹中创建并编辑 colorscheme.lua 文件。

    📂 ~/.config/nvim
    ├── 📂 lua 
    │  ├── 🌑 colorscheme.lua
    │  ├── 🌑 keymaps.lua
    │  └── 🌑 plugins.lua
    └── 🌑 init.lua  # 入口文件
    local colorscheme = "catppuccin"
    local status_ok, _ = pcall(vim.cmd, "colorscheme " .. colorscheme)
    
    if not status_ok then
      vim.notify("colorscheme " .. colorscheme .. " not found!")
      return
    end
    pcall() 用于捕获异常,当主题加载失败时,不至于 neovim 启动失败 vim.cmd(colorscheme .. colorscheme),等同于执行 :colorscheme catppuccin 命令
  3. init.lua 入口文件中导入主题模块。

    require "keymaps"
    require "plugins"
    require "colorscheme"
  4. 保存退出后,运行 :PackerSync 命令。

4. 文件浏览器模块

使用 nvim-tree.lua 作为文件浏览器。

nvim-tree: A file explorer tree for neovim written in lua

⚠️ nvim-tree 依赖 Nerd Fonts
Nerd Fonts ➤➤➤

💾 安装插件

编辑 plugins.lua 文件

return require('packer').startup(function(use)
  -- packer插件本身
  use 'wbthomason/packer.nvim'
  -- catppuccin 主题
  use { "catppuccin/nvim", as = "catppuccin" }
	-- nvim-tree 文件浏览器
	use {
		'nvim-tree/nvim-tree.lua',
		requires = {
			'nvim-tree/nvim-web-devicons', -- optional, for file icons
		},
		tag = 'nightly' -- optional, updated every week. (see issue #1193)
	}
end)

📃 配置文件

  • lua/ 文件夹中创建 conf/ 文件夹
  • conf/ 文件夹中创建 init.lua 文件
  • conf/ 文件夹中创建 nvim-tree.lua 文件
📂 ~/.config/nvim
├── 📂 lua 
│  ├── 📂 conf
│  │   ├── 🌑 init.lua
│  │   └── 🌑 nvim-tree.lua
│  ├── 🌑 colorscheme.lua
│  ├── 🌑 keymaps.lua
│  └── 🌑 plugins.lua
└── 🌑 init.lua  # 入口文件

init.lua 入口文件中导入 conf 模块

require "keymaps"
require "plugins"
require "colorscheme"
require "conf"

编辑 conf/init.lua 文件

require "conf.nvim-tree"

编辑 nvim-tree.lua 文件

local status_ok, nvim_tree = pcall(require, "nvim-tree")
if not status_ok then
  vim.notify("nvim-tree not found!")
  return
end
nvim_tree.set()  -- 使用默认配置
保存退出后,运行:PackerSync 快捷键设置

5. Buffer 管理模块

使用 bufferline 作为 Buffer 管理模块。

bufferline.nvim: A snazzy 💅 buffer line (with tabpage integration) for Neovim built using lua.

💾 安装插件
编辑 plugins.lua 文件

return require('packer').startup(function(use)
  -- packer插件本身
  use 'wbthomason/packer.nvim'
  -- catppuccin 主题
  use { "catppuccin/nvim", as = "catppuccin" }
	-- nvim-tree 文件浏览器
	use {
		'nvim-tree/nvim-tree.lua',
		requires = {
			'nvim-tree/nvim-web-devicons', -- optional, for file icons
		},
		tag = 'nightly' -- optional, updated every week. (see issue #1193)
	}
	-- buffer 管理
	use {'akinsho/bufferline.nvim', tag = "v3.*", requires = 'nvim-tree/nvim-web-devicons'}
end)

📃 配置文件

  • conf/ 文件夹中创建 bufferline.lua 文件
    📂 ~/.config/nvim
    ├── 📂 lua 
    │  ├── 📂 conf
    │  │   ├── 🌑 bufferline.lua
    │  │   ├── 🌑 init.lua
    │  │   └── 🌑 nvim-tree.lua
    │  ├── 🌑 colorscheme.lua
    │  ├── 🌑 keymaps.lua
    │  └── 🌑 plugins.lua
    └── 🌑 init.lua  # 入口文件

编辑 conf/init.lua 文件

require "conf.nvim-tree"
require "conf.bufferline.lua"

编辑 bufferline.lua 文件

local status_ok, bufferline = pcall(require, "bufferline")
if not status_ok then
  vim.notify("bufferline not found!")
  return
end

vim.opt.termguicolors = true
bufferline.setup ({
    options = {
    mode = "buffers", -- set to "tabs" to only show tabpages instead
    numbers = "ordinal", -- | "ordinal" | "buffer_id" | "both" | function({ ordinal, id, lower, raise }): string,
    --- @deprecated, please specify numbers as a function to customize the styling
    -- number_style = "superscript", --| "subscript" | "" | { "none", "subscript" }, -- buffer_id at index 1, ordinal at index 2
    close_command = "bdelete! %d",       -- can be a string | function, see "Mouse actions"
    right_mouse_command = "bdelete! %d", -- can be a string | function, see "Mouse actions"
    left_mouse_command = "buffer %d",    -- can be a string | function, see "Mouse actions"
    middle_mouse_command = nil,          -- can be a string | function, see "Mouse actions"
    -- NOTE: this plugin is designed with this icon in mind,
    -- and so changing this is NOT recommended, this is intended
    -- as an escape hatch for people who cannot bear it for whatever reason
		--
		indicator = {
			icon = '▎', -- this should be omitted if indicator style is not 'icon'
			style = 'icon',
		},
 
    buffer_close_icon = '',
    modified_icon = '●',
    close_icon = 'x',
    left_trunc_marker = '',
    right_trunc_marker = '',
    --- name_formatter can be used to change the buffer's label in the bufferline.
    --- Please note some names can/will break the
    --- bufferline so use this at your discretion knowing that it has
    --- some limitations that will *NOT* be fixed.
    name_formatter = function(buf)  -- buf contains a "name", "path" and "bufnr"
      -- remove extension from markdown files for example
      if buf.name:match('%.md') then
        return vim.fn.fnamemodify(buf.name, ':t:r')
      end
    end,
    max_name_length = 18,
    max_prefix_length = 15, -- prefix used when a buffer is de-duplicated
    tab_size = 18,
    diagnostics = false, --| "nvim_lsp" | "coc",
    diagnostics_update_in_insert = false,
    diagnostics_indicator = function(count, level, diagnostics_dict, context)
      return "("..count..")"
    end,
    -- NOTE: this will be called a lot so don't do any heavy processing here
    custom_filter = function(buf_number, buf_numbers)
      -- filter out filetypes you don't want to see
      if vim.bo[buf_number].filetype ~= "<i-dont-want-to-see-this>" then
        return true
      end
      -- filter out by buffer name
      if vim.fn.bufname(buf_number) ~= "<buffer-name-I-dont-want>" then
        return true
      end
      -- filter out based on arbitrary rules
      -- e.g. filter out vim wiki buffer from tabline in your work repo
      if vim.fn.getcwd() == "<work-repo>" and vim.bo[buf_number].filetype ~= "wiki" then
        return true
      end
      -- filter out by it's index number in list (don't show first buffer)
      if buf_numbers[1] ~= buf_number then
        return true
      end
    end,
    offsets = {{filetype = "NvimTree", text = "File Explorer", text_align="center"}}, -- | function , text_align = "left" | "center" | "right"}},
    show_buffer_icons = true, --| false, -- disable filetype icons for buffers
    show_buffer_close_icons = true, --| false,
    show_close_icon = true, --| false,
    show_tab_indicators = true, -- | false,
    persist_buffer_sort = true, -- whether or not custom sorted buffers should persist
    -- can also be a table containing 2 custom separators
    -- [focused and unfocused]. eg: { '|', '|' }
    separator_style = "thin", --| "slant" | "thick" | "thin" | { 'any', 'any' },
    enforce_regular_tabs = false, --| true,
    always_show_bufferline = true, -- | false,
    sort_by =  'directory',  -- ,'id' | 'extension' | 'relative_directory' | 'directory' | 'tabs' | function(buffer_a, buffer_b)
    --   -- add custom logic
    --   return buffer_a.modified > buffer_b.modified
    -- end
  }
})

🚗 保存退出后,运行:PackerSync

⌨️ 快捷键设置

6. 搜索模块

使用 telescope 作为搜索模块。

telescope.nvim is a highly extendable fuzzy finder over lists. Built on the latest awesome features from neovim core. Telescope is centered around modularity, allowing for easy customization. %}

💾 安装插件
编辑 plugins.lua 文件

return require('packer').startup(function(use)
  -- packer插件本身
  use 'wbthomason/packer.nvim'
  -- catppuccin 主题
  use { "catppuccin/nvim", as = "catppuccin" }
	-- nvim-tree 文件浏览器
	use {
		'nvim-tree/nvim-tree.lua',
		requires = {
			'nvim-tree/nvim-web-devicons', -- optional, for file icons
		},
		tag = 'nightly' -- optional, updated every week. (see issue #1193)
	}
	-- buffer 管理
	use {'akinsho/bufferline.nvim', tag = "v3.*", requires = 'nvim-tree/nvim-web-devicons'}
	-- telescope 搜索模块
  use {
    'nvim-telescope/telescope.nvim', tag = '0.1.0',
    -- or                            , branch = '0.1.x',
    requires = { {'nvim-lua/plenary.nvim'} }
  }
end)

📃 配置文件

  • conf/ 文件夹中创建 telescope.lua 文件
    📂 ~/.config/nvim
    ├── 📂 lua 
    │  ├── 📂 conf 
    │  │  ├── 🌑 init.lua
    │  │  ├── 🌑 nvim-tree.lua
    │  │  └── 🌑 telescope.lua
    │  ├── 🌑 colorscheme.lua
    │  ├── 🌑 keymaps.lua
    │  └── 🌑 plugins.lua
    └── 🌑 init.lua  # 入口文件

编辑 conf/init.lua 文件

require "conf.nvim-tree"
require "conf.telescope.lua"

编辑 telescope.lua 文件

local status_ok, telescope = pcall(require, "telescope")
if not status_ok then
  vim.notify("telescope not found!")
  return
end

telescope.setup {
  defaults = {

  },
  pickers = {
    find_files = {
      theme = "dropdown",
			previewer = false,  -- 搜索文件时的预览
    }
  },
  extensions = {
    
  },
}

🌟 使用 fzf 搜索引擎 telescope-fzf-native

🚗 保存退出后,运行:PackerSync

⌨️ 快捷键设置

7. 语法高亮

使用 nvim-treesitter 实现语法高亮。

nvim-treesitter: Nvim Treesitter configurations and abstraction layer.

💾 安装插件
编辑 plugins.lua 文件

return require('packer').startup(function(use)
  -- packer插件本身
  use 'wbthomason/packer.nvim'
  -- catppuccin 主题
  use { "catppuccin/nvim", as = "catppuccin" }
	-- nvim-tree 文件浏览器
	use {
		'nvim-tree/nvim-tree.lua',
		requires = {
			'nvim-tree/nvim-web-devicons', -- optional, for file icons
		},
		tag = 'nightly' -- optional, updated every week. (see issue #1193)
	}
	-- buffer 管理
	use {'akinsho/bufferline.nvim', tag = "v3.*", requires = 'nvim-tree/nvim-web-devicons'}
	-- telescope 搜索模块
  use {
    'nvim-telescope/telescope.nvim', tag = '0.1.0',
    -- or                            , branch = '0.1.x',
    requires = { {'nvim-lua/plenary.nvim'} }
  }
	-- nvim-treesitter 语法高亮
	use 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
end)

📃 配置文件

  • conf/ 文件夹中创建 treesitter.lua 文件
    📂 ~/.config/nvim
    ├── 📂 lua 
    │  ├── 📂 conf 
    │  │  ├── 🌑 init.lua
    │  │  ├── 🌑 nvim-tree.lua
    │  │  ├── 🌑 telescope.lua
    │  │  └── 🌑 treesitter.lua
    │  ├── 🌑 colorscheme.lua
    │  ├── 🌑 keymaps.lua
    │  └── 🌑 plugins.lua
    └── 🌑 init.lua  # 入口文件

编辑 conf/init.lua 文件

require "conf.nvim-tree"
require "conf.telescope"
require "conf.treesitter"

编辑 treesitter.lua 文件

local status_ok, configs = pcall(require, "nvim-treesitter.configs")
if not status_ok then
  vim.notify("treesitter not found!")
  return
end

configs.setup {
  ensure_installed = { "cpp", "c", "python", "go", "markdown", "json", "yaml", "html", "lua"  }, -- one of "all", "maintained" (parsers with maintainers), or a list of languages
	sync_install = false, -- install languages synchronously (only applied to `ensure_installed`)
  ignore_install = { "" }, -- List of parsers to ignore installing
  highlight = {
    enable = true, -- false will disable the whole extension
    disable = { "" }, -- list of language that will be disabled
    additional_vim_regex_highlighting = false,
  },
	indent = { enable = true, disable = { "yaml" } },
  context_commentstring = {
    enable = true,
    config = {
      -- Languages that have a single comment style
      typescript = "// %s",
      css = "/* %s */",
      scss = "/* %s */",
      html = "<!-- %s -->",
      svelte = "<!-- %s -->",
      vue = "<!-- %s -->",
      json = "",
    },
  },
  -- textobjects extension settings
  -- https://github.com/nvim-treesitter/nvim-treesitter-textobjects
  textobjects = {
    swap = {
      enable = false,
    },
    select = {
      enable = true,
      -- Automatically jump forward to textobj, similar to targets.vim
      lookahead = true,
      keymaps = {
        -- You can use the capture groups defined in textobjects.scm
        ["af"] = "@function.outer",
        ["if"] = "@function.inner",
        ["ac"] = "@class.outer",
        ["ic"] = "@class.inner",
      },
    },
    move = {
      enable = true,
      set_jumps = false, -- whether to set jumps in the jumplist
      goto_next_start = {
        ["]]"] = "@function.outer",
        -- ["]["] = "@function.outer",
      },
      goto_next_end = {
        ["]["] = "@function.outer",
        -- ["]["] = "@class.outer",
      },
      goto_previous_start = {
        ["[["] = "@function.outer",
        -- ["[]"] = "@function.outer",
      },
      goto_previous_end = {
        ["[]"] = "@function.outer",
        -- ["[]"] = "@class.outer",
      },
    },
    lsp_interop = {
      enable = false,
      border = 'none',
      peek_definition_code = {
        ["<leader>pf"] = "@function.outer",
        ["<leader>pF"] = "@class.outer",
      },
    },
  },
  textsubjects = {
    enable = false,
    keymaps = { ["."] = "textsubjects-smart", [";"] = "textsubjects-big" },
  },
  playground = {
    enable = false,
    disable = {},
    updatetime = 25, -- Debounced time for highlighting nodes in the playground from source code
    persist_queries = false, -- Whether the query persists across vim sessions
    keybindings = {
      toggle_query_editor = "o",
      toggle_hl_groups = "i",
      toggle_injected_languages = "t",
      toggle_anonymous_nodes = "a",
      toggle_language_display = "I",
      focus_language = "f",
      unfocus_language = "F",
      update = "R",
      goto_node = "<cr>",
      show_help = "?",
    },
  },
  rainbow = {
    enable = false,
    extended_mode = true, -- Highlight also non-parentheses delimiters, boolean or table: lang -> boolean
    max_file_lines = 1000, -- Do not enable for files with more than 1000 lines, int
  },
  autotag = { enable = false },
  -- matchup plugin
  -- https://github.com/andymass/vim-matchup
  matchup = {
    enable = false, -- mandatory, false will disable the whole extension
    -- disable = { "c", "ruby" },  -- optional, list of language that will be disabled
    -- [options]
  },
  -- autopairs plugin
  autopairs = {
    enable = false,
  },
}

-- 开启 Folding 模块
vim.opt.foldmethod = "expr"
vim.opt.foldexpr = "nvim_treesitter#foldexpr()"
-- 默认不要折叠
-- https://stackoverflow.com/questions/8316139/how-to-set-the-default-to-unfolded-when-you-open-a-file
vim.opt.foldlevel = 99

8. 状态栏模块

使用 lualine 作为状态栏。

lualine.nvim: A blazing fast and easy to configure Neovim statusline written in Lua.

💾 安装插件
编辑 plugins.lua 文件

return require('packer').startup(function(use)
  -- packer插件本身
  use 'wbthomason/packer.nvim'
  -- catppuccin 主题
  use { "catppuccin/nvim", as = "catppuccin" }
	-- nvim-tree 文件浏览器
	use {
		'nvim-tree/nvim-tree.lua',
		requires = {
			'nvim-tree/nvim-web-devicons', -- optional, for file icons
		},
		tag = 'nightly' -- optional, updated every week. (see issue #1193)
	}
	-- buffer 管理
	use {'akinsho/bufferline.nvim', tag = "v3.*", requires = 'nvim-tree/nvim-web-devicons'}
	-- telescope 搜索模块
  use {
    'nvim-telescope/telescope.nvim', tag = '0.1.0',
    -- or                            , branch = '0.1.x',
    requires = { {'nvim-lua/plenary.nvim'} }
  }
	-- nvim-treesitter 语法高亮
	use 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
	-- lualine 状态栏
	use {
		'nvim-lualine/lualine.nvim',
		requires = { 'kyazdani42/nvim-web-devicons', opt = true }
	}
end)

📃 配置文件

  • conf/ 文件夹中创建 lualine.lua 文件
    📂 ~/.config/nvim
    ├── 📂 lua 
    │  ├── 📂 conf 
    │  │  ├── 🌑 init.lua
    │  │  ├── 🌑 lualine.lua
    │  │  ├── 🌑 nvim-tree.lua
    │  │  ├── 🌑 telescope.lua
    │  │  └── 🌑 treesitter.lua
    │  ├── 🌑 colorscheme.lua
    │  ├── 🌑 keymaps.lua
    │  └── 🌑 plugins.lua
    └── 🌑 init.lua  # 入口文件

编辑 conf/init.lua 文件

require "conf.nvim-tree"
require "conf.telescope"
require "conf.treesitter"
require "conf.lualine"

编辑 lualine.lua 文件

local status_ok, lualine = pcall(require, "lualine")
if not status_ok then
  vim.notify("lualine not found!")
  return
end
lualine.setup()   --使用默认配置

🚗 保存退出后,运行:PackerSync

⌨️ 快捷键设置

9. 欢迎页

使用 alpha-nvim 作为欢迎页。

alpha-nvim: a lua powered greeter like vim-startify / dashboard-nvim.

💾 安装插件
编辑 plugins.lua 文件

return require('packer').startup(function(use)
  -- packer插件本身
  use 'wbthomason/packer.nvim'
  -- catppuccin 主题
  use { "catppuccin/nvim", as = "catppuccin" }
	-- nvim-tree 文件浏览器
	use {
		'nvim-tree/nvim-tree.lua',
		requires = {
			'nvim-tree/nvim-web-devicons', -- optional, for file icons
		},
		tag = 'nightly' -- optional, updated every week. (see issue #1193)
	}
	-- buffer 管理
	use {'akinsho/bufferline.nvim', tag = "v3.*", requires = 'nvim-tree/nvim-web-devicons'}
	-- telescope 搜索模块
  use {
    'nvim-telescope/telescope.nvim', tag = '0.1.0',
    -- or                            , branch = '0.1.x',
    requires = { {'nvim-lua/plenary.nvim'} }
  }
	-- nvim-treesitter 语法高亮
	use 'nvim-treesitter/nvim-treesitter', {'do': ':TSUpdate'}
	-- lualine 状态栏
	use {
		'nvim-lualine/lualine.nvim',
		requires = { 'kyazdani42/nvim-web-devicons', opt = true }
	}
	-- alpha-nvim 欢迎页
  use "goolord/alpha-nvim"
end)

📃 配置文件

  • conf/ 文件夹中创建 alpha.lua 文件
    📂 ~/.config/nvim
    ├── 📂 lua 
    │  ├── 📂 conf 
    │  │  ├── 🌑 alpha.lua
    │  │  ├── 🌑 init.lua
    │  │  ├── 🌑 lualine.lua
    │  │  ├── 🌑 nvim-tree.lua
    │  │  ├── 🌑 telescope.lua
    │  │  └── 🌑 treesitter.lua
    │  ├── 🌑 colorscheme.lua
    │  ├── 🌑 keymaps.lua
    │  └── 🌑 plugins.lua
    └── 🌑 init.lua  # 入口文件

编辑 conf/init.lua 文件

require "conf.nvim-tree"
require "conf.telescopea"
require "conf.treesitter"
require "conf.lualine"
require "conf.alpha"

编辑 alpha.lua 文件

local status_ok, alpha = pcall(require, "alpha")
if not status_ok then
	return
end

local dashboard = require("alpha.themes.dashboard")
dashboard.section.header.val = {
	[[                               __                ]],
	[[  ___     ___    ___   __  __ /\_\    ___ ___    ]],
	[[ / _ `\  / __`\ / __`\/\ \/\ \\/\ \  / __` __`\  ]],
	[[/\ \/\ \/\  __//\ \_\ \ \ \_/ |\ \ \/\ \/\ \/\ \ ]],
	[[\ \_\ \_\ \____\ \____/\ \___/  \ \_\ \_\ \_\ \_\]],
	[[ \/_/\/_/\/____/\/___/  \/__/    \/_/\/_/\/_/\/_/]],
}
dashboard.section.buttons.val = {
	dashboard.button("f", "  Find file", ":Telescope find_files <CR>"),
	dashboard.button("e", "  New file", ":ene <BAR> startinsert <CR>"),
	dashboard.button("p", "  Find project", ":Telescope projects <CR>"),
	dashboard.button("r", "  Recently used files", ":Telescope oldfiles <CR>"),
	dashboard.button("t", "  Find text", ":Telescope live_grep <CR>"),
	dashboard.button("c", "  Configuration", ":e ~/.config/nvim/init.lua <CR>"),
	dashboard.button("q", "  Quit Neovim", ":qa<CR>"),
}

local function footer()
-- NOTE: requires the fortune-mod package to work
	-- local handle = io.popen("fortune")
	-- local fortune = handle:read("*a")
	-- handle:close()
	-- return fortune
	return "Guoguo"
end

dashboard.section.footer.val = footer()

dashboard.section.footer.opts.hl = "Type"
dashboard.section.header.opts.hl = "Include"
dashboard.section.buttons.opts.hl = "Keyword"

dashboard.opts.opts.noautocmd = true
-- vim.cmd([[autocmd User AlphaReady echo 'ready']])
alpha.setup(dashboard.opts)

🚗 保存退出后,运行:PackerSync

⌨️ 快捷键设置

10. lsp 模块

neovim lsp 文档 ➤➤➤ 📚📚📚

语言服务器协议(LSP)定义了编辑器或IDE与语言服务器之间使用的协议,该协议提供自动补全、转到定义、查找所有引用等语言功能。
Nvim提供了一个LSP客户端,但服务器由第三方提供。

  1. 安装 Nvim LSP 客户端:nvim-lspconfig
  2. 安装 LSP 服务器管理器:mason.nvim

    mason.nvim: Portable package manager for Neovim that runs everywhere Neovim runs. Easily install and manage LSP servers, DAP servers, linters, and formatters.

  3. 安装 LSP 服务器

💾 安装插件

编辑 plugins.lua 文件

return require('packer').startup(function(use)
  -- packer插件本身
  use 'wbthomason/packer.nvim'
	-- catppuccin 主题
  use { "catppuccin/nvim", as = "catppuccin" }
	-- nvim-tree 文件浏览器
	use {
		'nvim-tree/nvim-tree.lua',
		requires = {
			'nvim-tree/nvim-web-devicons', -- optional, for file icons
		},
		tag = 'nightly' -- optional, updated every week. (see issue #1193)
	}
  -- lsp
	use {
			"williamboman/mason.nvim",
			"williamboman/mason-lspconfig.nvim",
			"neovim/nvim-lspconfig",
	}
end)

📃 配置文件

  • lua/ 文件夹中创建 lsp/ 文件夹
  • lsp/ 文件夹中创建 init.lua 文件
  • lsp/ 文件夹中创建 mason.lua 文件
    📂 ~/.config/nvim
    ├── 📂 lua 
    │  ├── 📂 conf 
    │  │  ├── 🌑 alpha.lua
    │  │  ├── 🌑 init.lua
    │  │  ├── 🌑 lualine.lua
    │  │  ├── 🌑 nvim-tree.lua
    │  │  ├── 🌑 telescope.lua
    │  │  └── 🌑 treesitter.lua
    │  ├── 📂 lsp 
    │  │  ├── 🌑 init.lua
    │  │  └── 🌑 mason.lua
    │  ├── 🌑 colorscheme.lua
    │  ├── 🌑 keymaps.lua
    │  └── 🌑 plugins.lua
    └── 🌑 init.lua  # 入口文件

init.lua 入口文件中导入 lsp 模块

require "keymaps"
require "plugins"
require "colorscheme"
require "lsp"

编辑 lsp/init.lua 文件

require "lsp.mason"

编辑 mason.lua 文件

local status_ok, mason = pcall(require, "mason")
if not status_ok then
  vim.notify("mason not found!")
  return
end

local status_ok, mason_lspconfig = pcall(require, "mason-lspconfig")
if not status_ok then
  vim.notify("mason-lspconfig not found!")
  return
end

local status_ok, lspconfig = pcall(require, "lspconfig")
if not status_ok then
	vim.notiry("lspconfig not found")
	return
end

mason.setup({
    ui = {
        icons = {
            package_installed = "✓",
            package_pending = "➜",
            package_uninstalled = "✗"
        }
    }
})

mason_lspconfig.setup({
	ensure_installed = { "sumneko_lua", "rust_analyzer", "gopls", "jsonls", "tsserver", "pyright" },
	automatic_installation = true,
})

mason_lspconfig.setup_handlers {
		-- The first entry (without a key) will be the default handler
		-- and will be called for each installed server that doesn't have
		-- a dedicated handler.
		function (server_name) -- default handler (optional)
				require("lspconfig")[server_name].setup {}
		end,
		-- Next, you can provide a dedicated handler for specific servers.
		-- For example, a handler override for the `rust_analyzer`:
		["sumneko_lua"] = function ()
			lspconfig.sumneko_lua.setup {
				 settings = {
						 Lua = {
								 diagnostics = {
										 globals = { "vim" }
								 }
						 }
				 }
			}
		end,

}

🚗 执行:PackerSync命令

🩻 执行 :checkhealth mason 检查 maosn 安装情况

🚀 执行 :mason 安装相应的 LSP

11. dap 模块

安装字体(用于显示图标)

Nerd Fonts 是一个使用大量字体图标来解决程序员在开发过程中缺少合适字体的问题的项目。

brew tap homebrew/cask-fonts
brew install --cask font-caskaydia-cove-nerd-font
# or
curl -fLo "Caskaydia Cove Nerd Font Complete.otf" \
https://github.com/ryanoasis/nerd-fonts/raw/HEAD/patched-fonts/<FONT_PATH>/complete/Caskaydia%20Cove%20Nerd%20Font%20Complete.otf

Referance

[1] jjmyag,从Goland转到Neovim


neovim 配置 (100%lua)
http://example.com/2022/12/09/neovim/
作者
Guoguo
发布于
2022年12月9日
许可协议