Xshell+Neovim安装教程

Xshell+Neovim安装教程,本文使用的是Ubuntu 22.04.3 LTS系统

装neovim

首先,确保你的系统已经安装了 apt 工具。如果你的系统是基于 Debian 的发行版(如 Ubuntu),可以跳过此步骤。否则,执行以下命令来安装 apt:

1
sudo apt-get install apt

添加 Neovim 的官方软件源。执行以下命令来添加源:

1
sudo add-apt-repository ppa:neovim-ppa/unstable

更新 apt 软件包列表:

1
sudo apt-get update

安装 Neovim。执行以下命令来安装最新版本的 Neovim:

1
sudo apt-get install neovim

完成上述步骤后,你应该成功安装了 Neovim。

可以通过运行 nvim --version 命令来验证安装结果。

或者去 https://github.com/neovim/neovim/releases 下载压缩包

本文用的 nvim 0.9.1

nvim配置文件

lua语法

1
2
3
4
5
6
7
8
9
10
11
12
# 进入家目录
cd
mkdir -p .config/nvim
cd .config/nvim
# 新建nvim入口配置文件
touch init.lua

# 存放个人配置
cd
mkdir -p .config/nvim/lua/user
cd .config/nvim/lua/user
touch options.lua

个人neovim配置

options.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
local opt = vim.opt

-- 行号
opt.number = true

-- 缩进
opt.tabstop = 4
opt.shiftwidth = 4
opt.expandtab = true
opt.autoindent = true

-- 显示光标行
opt.cursorline = true

-- 搜索
opt.ignorecase = true
opt.smartcase = true

-- 禁用鼠标
opt.mouse = ""

-- 外观
opt.termguicolors = true
opt.signcolumn = "yes"

在入口lua文件中引用一下,个人配置,否则不生效

init.lua

1
require("user.options")

vim脚本语法

1
2
3
4
5
6
7
8
9
10
11
12
# 进入家目录
cd
mkdir -p .config/nvim
cd .config/nvim
# 新建nvim入口配置文件
touch init.vim

# 存放个人配置
cd
mkdir -p .config/nvim/lua/user
cd .config/nvim/lua/user
touch options.vim

个人neovim配置

options.vim

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
syntax on                      " 语法高亮
filetype on
filetype plugin on
filetype plugin indent on " 开启自动识别文件类型,并根据文件类型加载不同的插件和缩进规则
set number " 显示行号
set relativenumber " 显示相对行号
set autoindent
set smartindent
set softtabstop=4
set shiftwidth=4 " 设置位移宽度为4
set tabstop=4 " 设置缩进宽度为4
set expandtab " 将缩进替换为空格
set nobackup " 不生成backup文件
set scrolloff=10 " 设置滚动时始终显示上下10行
set nowrap " 禁止折行
set incsearch " 增量式搜索
set ignorecase " 搜索时大小写不敏感
set smartcase " 搜索时对首字母大小写敏感
set showcmd " 显示键入的命令前缀
set showmode " 显示当前模式(插入、可视等)
set hlsearch " 高亮搜索结果
set history=1000 " 设置命令历史记录为1000
set wildmenu " 设置tab补全
set wildmode=list:longest " 使tab补全类似于Bash
set encoding=utf-8 " 设置编码方式为UTF-8
set showmatch " 当光标置于成对符号(例如括号)时,高亮匹配的符号对
exec "nohlsearch"
set mouse=a " 支持鼠标
set foldmethod=indent " 代码折叠
set foldcolumn=0 " 设置代码区域的宽度
setlocal foldlevel=1 " 设置折叠层数为
set foldlevelstart=99 " 打开文件是默认不折叠代码
set cursorline " 光标所在的当前行高亮
set cursorcolumn " 高亮当前列



let g:mapleader = " " " leader 改为空格

cmap w!! w !sudo tee >/dev/null % " w!!写入只读文件

map tq :tabclose " 标签页操作 关闭标签页 tabe close
map tmh :-tabmove " 移动标签页 tabe move h/j
map tml :+tabmove

noremap ta :tabe<CR> " ta 为新建标签页,th 和 tl 分别向左右跳转
noremap th :-tabnext<CR>
noremap tl :+tabnext<CR>

let &t_SI = "\]50;CursorShape=1\x7" " 根据不同模式,改变光标样式
let &t_SR = "\]50;CursorShape=2\x7"
let &t_EI = "\]50;CursorShape=0\x7"

noremap j gj
noremap k gk
noremap gj j
noremap gk k
noremap J 5gj
noremap K 5gk

noremap <leader><CR> :nohlsearch<CR> " 取消高亮映射 空格(<leader>)和回车(<CR>)

noremap s <nop> " 分屏(split)
noremap sl :set splitright<CR>:vsplit<CR>
noremap sh :set nosplitright<CR>:vsplit<CR>
noremap sj :set splitbelow<CR>:split<CR>
noremap sk :set nosplitbelow<CR>:split<CR>

noremap <leader>l <c-w>l " 使用空格加方向键实现分屏之间的跳转
noremap <leader>h <c-w>h
noremap <leader>j <c-w>j
noremap <leader>k <c-w>k

noremap <leader>L <c-w>L " 使用空格加大写方向键将当前分屏放置到指定方向的最边缘
noremap <leader>H <c-w>H
noremap <leader>J <c-w>J
noremap <leader>K <c-w>K


noremap <leader>o o<Esc>k " 空格加小写字母 o:向下插入空行
noremap <leader>O O<Esc>j " 空格加大写字母 O:向上插入空行
noremap <leader>w :w<CR> " 空格加小写字母 w:保存文档
noremap <leader>q :q<CR> " 空格加小写字母 q:退出 Neovim
noremap Y "+y " 大写字母 Y:复制到系统剪切板

noremap = nzz " 查找 zz 可以将当前行居中,继续查找设置为更常用(输入法常用)的 = 和 - 键:
noremap - Nzz

nnoremap R :source $MYVIMRC<CR> " 普通模式下大写字母 R:刷新 Neovim 配置
"0 和 $ 默认情况下分别表示实际行的行首和行尾
nnoremap g0 0
nnoremap 0 g0
nnoremap $ g$
nnoremap g$ $




silent !mkdir -p ~/.config/nvim/tmp/backup " 编辑 保留记录,退出后再次打开依然可以 undo
silent !mkdir -p ~/.config/nvim/tmp/undo
set backupdir=~/.config/nvim/tmp/backup,.
set directory=~/.config/nvim/tmp/backup,.
if has('persistent_undo')
set undofile
set undodir=~/.config/nvim/tmp/undo,.
endif

noremap <leader>e :Explore<CR> " 当前窗口下打开
noremap <leader>ve :Vexplore<CR> " 竖直分割窗口打开
noremap <leader>se :Sexplore<CR> " 水平分割窗口打开

au BufReadPost * if line("'\"") > 1 && line("'\"") <= line("$") | exe "normal! g'\"" | endif " 恢复光标

nvim插件

1
2
3
cd
mkdir -p .config/nvim/lua/plugins
touch plugins.lua

安装 packer.nvim

packer.nvim 是一个用于管理 Neovim 插件的包管理器。它可以帮助用户轻松地安装、更新和删除他们的插件。

packer.nvim 也支持异步操作,可以提高插件管理的效率,并且使用 Lua 编写,与 Neovim 的配置语言相容。

通过 packer.nvim,用户可以更加方便地组织和管理他们的 Neovim 插件,提高编辑器的定制化程度。

输入以下内容:(Github地址

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
local ensure_packer = function()
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})
vim.cmd [[packadd packer.nvim]]
return true
end
return false
end

local packer_bootstrap = ensure_packer()

-- 每次保存 plugins.lua 自动安装插件
vim.cmd([[
augroup packer_user_config
autocmd!
autocmd BufWritePost plugins.lua source <afile> | PackerSync
augroup end
]])

return require('packer').startup(function(use)
use 'wbthomason/packer.nvim'
if packer_bootstrap then
require('packer').sync()
end
end)

init.lua 中添加引用

1
require("plugins.plugins")

每次修改了 plugins.lua 之后

我习惯 vim 命令行模式下输入

source % 然后使用 PackerInstall 或者 PackerSync

  • source % 是为了让当前配置生效
  • PackerInstall 是安装未安装的包或者插件
  • PackerSync 会去比对安装目录,执行安装,删除或者更新 vim 插件

安装主题

plugins/plugins.lua

1
2
3
4
5
6
7
8
9
10
return require('packer').startup(function(use)
use 'wbthomason/packer.nvim'

-- 这里添加你的主题
use 'folke/tokyonight.nvim'

if packer_bootstrap then
require('packer').sync()
end
end)

user/options.lua

1
vim.cmd[[colorscheme tokyonight-night]]

使用Xshell的话,需要将 工具-选项-高级-使用本色 勾上

然后重启Xshell。

安装状态栏

https://github.com/nvim-lualine/lualine.nvim

plugins/plugins.lua

1
2
3
4
use {
'nvim-lualine/lualine.nvim',
requires = { 'nvim-tree/nvim-web-devicons', opt = true }
}

:w 保存

新建 plugins/lualine.lua

1
2
3
4
5
require('lualine').setup(
{
options = { theme = 'tokyonight' },
}
)

修改入口文件init.lua 添加

1
require("plugins.lualine")

安装文件树

https://github.com/nvim-tree/nvim-tree.lua

plugins/plugins.lua

1
2
3
4
use {
'nvim-tree/nvim-tree.lua',
requires = { 'nvim-tree/nvim-web-devicons'} -- 图标
}

:w 保存

新建 plugins/nvim-tree.lua

1
2
3
4
5
6
-- disable netrw at the very start of your init.lua
vim.g.loaded_netrw = 1
vim.g.loaded_netrwPlugin = 1

-- empty setup using defaults
require("nvim-tree").setup()

改键,用来呼出文件树

新建 user/keymaps.lua

1
2
3
4
local keymap = vim.keymap

-- 文档树改键,F9呼出
keymap.set("n", "<F9>", ":NvimTreeToggle<CR>")

修改入口文件init.lua 添加

1
2
require("plugins.nvim-tree")
require("user.keymaps")

使用F9即可呼出文档树,按Tab键即可进入到对应文件。

ctrl + w 然后点击左右方向键即可在文件和文档树之间切换,这样比较麻烦,所以下个插件就是解决这个的。

tmux窗格和vim拆分之间的无缝导航

https://github.com/christoomey/vim-tmux-navigator

plugins/plugins.lua

1
use 'christoomey/vim-tmux-navigator'

:w 保存

安装完成以后,使用 ctrl + h 就会跳到左边,ctrl + l 就会跳到右边

语法高亮插件

https://github.com/nvim-treesitter/nvim-treesitter

plugins/plugins.lua

1
2
3
4
use {
'nvim-treesitter/nvim-treesitter',
run = ':TSUpdate'
}

:w 保存

新建 plugins/treesitter.lua

官方示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
require'nvim-treesitter.configs'.setup {
-- A list of parser names, or "all" (the five listed parsers should always be installed)
ensure_installed = { "c", "lua", "vim", "vimdoc", "query" },

-- Install parsers synchronously (only applied to `ensure_installed`)
sync_install = false,

-- Automatically install missing parsers when entering buffer
-- Recommendation: set to false if you don't have `tree-sitter` CLI installed locally
auto_install = true,

-- List of parsers to ignore installing (or "all")
ignore_install = { "javascript" },

---- If you need to change the installation directory of the parsers (see -> Advanced Setup)
-- parser_install_dir = "/some/path/to/store/parsers", -- Remember to run vim.opt.runtimepath:append("/some/path/to/store/parsers")!

highlight = {
enable = true,

-- NOTE: these are the names of the parsers and not the filetype. (for example if you want to
-- disable highlighting for the `tex` filetype, you need to include `latex` in this list as this is
-- the name of the parser)
-- list of language that will be disabled
disable = { "c", "rust" },
-- Or use a function for more flexibility, e.g. to disable slow treesitter highlight for large files
disable = function(lang, buf)
local max_filesize = 100 * 1024 -- 100 KB
local ok, stats = pcall(vim.loop.fs_stat, vim.api.nvim_buf_get_name(buf))
if ok and stats and stats.size > max_filesize then
return true
end
end,

-- Setting this to true will run `:h syntax` and tree-sitter at the same time.
-- Set this to `true` if you depend on 'syntax' being enabled (like for indentation).
-- Using this option may slow down your editor, and you may see some duplicate highlights.
-- Instead of true it can also be a list of languages
additional_vim_regex_highlighting = false,
},
}

水友使用示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
local status, treesitter = pcall(require, "nvim-treesitter.configs")
if not status then
vim.notify("没有找到 nvim-treesitter")
return
end

treesitter.setup({
-- 安装 language parser
-- :TSInstallInfo 命令查看支持的语言
ensure_installed = { "json", "html", "css", "vim", "lua", "javascript" ,"c" ,"cpp" ,"python","java","bash",},
-- 启用代码高亮模块
highlight = {
enable = true,
additional_vim_regex_highlighting = false,
},
-- 启用增量选择模块
incremental_selection = {
enable = true,
keymaps = {
init_selection = "<CR>",
node_incremental = "<CR>",
node_decremental = "<BS>",
scope_incremental = "<TAB>",
},
},
-- 启用代码缩进模块 (=)
indent = {
enable = true,
}
})
-- 开启 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

修改入口文件init.lua 添加

1
require("plugins.treesitter")

括号不同颜色

https://github.com/p00f/nvim-ts-rainbow

plugins/plugins.lua

1
use 'nvim-ts-rainbow',

:w 保存

新建 plugins/rainbow.lua

1
2
3
4
5
6
7
8
9
require("nvim-treesitter.configs").setup {
highlight = {
},
rainbow = {
enable = true,
extended_mode = true,
max_file_lines = nil,
}
}

修改入口文件init.lua 添加

1
require("plugins.rainbow")

自动补全括号或者引号等

https://github.com/windwp/nvim-autopairs

plugins/plugins.lua

1
use 'windwp/nvim-autopairs',

:w 保存

新建 plugins/autopairs.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
local npairs = require("nvim-autopairs")
local Rule = require('nvim-autopairs.rule')

npairs.setup({
check_ts = true,
ts_config = {
lua = {'string'},-- it will not add a pair on that treesitter node
javascript = {'template_string'},
java = false,-- don't check treesitter on java
}
})

local ts_conds = require('nvim-autopairs.ts-conds')


-- press % => %% only while inside a comment or string
npairs.add_rules({
Rule("%", "%", "lua")
:with_pair(ts_conds.is_ts_node({'string','comment'})),
Rule("$", "$", "lua")
:with_pair(ts_conds.is_not_ts_node({'function'}))
})

修改入口文件init.lua 添加

1
require("plugins.autopairs")

语法提示相关

plugins/plugins.lua

1
2
3
4
5
use {
'williamboman/mason.nvim',
'williamboman/mason-lspconfig.nvim',
'neovim/nvim-lspconfig',
}

:w 保存

新建 plugins/lsp.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
require("mason").setup({
ui = {
icons = {
package_installed = "✓",
package_pending = "➜",
package_uninstalled = "✗"
}
}
})

require("mason-lspconfig").setup {
ensure_installed = { "lua_ls"},
}

require("lspconfig").lua_ls.setup({})

保存退出

再进入这个文件,输入 :Mason 打开面板

输入 / + 你要搜索的语言,定位到那一行,然后按 i,就会去安装了(比如 /java

修改入口文件init.lua 添加

1
require("plugins.lsp")

自动补全

plugins/plugins.lua

1
2
3
4
5
6
use "hrsh7th/cmp-nvim-lsp"
use "hrsh7th/cmp-buffer"
use "hrsh7th/cmp-path"
use "saadparwaiz1/cmp_luasnip"
use "L3MON4D3/LuaSnip"
use "rafamadriz/friendly-snippets"

:w 保存

新建 plugins/cmp.lua (相关内容可以到网上搜寻)

1
2
3
4
5
6
```

修改入口文件`init.lua` 添加

```lua
require("plugins.cmp")

多标签页,文件缓冲

plugins/plugins.lua

1
use "akinsho/bufferline.nvim"

:w 保存

新建 plugins/bufferline.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
vim.opt.termguicolors = true
require("bufferline").setup {
options = {
-- always_show_bufferline = false,
-- 使用nvim内置lsp
diagnostics = "nvim_lsp"
-- 左侧让出 nvim-tree 的位置
offsets = {
{
filetype = "NvimTree",
text = "File Explorer",
highlight = "Directory",
text_align = "left"
}
}
}
}

修改入口文件init.lua 添加

1
require("plugins.bufferline")

改键,使得标签页可以切换

user/keymaps.lua

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
-- 老板键,空格键
vim.g.mapleader = " "

local keymap = vim.keymap

-- 单行或者多行移动
-- 下移
keymap.set("v", "1", ":m '>+1<CR>gv=gv")
-- 上移
keymap.set("v", "2", ":m '<-2<CR>gv=gv")

-- 文档树改键,F9呼出
keymap.set("n", "<F9>", ":NvimTreeToggle<CR>")

-- 在 bufferline 中切换文件
-- 空格 + h,向左切换
keymap.set("n", "<leader>h", ":bnext<CR>")
-- 空格 + l,向右切换
keymap.set("n", "<leader>l", ":bprevious<CR>")

文件搜索

plugins/plugins.lua

1
2
3
4
use {
'nvim-telescope/telescope.nvim', tag = '0.1.1'
requires = {{'nvim-lua/plenary.nvim'}}
}

:w 保存

新建 plugins/telescope.lua

1
2
3
4
5
6
local builtin = require('telescope.builtin')

vim.keymap.set('n', '<leader>ff', builtin.find_files, {})
vim.keymap.set('n', '<leader>fg', builtin.live_grep, {})
vim.keymap.set('n', '<leader>fb', builtin.buffers, {})
vim.keymap.set('n', '<leader>fh', builtin.help_tags, {})

修改入口文件init.lua 添加

1
require("plugins.telescope")

使用 空格 + ff ,即可搜索文件

点击查看

本文标题:Xshell+Neovim安装教程

文章作者:LiJing

发布时间:2024年01月01日 - 15:23:49

最后更新:2024年01月01日 - 22:57:45

原始链接:https://blog-next.xiaojingge.com/posts/2417738973.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。

-------------------本文结束 感谢您的阅读-------------------