nvim 打造一个适合自己的开发环境

编辑器以前一直使用vscode + vim 插件,但是由于疫情在家工作任务不太多,就自己使用nvim搭建了一套适合自己的开发环境。

这里分享一下自己主要使用的插件,默认配置以及快捷点设置。

github

以下以mac为例,windows 有些插件可能需要去对应的github 查看安装教程。
安装之后的效果是:

nvim 安装

前往github neovim 下载对应版本和系统的文件。
这里使用在线编译进行安装
下载版本为: 0.7

我专门写了一个shell脚本你可以:

// 创建目录
1. mkdir shell
2. wget https://github.com/zlongCoding/shell/blob/main/nvim.sh
/**
|    bashPath='修改成自己的主路径' 
|  
|   nvimPath="${bashPath}/Desktop/nvim"  //nvim下载地址
|   nvimConfig="${bashPath}/.config/nvim"   //nvim 配置地址
|   zshrcPath="${bashPath}/.zshrc"       //zshrc地址
|   nvimGitPath='https://github.com/neovim/neovim' 
**/
3. ./nvim.sh

功能主要是:
前端开发,go开发、其他的可以自行配置

如果按照我自己的配置其实这样子已经足够了,但是下面我会简单说一下使用的插件。

$ cd ~/.config/nvim
$ touch init.lua && mkdir lua
├── lua
│   ├── autocmds.lua
│   ├── basic.lua
│   ├── colorscheme.lua
│   ├── keybindings.lua
│   ├── plugins.lua
│   ├── shell.lua
│   ├── lsp
│   ├── plugin-config
│   ├── shell
│   ├── spector
│   └── utils
└── init.lua

上面是我们的主文件目录

basic.lua

修改vim基本配置

-- utf8
vim.g.encoding = "UTF-8"
vim.o.fileencoding = "utf-8"
-- jkhl 移动时光标周围保留8行
vim.o.scrolloff = 8
vim.o.sidescrolloff = 8
-- 使用相对行号
vim.wo.number = true
vim.wo.relativenumber = true
-- 高亮所在行
vim.wo.cursorline = true
-- 显示左侧图标指示列
vim.wo.signcolumn = "yes"
-- 右侧参考线,超过表示代码太长了,考虑换行
vim.wo.colorcolumn = "80"
-- 缩进2个空格等于一个Tab
vim.o.tabstop = 2
vim.bo.tabstop = 2
vim.o.softtabstop = 2
vim.o.shiftround = true
-- >> << 时移动长度
vim.o.shiftwidth = 2
vim.bo.shiftwidth = 2
-- 空格替代tab
vim.o.expandtab = true
vim.bo.expandtab = true
-- 新行对齐当前行
vim.o.autoindent = true
vim.bo.autoindent = true
vim.o.smartindent = true
-- 搜索大小写不敏感,除非包含大写
vim.o.ignorecase = true
vim.o.smartcase = true
-- 搜索不要高亮
vim.o.hlsearch = false
-- 边输入边搜索
vim.o.incsearch = true
-- 命令行高为2,提供足够的显示空间
vim.o.cmdheight = 1
-- 当文件被外部程序修改时,自动加载
vim.o.autoread = true
vim.bo.autoread = true
-- 禁止折行
vim.wo.wrap = false
-- 光标在行首尾时<Left><Right>可以跳到下一行
vim.o.whichwrap = "<,>,[,]"
-- 允许隐藏被修改过的buffer
vim.o.hidden = true
-- 鼠标支持
vim.o.mouse = "a"
-- 禁止创建备份文件
vim.o.backup = false
vim.o.writebackup = false
vim.o.swapfile = false
-- smaller updatetime
vim.o.updatetime = 300
-- 设置 timeoutlen 为等待键盘快捷键连击时间500毫秒,可根据需要设置
-- 遇到问题详见:https://github.com/nshen/learn-neovim-lua/issues/1
vim.o.timeoutlen = 500
-- split window 从下边和右边出现
vim.o.splitbelow = true
vim.o.splitright = true
-- 自动补全不自动选中
vim.g.completeopt = "menu,menuone,noselect,noinsert"
-- 样式
vim.o.termguicolors = true
vim.opt.termguicolors = true
-- 是否显示不可见字符
vim.o.list = false
-- 不可见字符的显示,这里只把空格显示为一个点
vim.o.listchars = "space:·,tab:··"
-- 补全增强
vim.o.wildmenu = true
-- Dont' pass messages to |ins-completin menu|
vim.o.shortmess = vim.o.shortmess .. "c"
-- 补全最多显示10行
vim.o.pumheight = 10
-- 永远显示 tabline
vim.o.showtabline = 2
-- 使用增强状态栏插件后不再需要 vim 的模式提示
vim.o.showmode = false
-- 配置剪切板
vim.opt.clipboard = "unnamedplus"

init.lua引入

-- 基础配置
require("basic")

plugins.lua

安装插件

添加如下代码:

-- 自动安装 Packer.nvim
-- 插件安装目录
-- ~/.local/share/nvim/site/pack/packer/
local fn = vim.fn
local install_path = fn.stdpath("data") .. "/site/pack/packer/start/packer.nvim"
local paccker_bootstrap
if fn.empty(fn.glob(install_path)) > 0 then
    vim.notify("正在安装Pakcer.nvim,请稍后...")
    paccker_bootstrap =
        fn.system(
        {
            "git",
            "clone",
            "--depth",
            "1", -- "https://github.com/wbthomason/packer.nvim",
            "https://gitcode.net/mirrors/wbthomason/packer.nvim",
            install_path
        }
    )

    -- https://github.com/wbthomason/packer.nvim/issues/750
    local rtp_addition = vim.fn.stdpath("data") .. "/site/pack/*/start/*"
    if not string.find(vim.o.runtimepath, rtp_addition) then
        vim.o.runtimepath = rtp_addition .. "," .. vim.o.runtimepath
    end
    vim.notify("Pakcer.nvim 安装完毕")
end

-- Use a protected call so we don't error out on first use
local status_ok, packer = pcall(require, "packer")
if not status_ok then
    vim.notify("没有安装 packer.nvim")
    return
end

packer.startup(
    {
        function(use)


            ///这里是安装插件的位置 TODO:-------->




            if paccker_bootstrap then
                packer.sync()
            end
        end,
        config = {
            -- 最大并发数
            max_jobs = 16,
            -- 自定义源
            git = {
                default_url_format = "https://mirror.ghproxy.com/https://github.com/%s"
            }
            -- display = {
            -- 使用浮动窗口显示
            --   open_fn = function()
            --     return require("packer.util").float({ border = "single" })
            --   end,
            -- },
        }
    }
)

下面所有的插件在plugins.lua文件中安装:

wbthomason/packer.nvim

nvim 包管理器

 use("wbthomason/packer.nvim")

使用:PackerInstall 可以下载插件

init.lua引入

-- Packer插件管理
require("plugins")

ful1e5/onedark.nvim

主题色

    use("ful1e5/onedark.nvim")

colorscheme.lua文件中写入

vim.o.background = "dark"

local colorscheme = "onedark"

local status_ok, _ = pcall(vim.cmd, "colorscheme " .. colorscheme)
if not status_ok then
  vim.notify("colorscheme: " .. colorscheme .. " 没有找到!")
  return
end

init.lua引入

-- 主题设置
require("colorscheme")

到这里我们基本可以简单操作一下vim。
在控制台输入nvim:

$ nvim
//进入vim 编辑界面运行
:PackerInstall

当从github上下载完包只有我们vim 主题色就会发生变化。

其他插件的安装使用都是这个操作。

kyazdani42/nvim-tree.lua

文件目录管理

use(
    {
         "kyazdani42/nvim-tree.lua",
          requires = "kyazdani42/nvim-web-devicons"
      }
)

plugin-config目录,创建nvim-tree.lua文件
添加如下配置

-- https://github.com/kyazdani42/nvim-tree.lua
-- local nvim_tree = require'nvim-tree'
local status, nvim_tree = pcall(require, "nvim-tree")
if not status then
  vim.notify("没有找到 nvim-tree")
  return
end

-- 列表操作快捷键
local list_keys = require("keybindings").nvimTreeList



local g = vim.g

g.nvim_tree_add_trailing = 0 -- append a trailing slash to folder names
g.nvim_tree_git_hl = 0
g.nvim_tree_highlight_opened_files = 0
--
--这里是升级之后的配置
--[[ g.nvim_tree_indent_markers = 1 ]]
g.nvim_tree_root_folder_modifier = table.concat {":t:gs?$?/..", string.rep(" ", 1000), "?:gs?^??"}

g.nvim_tree_show_icons = {
    folders = 1,
    files = 1,
    git = 1
}


nvim_tree.setup({
     -- 隐藏 .文件 和 node_modules 文件夹
    filters = {
        dotfiles = false,
        custom = {".git"}
    },
    disable_netrw = true,
    hijack_netrw = true,
    ignore_ft_on_setup = {"dashboard"},
    -- auto_close = false,
    open_on_tab = false,
    hijack_cursor = true,
    hijack_unnamed_buffer_when_opening = false,
    update_cwd = true,
    update_focused_file = {
        enable = true,
        update_cwd = false
    },
    renderer = {
        indent_markers = {
            enable = true
        }
    },
    view = {
        -- 宽度
        width = 36,
        -- 也可以 'right'
        side = "left",
        -- 隐藏根目录
        hide_root_folder = true,
        -- 自定义列表中快捷键
        mappings = {
            custom_only = true
            --[[ list = list_keys, ]]
        },
        -- 不显示行数
        number = false,
        relativenumber = false,
        -- 显示图标
        signcolumn = "yes"
    },
    git = {
        enable = false,
        ignore = false
    },
    actions = {
        open_file = {
            resize_window = true
        }
    }

})

init.lua引入

-- 插件配置
require("plugin-config.nvim-tree")

goolord/alpha-nvim

欢迎页
同理 在plugin-config目录 创建dashboard.lua文件

vim.g.dashboard_default_executive = "telescope"
vim.g.dashboard_custom_section = {
}
vim.g.dashboard_custom_header = {
}

init.lua引入

require("plugin-config.dashboard")

由于有些字体文件这里无法显示,所有有兴趣的可以查看github 源代码

nvim-web-devicons

nvim-icons

akinsho/bufferline.nvim

tab 文件多文件管理

nvim-lualine/lualine.nvim

状态栏管理

nvim-telescope/telescope.nvim

文件查询,文字查询,文件夹查询等等

LinArcX/telescope-env.nvim

查看系统环境变量

nvim-telescope/telescope-ui-select.nvim

telescope选择ui

folke/which-key.nvim

查看快捷键

nvim-treesitter/nvim-treesitter

主题代码颜色增强

ahmedkhalf/project.nvim

项目管理

:Telescope projects

p00f/nvim-ts-rainbow

彩虹括号

lukas-reineke/indent-blankline.nvim

代码缩进显示标识

akinsho/toggleterm.nvim

切换多个终端

ur4ltz/surround.nvim

vim surround

numToStr/Comment.nvim

注释插件

JoosepAlviste/nvim-ts-context-commentstring

tsx 注释增强

windwp/nvim-autopairs

自动生成关闭标签

windwp/nvim-ts-autotag

tsx 自动生成关闭标签

lewis6991/gitsigns.nvim

git 管理

puremourning/vimspector

调试

SmiteshP/nvim-gps

在bufferline中显示当前的代码名称

rmagatti/goto-preview

弹框定位到具体内容

folke/trouble.nvim

错误美化

Trouble
TroubleToggle
TroubleToggle workspace_diagnostics
TroubleToggle document_diagnostics
TroubleToggle quickfix
TroubleToggle loclist
TroubleToggle lsp_references

sbdchd/neoformat

格式化

folke/todo-comments.nvim

TODO list

mg979/vim-visual-multi

多光标操作

michaelb/sniprun

运行代码片段

windwp/nvim-spectre

替换文本

kristijanhusak/vim-carbon-now-sh

截图

RRethy/vim-illuminate

高亮相同单词

unblevable/quick-scope

高亮目标行首字符

liuchengxu/vista.vim

大纲预览

m-demare/hlargs.nvim

函数参数高亮

phaazon/hop.nvim

快速定位单词和某一行

romgrk/nvim-treesitter-context

当前方法吸顶

williamboman/nvim-lsp-installer

lsp 语言下载

neovim/nvim-lspconfig

lsp 语法支持提示

hrsh7th/nvim-cmp

补全引擎

  • hrsh7th/cmp-vsnip
  • hrsh7th/cmp-nvim-lsp
  • hrsh7th/cmp-buffer
  • hrsh7th/cmp-path
  • hrsh7th/cmp-cmdline
  • hrsh7th/cmp-nvim-lsp-signature-help

都是同一个开发的对lsp的增强,都安装一下

f3fora/cmp-spell

拼写建议

rafamadriz/friendly-snippets

常用的代码片段

onsails/lspkind.nvim

lsp ui美化

tami5/lspsaga.nvim

lsp ui美化

jose-elias-alvarez/nvim-lsp-ts-utils

ts 增强

folke/lua-dev.nvim

lua 增强

b0o/schemastore.nvim

json增强

b0o/schemastore.nvim

json增强

fatih/vim-go

go 语法增强

j-hui/fidget.nvim

lsp 加载进度ui

其实在nvim插件配置中,都是一样的,只要我们掌握的一种,其他自己需要的插件就可以自己安装了。

这里有个重点说一下。

shell

在我自己的配置中,默认打开go 项目会自动生成.vimspector.json文件。
创建文件vimspector配置文件

$ mkdir -p lua/spector/vimspector/templates
$ touch go.vimspector.json

go.vimspector.json文件写入:适合go 的.vimspector.json设置

{
  "configurations": {
    "launch": {
      "adapter": "vscode-go",
      "configuration": {
        "request": "launch",
        "program": "${file}"
      }
    }
  }
}

创建文件lua/shell/vimspector.sh:

#!/bin/sh

# workdir=$(cd $(dirname $0); pwd)

# targetFile= "$workdir/go.mod"

targetFileType=""



if [ -f "./.vimspector.json" ]; then
  exit 1
fi

if [ -f "./go.mod" ]; then
  targetFileType="go"
elif [ -f "./package.json" ]; then
  targetFileType="js"
fi

if [ $targetFileType ]; then
## 这里地址改成自己地址
  cp /Users/longzhang/.config/nvim/lua/spector/vimspector/templates/${targetFileType}.vimspector.json .vimspector.json
fi

lua/shell.lua 文件写入:

cmd =("sh /Users/longzhang/.config/nvim/lua/shell/vimspector.sh ")


os.execute(cmd)

init.sh 引入:

-- 执行shell 脚本
require("shell")

当做完这些之后,当我们每次通过vim 进入项目,就是执行shell 脚本,来检查是否符合判断,如果符合对应的判断条件,就会在项目根目录生成.vimspector.json文件

github

vim
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!