从0安装并配置Neovim,并配置C,Python,Rust相关插件以及颜色主题

os: ubuntu22.04
shell: bash

1.安装Neovim

自行去neovim的github下载,apt下载的版本太低(版本至少大于0.8.x,apt下载的版本在0.8之下),后续插件会出问题
链接: Neovim Install

2.安装rust, c和python相关

# 安装 C 编译器和相关工具
sudo apt install build-essential -y

# 安装 Python3 及相关依赖
sudo apt install python3 python3-pip python3-venv -y

# 安装 Rust,通过 rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
source $HOME/.cargo/env

# 安装 Node.js (需要 Coc.nvim 插件支持)
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt install -y nodejs

3.配置Neovim

Neovim的配置文件有两种风格,一个是init.vim,另一个是init.lua,这里使用init.lua来配置

3.1 创建配置文件

创建init.lua并编辑

mkdir -p ~/.config/nvim

3.2 安装packer.nvim插件管理器

安装

git clone --depth 1 https://github.com/wbthomason/packer.nvim\
  ~/.local/share/nvim/site/pack/packer/start/packer.nvim

3.3 配置init.lua

-- 加载 packer.nvim 插件管理器
vim.cmd [[packadd packer.nvim]]

-- 显示行号
vim.opt.number = true

require('packer').startup(function()
  -- Packer 可以管理自己
  use 'wbthomason/packer.nvim'

  -- 常用插件
  use 'nvim-lua/plenary.nvim' -- Lua 实用工具

  -- LSP 配置和安装
  use 'neovim/nvim-lspconfig' -- LSP 客户端配置

  -- 自动完成插件
  use 'hrsh7th/nvim-cmp' -- 自动补全
  use 'hrsh7th/cmp-nvim-lsp' -- LSP 补全源
  use 'hrsh7th/cmp-buffer' -- 缓冲区补全
  use 'hrsh7th/cmp-path' -- 路径补全

  -- 语法高亮和代码分析插件
  use 'nvim-treesitter/nvim-treesitter'

end)

报错推出后,进入 nvim输入:PackerSync安装插件,并等待finished

3.4配置LSP

安装LSP

# 安装 Python 的 LSP 服务器
pip3 install 'python-lsp-server[all]'

# 安装 C/C++ 的 LSP 服务器
sudo apt install clangd -y

# 安装 Rust 的 LSP 服务器
rustup component add rust-analyzer

编辑 ~/.config/nvim/init.lua,添加 LSP 的配置

local lspconfig = require('lspconfig')

-- 配置 Python 的 LSP
lspconfig.pylsp.setup{}

-- 配置 C/C++ 的 LSP
lspconfig.clangd.setup{}

-- 配置 Rust 的 LSP
lspconfig.rust_analyzer.setup{}

继续编辑~/.config/nvim/init.lua,实现自动补全

-- nvim-cmp 配置
local cmp = require'cmp'

cmp.setup({
  snippet = {
    expand = function(args)
      vim.fn["vsnip#anonymous"](args.body) -- For `vsnip` users.
    end,
  },
  mapping = {
    ['<Tab>'] = cmp.mapping.select_next_item(),
    ['<S-Tab>'] = cmp.mapping.select_prev_item(),
    ['<CR>'] = cmp.mapping.confirm({ select = true }), -- Enter 键确认补全
  },
  sources = cmp.config.sources({
    { name = 'nvim_lsp' }, -- LSP 源
    { name = 'buffer' }, -- 缓冲区源
  })
})

继续编辑~/.config/nvim/init.lua,实现语法高亮

-- 配置 Treesitter
require'nvim-treesitter.configs'.setup {
  ensure_installed = { "c", "cpp", "python", "rust" }, -- 自动安装这些语言的语法解析器
  highlight = { enable = true },
}

4.可选插件

4.1 文件浏览器

kyazdani42/nvim-tree.lua 文件浏览器,这个装上后Neovim和常用IDE差别就不大了

编辑.config/nvim/init.lua

-- nvim-tree 配置
require('nvim-tree').setup {
  disable_netrw       = true,    -- 禁用 netrw
  hijack_netrw        = true,    -- 接管 netrw
  update_cwd          = true,    -- 更新当前工作目录
  diagnostics = {
    enable = true,               -- 启用 LSP 诊断
    show_on_dirs = true,         -- 在目录上显示诊断信息
    icons = {
      hint = "",
      info = "",
      warning = "",
      error = "",
    }
  },
  view = {
    width = 30,                  -- 设置窗口宽度
    side = 'left',               -- 将文件树显示在左侧
    adaptive_size = true,        -- 自适应窗口大小
  },
  renderer = {
    icons = {
      glyphs = {
        default = "",           -- 文件默认图标
        symlink = "",           -- 符号链接图标
        folder = {
          arrow_open = "",      -- 打开文件夹箭头
          arrow_closed = "",    -- 关闭文件夹箭头
          default = "",
          open = "",
          empty = "",
          empty_open = "",
          symlink = "",
          symlink_open = "",
        },
      },
    },
  },
  actions = {
    open_file = {
      quit_on_open = true,        -- 打开文件后自动关闭 nvim-tree
    },
  },
  -- 这里自定义快捷键映射,新的配置方式
  on_attach = function(bufnr)
    local api = require('nvim-tree.api')

    local function opts(desc)
      return { desc = "nvim-tree: " .. desc, buffer = bufnr, noremap = true, silent = true, nowait = true }
    end

    -- 默认快捷键配置示例
    vim.keymap.set('n', '<C-]>', api.tree.change_root_to_node, opts('CD'))
    vim.keymap.set('n', '<C-n>', ':NvimTreeToggle<CR>', opts('Toggle NvimTree'))
    vim.keymap.set('n', 'u', api.tree.change_root_to_parent, opts('Up'))
  end,
}

进入nvim :PackerSync安装插件

4.2 底部状态栏

lualine.nvim显示当前编辑器的状态信息,如文件名,光标位置等

在packer里添加

-- 状态栏插件 lualine
  use {
    'hoob3rt/lualine.nvim',
    requires = { 'kyazdani42/nvim-web-devicons', opt = true }
  }
  

进入nvim :PackerSync 安装插件

配置

-- 基本配置 lualine
require('lualine').setup {
  options = {
    icons_enabled = true,
    theme = 'auto', -- 可以根据需要选择不同主题,比如 'gruvbox', 'dracula' 等
    component_separators = { left = '|', right = '|'},
    section_separators = { left = '', right = ''},
    disabled_filetypes = {},
  },
  sections = {
    lualine_a = {'mode'},
    lualine_b = {'branch'},
    lualine_c = {'filename'},
    lualine_x = {'encoding', 'fileformat', 'filetype'},
    lualine_y = {'progress'},
    lualine_z = {'location'}
  },
  inactive_sections = {
    lualine_a = {},
    lualine_b = {},
    lualine_c = {'filename'},
    lualine_x = {'location'},
    lualine_y = {},
    lualine_z = {}
  },
  tabline = {},
  extensions = {}
}

主题颜色

navarasu/onedark.nvim onedark,vsocde转过来的,还是喜欢这个

packer里添加

use 'navarasu/onedark.nvim'    -- 安装 onedark 主题

init.lua中添加

-- 加载 onedark 主题
require('onedark').setup {
  style = 'dark' -- 选择主题风格,有 dark, darker, cool, deep, warm, warmer, light
}
require('onedark').load()  -- 应用主题

init.lua中的lualina中的主题也改一下,如下

require('lualine').setup {
  options = {
    theme = 'onedark',  -- 设置 lualine 使用 onedark 主题
    section_separators = '',  -- 你可以自定义分隔符
    component_separators = '|', -- 设置组件分隔符
  },
}

至此,完成

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值