其他分享
首页 > 其他分享> > NVIM 配置打造

NVIM 配置打造

作者:互联网

NVIM 配置

NVIM 安装

# 安装 NVIM
brew install nvim
brew install ranger
# 需要有 python3 支持
brew install python3
brew install node

# 对 nvim 添加 python 支持
pip3 install neovim pynvim

# 输入 nvim 检测
:checkhealth

image-20220106235325902

NVIM 配置

init.vim

mkdir -p ~/.config/nvim
nvim ~/.config/nvim/init.vim
" 配置文件导入 "{{{
" ---------------------------------------------------------------------
" 基础使用习惯配置
runtime ./vimrc.vim
" 插件管理
"./lua/plugins.lua
lua require('plugins')
" 按键映射
runtime ./maps.vim

vimrc.vim

nvim ~/.config/nvim/vimrc.vim 
"----vim 个人使用习惯配置start------
set encoding=UTF-8
" leader设置成空格
let mapleader=" "
" 使用鼠标
set mouse=c
" 显示行号
set nu
" 相对行号
set relativenumber
" tab=4个空格
set tabstop=4
set shiftwidth=4
" 改变 vim中光标的形状
let g:db_ui_use_nerd_fonts=1
let &t_SI.="\e[5 q" "SI = INSERT mode
let &t_SR.="\e[4 q" "SR = REPLACE mode
let &t_EI.="\e[1 q" "EI = NORMAL mode (ELSE)
" 高度光标所在行
"set cursorline
" 设置不换行
"set nowrap
set wrap
" 显示按下的按键
set showcmd
" 按tab显示菜单
set wildmenu
" 不需要备份文件
set nobackup
"----vim 个人使用习惯配置end------
"ssh 远程粘贴板
if executable('clipboard-provider')
    let g:clipboard = {
          \ 'name': 'myClipboard',
          \     'copy': {
          \         '+': 'clipboard-provider copy',
          \         '*': 'env COPY_PROVIDERS=tmux clipboard-provider copy',
          \     },
          \     'paste': {
          \         '+': 'clipboard-provider paste',
          \         '*': 'env COPY_PROVIDERS=tmux clipboard-provider paste',
          \     },
          \ }
endif
"随机选一个颜色风格
function RandomColorScheme()
  let mycolors = split(globpath(&rtp,"**/colors/*.vim"),"\n") 
  exe 'so ' . mycolors[localtime() % len(mycolors)]
  unlet mycolors
endfunction

"call RandomColorScheme()

:command NewColor call RandomColorScheme()


" 文件类型设置 "{{{
" ---------------------------------------------------------------------
" JSX 语法高亮
" set filetypes as typescriptreact
autocmd BufNewFile,BufRead *.tsx,*.jsx,*.js set filetype=typescriptreact

" 添加头部文件
function HeaderPython()
  call setline(1, "#!/usr/bin/env python")
  call append(1, "# -*- coding: utf-8 -*-")
  call append(2, "# SR @ " . strftime('%Y-%m-%d %T', localtime()))
  normal G
  normal o
  normal o
endf

autocmd bufnewfile *.py call HeaderPython()

maps.vim

nvim ~/.config/nvim/maps.vim 
"-------------------------------------------------------------------------------
" window
"-------------------------------------------------------------------------------
" Split window
nmap ss :split<Return><C-w>w
nmap sv :vsplit<Return><C-w>w
" Move window
"nmap <Space> <C-w>w
"map s<left> <C-w>h
"map s<up> <C-w>k
"map s<down> <C-w>j
"map s<right> <C-w>l
map sh <C-w>h
map sk <C-w>k
map sj <C-w>j
map sl <C-w>l

" Resize window
" 在mac/linux中使用Alt键,在webssh
" 中alt没用,就使用Ctrl,WEBSSH主要的WINDOWS中使用
nmap <M-left> <C-w><
nmap <C-left> <C-w><
nmap s<left> <C-w><

nmap <M-right> <C-w>>
nmap <C-right> <C-w>>
nmap s<right> <C-w>>

nmap <M-up> <C-w>+
nmap <C-up> <C-w>+
nmap s<up> <C-w>+

nmap <M-down> <C-w>-
nmap <C-down> <C-w>-
nmap s<down> <C-w>-

" 插入模式移动光标
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>
inoremap <C-d> <Delete>
" hh在我用的单词里出现的频率极低,其实感觉home用的没有end多,统一风格都用大写的吧
"inoremap HH <Home>
" 单词中包含ll的太多了,所以用大写LL
"inoremap LL <End>
" jk <Esc>
inoremap jk <Esc>
" 插入模式鼠标滚轮抵消,不然会出现滚动鼠标录入了一堆5j5k
inoremap 5k <Esc>
inoremap 5j <Esc>
inoremap 9<CR> <Esc>a
" 快速跳转行首与行尾  
nnoremap L $
nnoremap H ^
" 向下5行
noremap <C-j> 5j
" 向上5行
noremap <C-k> 5k
" 保 存
noremap <C-s> :w<CR>
noremap s :w<CR>
" Coc智能处理,使用IDEA Alt+Enter 同样按键
"noremap <M-Enter> :CocAction<CR>
inoremap <C-s> <ESC> :w<CR>
" 代码格式化
noremap <leader>f :Format<CR> 
noremap <leader>r :luafile ~/.wp/lua/run.lua<CR>

" 强制退出
map Q :q<CR>
"map qq :q<CR>
" 重新加载设置
map R :source $MYVIMRC<CR>
"自动关闭标签
inoremap <buffer> <C-v> <esc>yiwi<lt><esc>ea></><esc>hpF>i
set iskeyword+=<,>
iab <h1> <lt>h1> <lt>/h1><esc>5ha

" 全选
nmap <C-a> gg<S-v>G
" 加/减数字1
nnoremap + <C-a>
nnoremap - <C-x>
"-------------------------------------------------------------------------------
" Buffers
"-------------------------------------------------------------------------------
" Open current directory
" 插入模式移动光标
inoremap <C-h> <Left>
inoremap <C-j> <Down>
inoremap <C-k> <Up>
inoremap <C-l> <Right>
inoremap <C-d> <Delete>
" hh在我用的单词里出现的频率极低,其实感觉home用的没有end多,统一风格都用大写的吧
"inoremap HH <Home>
" 单词中包含ll的太多了,所以用大写LL
"inoremap LL <End>
" jk <Esc>
inoremap jk <Esc>
" 插入模式鼠标滚轮抵消,不然会出现滚动鼠标录入了一堆5j5k
inoremap 5k <Esc>
inoremap 5j <Esc>
inoremap 9<CR> <Esc>a
" 快速跳转行首与行尾  
nnoremap L $
nnoremap H ^
" 向下5行
noremap <C-j> 5j
" 向上5行
noremap <C-k> 5k
" 保存
noremap <C-s> :w<CR>
" Coc智能处理,使用IDEA Alt+Enter 同样按键
noremap <M-Enter> :CocAction<CR>
inoremap <C-s> <ESC> :w<CR>
" 代码格式化
"noremap <leader>f :Format<CR> 

" 强制退出
map Q :q<CR>
" 重新加载设置
map R :source $MYVIMRC<CR>
"自动关闭标签
inoremap <buffer> <C-v> <esc>yiwi<lt><esc>ea></><esc>hpF>i
set iskeyword+=<,>
iab <h1> <lt>h1> <lt>/h1><esc>5ha

" 全选
nmap <C-a> gg<S-v>G
" 加/减数字1
nnoremap + <C-a>
nnoremap - <C-x>
"-------------------------------------------------------------------------------
" Buffers
"-------------------------------------------------------------------------------
" Open current directory
"nmap te :tabedit
"nmap <S-Tab> :tabprev<Return>
nmap <S-Tab> :bprev<Return>
"nmap <Tab> :tabnext<Return>
nmap <Tab> :bnext<Return>
" 窗口管理器
" invoke with '-'
nmap  -  <Plug>(choosewin)
"nmap  sw  <Plug>(choosewin)
"nmap  <leader>w  <Plug>(choosewin)

PS:一些插件的快捷键在末尾 当所有插件都配置完毕的时候将快捷键加入该文件中

packer 插件管理器

mkdir -p ~/.config/nvim/lua
nvim ~/.config/nvim/plugins.lua 
---@diagnostic disable: undefined-global
--在没有安装packer的电脑上,自动安装packer插件
local fn = vim.fn
local install_path = fn.stdpath('data')..'/site/pack/packer/start/packer.nvim'
if fn.empty(fn.glob(install_path)) > 0 then
	 --fn.system({'git', 'clone', '--depth', '1', 'https://github.com/wbthomason/packer.nvim', install_path})	--默认地址
	fn.system({'git', 'clone', '--depth', '1', 'https://codechina.csdn.net/mirrors/wbthomason/packer.nvim.git', install_path})	--csdn加速镜像
	vim.cmd 'packadd packer.nvim'
end
-- Only required if you have packer configured as `opt`
vim.cmd [[packadd packer.nvim]]
return require('packer').startup({
	function()
		use 'wbthomason/packer.nvim'-- Packer can manage itself
	end,
	config = {
		max_jobs = 16,
		git = {
			   default_url_format = "https://github.com.cnpmjs.org/%s"
		},
		display = {
			open_fn = function()
				return require('packer.util').float({ border = 'single' })
			end
		}
	}
})

# 插件安装方式
PackerInstall

插件配置

字体配置

# 终端安装该字体并且选择该字体防止乱码
brew tap homebrew/cask-fonts
brew install font-hack-nerd-font --cask

lualine标签栏美化

安装

nvim ~/.config/nvim/lua/plugins.lua
 --状态栏插件
 use {
 "nvim-lualine/lualine.nvim",
 requires = {"kyazdani42/nvim-web-devicons", opt = true}
 }

配置

nvim ~/.config/nvim/after/plugin/lualine.lua 
local status, lualine = pcall(require, "lualine")
if (not status) then
  return
end

lualine.setup {
  options = {
    icons_enabled = true,
    theme = "auto",
    component_separators = {left = "", right = ""},
    section_separators = {left = "", right = ""},
    disabled_filetypes = {},
    always_divide_middle = true
  },
  sections = {
    lualine_a = {"mode"},
    lualine_b = {
      "branch",
      "diff"
      --{"diagnostics", sources = {"nvim_lsp"}}
    },
    lualine_c = {"filename"},
    lualine_x = {
      {"diagnostics", sources = {"nvim_lsp"}, symbols = {error = " ", warn = " ", info = " ", hint = " "}},
      "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 = {}
}

image-20220107013051893

bufferline状态栏

安装

-- bufferline 显示标签页,与lualine配合使用
use "akinsho/bufferline.nvim"

配置

nvim ~/.config/nvim/after/plugin/bufferline.lua
local status, bufferline = pcall(require, "bufferline")
if (not status) then
  return
end

vim.opt.termguicolors = true
bufferline.setup {
  options = {
    --numbers = "ordinal" | "buffer_id" | "both" | function({ ordinal, id, lower, raise }): string,
    --numbers = "both",
    --- @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
    --number_style = "none",
    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 = "",
    buffer_close_icon = "",
    modified_icon = "●",
    close_icon = "",
    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 = 15,
    max_prefix_length = 12, -- prefix used when a buffer is de-duplicated
    tab_size = 15,
    --diagnostics = false | "nvim_lsp" | "coc",
    diagnostics = "nvim_lsp",
    diagnostics_update_in_insert = false,
    --[[diagnostics_indicator = function(count, level, diagnostics_dict, context)
      return "(" .. count .. ")"
    end,]]
    -- rest of config ...

    --- count is an integer representing total count of errors
    --- level is a string "error" | "warning"
    --- diagnostics_dict is a dictionary from error level ("error", "warning" or "info")to number of errors for each level.
    --- this should return a string
    --- Don't get too fancy as this function will be executed a lot
    diagnostics_indicator = function(count, level, diagnostics_dict, context)
      local icon = level:match("error") and " " or " "
      return " " .. icon .. count
    end,
    -- NOTE: this will be called a lot so don't do any heavy processing here
    custom_filter = function(buf_number)
      --如果是defx则隐藏
      local finded, _ = string.find(vim.bo[buf_number].filetype, "defx")
      if finded ~= nil then
        return false
      end
      return true
    end,
    --offsets = {{filetype = "NvimTree", text = "File Explorer" | function , text_align = "left" | "center" | "right"}},
    --show_buffer_icons = true | false, -- disable filetype icons for buffers
    show_buffer_icons = true, -- disable filetype icons for buffers
    --show_buffer_close_icons = true | false,
    show_buffer_close_icons = false,
    --show_close_icon = true | false,
    show_close_icon = false,
    --show_tab_indicators = true | false,
    show_tab_indicators = true,
    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 = "slant" | "thick" | "thin" | {"any", "any"},
    separator_style = "thin",
    --enforce_regular_tabs = false | true,
    enforce_regular_tabs = false,
    --always_show_bufferline = true | false,
    always_show_bufferline = true
    --[[sort_by = "id" | "extension" | "relative_directory" | "directory" | "tabs" | function(buffer_a, buffer_b)
        -- add custom logic
        return buffer_a.modified > buffer_b.modified
      end]]
  }
}

--按键映射
--nnoremap <silent> gb :BufferLinePick<CR>
vim.api.nvim_set_keymap("n", "gb", "<Cmd>BufferLinePick<CR>", {noremap = true, silent = true})

vim.api.nvim_set_keymap("n", "<leader>1", "<Cmd>BufferLineGoToBuffer 1<CR>", {noremap = true, silent = true})
vim.api.nvim_set_keymap("n", "<leader>2", "<Cmd>BufferLineGoToBuffer 2<CR>", {noremap = true, silent = true})
vim.api.nvim_set_keymap("n", "<leader>3", "<Cmd>BufferLineGoToBuffer 3<CR>", {noremap = true, silent = true})
vim.api.nvim_set_keymap("n", "<leader>4", "<Cmd>BufferLineGoToBuffer 4<CR>", {noremap = true, silent = true})
vim.api.nvim_set_keymap("n", "<leader>5", "<Cmd>BufferLineGoToBuffer 5<CR>", {noremap = true, silent = true})
vim.api.nvim_set_keymap("n", "<leader>6", "<Cmd>BufferLineGoToBuffer 6<CR>", {noremap = true, silent = true})
vim.api.nvim_set_keymap("n", "<leader>7", "<Cmd>BufferLineGoToBuffer 7<CR>", {noremap = true, silent = true})
vim.api.nvim_set_keymap("n", "<leader>8", "<Cmd>BufferLineGoToBuffer 8<CR>", {noremap = true, silent = true})
vim.api.nvim_set_keymap("n", "<leader>9", "<Cmd>BufferLineGoToBuffer 9<CR>", {noremap = true, silent = true})

image-20220107013221086

dashboard开屏插件

安装

 use {"glepnir/dashboard-nvim"}

配置

nvim ~/.config/nvim/after/plugin/dashboard.lua      
local vim = vim
vim.g.dashboard_custom_header = {
        "",
        "   ⠀⠀⠀⠀⠀⠀⠀⠀⠀⡴⠞⠉⢉⣭⣿⣿⠿⣳⣤⠴⠖⠛⣛⣿⣿⡷⠖⣶⣤⡀⠀⠀⠀   ",
        "    ⠀⠀⠀⠀⠀⠀⠀⣼⠁⢀⣶⢻⡟⠿⠋⣴⠿⢻⣧⡴⠟⠋⠿⠛⠠⠾⢛⣵⣿⠀⠀⠀⠀  ",
        "    ⣼⣿⡿⢶⣄⠀⢀⡇⢀⡿⠁⠈⠀⠀⣀⣉⣀⠘⣿⠀⠀⣀⣀⠀⠀⠀⠛⡹⠋⠀⠀⠀⠀  ",
        "    ⣭⣤⡈⢑⣼⣻⣿⣧⡌⠁⠀⢀⣴⠟⠋⠉⠉⠛⣿⣴⠟⠋⠙⠻⣦⡰⣞⠁⢀⣤⣦⣤⠀  ",
        "    ⠀⠀⣰⢫⣾⠋⣽⠟⠑⠛⢠⡟⠁⠀⠀⠀⠀⠀⠈⢻⡄⠀⠀⠀⠘⣷⡈⠻⣍⠤⢤⣌⣀  ",
        "    ⢀⡞⣡⡌⠁⠀⠀⠀⠀⢀⣿⠁⠀⠀⠀⠀⠀⠀⠀⠀⢿⡀⠀⠀⠀⠸⣇⠀⢾⣷⢤⣬⣉  ",
        "    ⡞⣼⣿⣤⣄⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⣿⠀⠸⣿⣇⠈⠻  ",
        "    ⢰⣿⡿⢹⠃⠀⣠⠤⠶⣼⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⣿⠀⠀⣿⠛⡄⠀  ",
        "    ⠈⠉⠁⠀⠀⠀⡟⡀⠀⠈⡗⠲⠶⠦⢤⣤⣤⣄⣀⣀⣸⣧⣤⣤⠤⠤⣿⣀⡀⠉⣼⡇⠀  ",
        "    ⣿⣴⣴⡆⠀⠀⠻⣄⠀⠀⠡⠀⠀⠀⠈⠛⠋⠀⠀⠀⡈⠀⠻⠟⠀⢀⠋⠉⠙⢷⡿⡇⠀  ",
        "    ⣻⡿⠏⠁⠀⠀⢠⡟⠀⠀⠀⠣⡀⠀⠀⠀⠀⠀⢀⣄⠀⠀⠀⠀⢀⠈⠀⢀⣀⡾⣴⠃⠀  ",
        "    ⢿⠛⠀⠀⠀⠀⢸⠁⠀⠀⠀⠀⠈⠢⠄⣀⠠⠼⣁⠀⡱⠤⠤⠐⠁⠀⠀⣸⠋⢻⡟⠀⠀  ",
        "    ⠈⢧⣀⣤⣶⡄⠘⣆⠀⠀⠀⠀⠀⠀⠀⢀⣤⠖⠛⠻⣄⠀⠀⠀⢀⣠⡾⠋⢀⡞⠀⠀⠀  ",
        "    ⠀⠀⠻⣿⣿⡇⠀⠈⠓⢦⣤⣤⣤⡤⠞⠉⠀⠀⠀⠀⠈⠛⠒⠚⢩⡅⣠⡴⠋⠀⠀⠀⠀  ",
        "    ⠀⠀⠀⠈⠻⢧⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⣻⠿⠋⠀⠀⠀⠀⠀⠀  ",
        "    ⠀⠀⠀⠀⠀⠀⠉⠓⠶⣤⣄⣀⡀⠀⠀⠀⠀⠀⢀⣀⣠⡴⠖⠋⠁⠀⠀⠀⠀⠀⠀⠀⠀  ",
        "                                       ",
        "",
}
vim.g.dashboard_custom_section = {
        a = {
                description = { "     新 文 件  SPC fo" },
                command = "DashboardNewFile",
        },
        b = {
                description = { "     查找文件  SPC ff" },
                -- command = "Telescope fd",
                command = "Telescope fd find_command=fd,--hidden",
        },
        c = {
                description = { "     已用文件  SPC fh" },
                command = "Telescope oldfiles",
        },
        d = {
                description = { "     跳转标记  SPC fm" },
                command = "Telescope marks",
        },
        e = {
                description = { "     查看内容  SPC fw" },
                command = "Telescope live_grep",
        },
        f = {
                description = { "     查看主题  SPC tc" },
                command = "Telescope colorscheme",
        },
        g = {
                description = { "    ∂ 查看命令  SPC fc" },
                command = "Telescope commands",
        },
        h = {
                description = { "     查看帮助  SPC fa" },
                command = "Telescope man_pages",
        },
}
vim.g.dashboard_disable_at_vimenter = 0
vim.g.dashboard_footer_icon = "      

标签:end,--,配置,nnoremap,vim,NVIM,打造,true,nvim
来源: https://www.cnblogs.com/SR-Program/p/15773546.html