aboutsummaryrefslogtreecommitdiffstats
path: root/content/posts/从0开始搭建自己的终端开发环境.md
blob: 14009c919c8f52dedcb5d1d55871f51409e19d22 (plain) (blame)
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
---
abbrlink: 2799171278
categories:
- 往昔
date: "2025-04-15 21:15:24"
tags:
- 终端
- Neovim
title: 从0开始搭建自己的终端开发环境

---

**系统**:WSL Arch

### 主折腾点

**代码编辑器**:Vim(Neovim)

**终端体验优化**:tmux

**文件管理器**:yazi

## 代码编辑器——Neovim

### 前言

Neovim和Vim相比,在性能和易用性上有很大的提升,在异步任务表现突出;使用更加现代的lua脚本配置,可读性更高。

#### 为什么不用Lazyvim?

答:我个人用了很长一段时间Lazyvim,虽然开箱即用,简单调试就可以使用,但是我并不能清除的理解他到底给我装了什么,我是否需要那些插件。感觉像在用别人的软件,而自己从头开始配置vim的掌控感是使用他人的配置文件无法比拟的,在配置的过程中也可以培养排错和程序调试能力,唯一的缺点就是略微耗费时间。

### 安装

先滚一把系统保证软件包都是最新的

`sudo pacman -Syyu`

安装Neovim

`sudo pacman -S neovim`

这时候在终端输入`nvim`就可以打开neovim了。![image-20250415212435171](E:/Blog/source/im

 **按`:q`退出。**

这个初始的界面非常简陋,我们先进行核心功能配置,后续在更改主题。

nvim的用户级配置文件在`~/.config/nvim`中。



### 安装包管理器

我们选择Lazy作为包管理器,他和其他的包管理器相比起来,在懒加载、UI界面等方面有显著优势,可以减小配置复杂度。

访问[lazy.nvim官网](https://lazy.folke.io/installation),官方文档永远都是最好的教程。这里有一段代码

```lua
-- Bootstrap lazy.nvim
local lazypath = vim.fn.stdpath("data") .. "/lazy/lazy.nvim"
if not (vim.uv or vim.loop).fs_stat(lazypath) then
  local lazyrepo = "https://github.com/folke/lazy.nvim.git"
  local out = vim.fn.system({ "git", "clone", "--filter=blob:none", "--branch=stable", lazyrepo, lazypath })
  if vim.v.shell_error ~= 0 then
    vim.api.nvim_echo({
      { "Failed to clone lazy.nvim:\n", "ErrorMsg" },
      { out, "WarningMsg" },
      { "\nPress any key to exit..." },
    }, true, {})
    vim.fn.getchar()
    os.exit(1)
  end
end
vim.opt.rtp:prepend(lazypath)

-- Make sure to setup `mapleader` and `maplocalleader` before
-- loading lazy.nvim so that mappings are correct.
-- This is also a good place to setup other settings (vim.opt)
vim.g.mapleader = " "
vim.g.maplocalleader = "\\"

-- Setup lazy.nvim
require("lazy").setup({
  spec = {
    -- import your plugins
    { import = "plugins" },
  },
  -- Configure any other settings here. See the documentation for more details.
  -- colorscheme that will be used when installing plugins.
  install = { colorscheme = { "habamax" } },
  -- automatically check for plugin updates
  checker = { enabled = true },
})
```

这段代码主要在为Lazy包管理器初始化,所以按照官网的目录结构,我们在`~/.config/nvim/lua`下新建一个config文件夹,把代码粘贴进去。

注意到,代码中有一段是导入plugins这个模块,所以我们需要在`~/.config/nvim/lua`下创建一个plugins文件夹

然后在`~/.config/nvim/init.lua`里粘贴这行代码

```lua
require("config.lazy")
```

`init/lua`是Neovim的初始化配置文件,程序会首先读取这个文件里的代码。

这行代码是引入config/lazy这个文件,初始化Lazy,所以要放在第一行。

那么经过这一番操作,Lazy安装好了,我们的目录结构应该如下:

```
`-- nvim
    `-- lua
        `-- config
            `-- lazy.lua
```

现在在终端输入`nvim`启动Neovim,会发现有个报错

```
No specs found for module "plugins"
```

查阅[bug: No specs found for module "plugins" (Structured Setup) · folke/lazy.nvim · Discussion #1875](https://github.com/folke/lazy.nvim/discussions/1875)可得,这个问题是由于我们还没有安装任何插件导致的,他不影响什么,但是很烦人。如果想要关掉,可以在plugins下新建一个lua文件,然后输入

```lua
return{
    
}
```

这样就可以正常启动nvim不报错了。由此可见,Lazy安装插件是返回一张表,所以以后每次新建插件文件的时候都要填写以上内容。

### 基础配置

在安装插件前,我们要先进行一些基础配置,让这个编辑器用的舒服一点。

设置别名:``

```lua
local opt = vim.opt
```

声明一个本地变量opt=vim.opt,简写提高效率。

为什么要显示声明local?原因是lua脚本的所有变量默认都是全局变量,很怪对吧,我也觉得。

```lua
opt.relativenumber = false	-- 禁用相对行号
opt.number = true		   -- 启用绝对行号
vim.api.nvim_set_hl(0, "LineNr", { fg = "#FFD700" }) -- 所有行号设为金色
```

相对行号可以更容易的数行数来达到`hjkl/d/v等 + 行数`来快速进行文本编辑操作,但是我个人不太适应,就没开,后期可以通过修改按键配置来做到随时开关。



### 安装插件

#### neo-tree

neo-tree 编辑器的文件系统资源管理器。使用此插件,用户可以直观地浏览复杂的目录层次结构,快速打开文件进行读取或编辑,并执行基本的文件系统作。

仓库:[nvim-neo-tree/neo-tree.nvim: Neovim plugin to manage the file system and other tree like structures.](https://github.com/nvim-neo-tree/neo-tree.nvim)

进入Plugins目录,新建一个文件名为nerdtree的lua脚本。先把上文提到的返回表写进去。

```lua
-- neo-tree.lua
-- 代码来自仓库README
{
  "nvim-neo-tree/neo-tree.nvim",
  branch = "v3.x",
  dependencies = {
    "nvim-lua/plenary.nvim",
    "nvim-tree/nvim-web-devicons", -- not strictly required, but recommended
    "MunifTanjim/nui.nvim",
    -- {"3rd/image.nvim", opts = {}}, -- Optional image support in preview window: See `# Preview Mode` for more information
  },
  lazy = false, -- neo-tree will lazily load itself
  ---@module "neo-tree"
  ---@type neotree.Config?
  opts = {
    -- fill any relevant options here
  },
}
```

插件装好了发现,不知道怎么打开啊。这时候就需要我们自己动手改一个舒服点的键位。在`~/.config/nvim/lua/config`中新建一个keymaps.lua,并在init.lua中引用

```lua
-- 习惯上,我喜欢把leader键映射到空格,敲起来顺手
-- 基础按键配置
vim.g.mapleader = " "
local opt = { noremap = true, slient = true }
-- 窗口切换更顺手,少按一个w
vim.keymap.set("n", "<C-h>", "<C-w>h")
vim.keymap.set("n", "<C-j>", "<C-w>j")
vim.keymap.set("n", "<C-k>", "<C-w>k")
vim.keymap.set("n", "<C-l>", "<C-w>l")

-- Neotree
vim.keymap.set("n", "<Leader>e", "<cmd>Neotree toggle<CR>", opts)
```

#### 代码补全、格式化、检查

要让vim看起来像一个正经的处理代码的文本编辑器,这些是必不可少的。

```lua
-- 文件路径:lua/plugins/code-edit.lua
return {
  -- LSP 核心组件 (必须最先声明)
  {
    "neovim/nvim-lspconfig",
    dependencies = {
      "williamboman/mason.nvim",
      "williamboman/mason-lspconfig.nvim",
    },
    config = function()
      -- 延迟加载 LSP 配置
      vim.schedule(function()
        local lspconfig = require("lspconfig")
        local mason = require("mason")
        local mason_lspconfig = require("mason-lspconfig")

        -- 通用 LSP 配置
        local on_attach = function(client, bufnr)
          -- 快捷键配置...
        end

        mason.setup()
        mason_lspconfig.setup({
          ensure_installed = { "tsserver", "cssls", "html", "clangd", "eslint" }
        })

        mason_lspconfig.setup_handlers({
          function(server_name)
            lspconfig[server_name].setup({
              on_attach = on_attach,
              capabilities = require("cmp_nvim_lsp").default_capabilities(),
              -- 各语言特殊配置...
            })
          end
        })
      end)
    end
  },

  -- 自动补全引擎(需在 LSP 之后加载)
  {
    "hrsh7th/nvim-cmp",
    dependencies = {
      "hrsh7th/cmp-nvim-lsp",
      "L3MON4D3/LuaSnip",
    },
    config = function()
      -- cmp 配置...
    end
  },

  -- 格式化工具
  {
    "stevearc/conform.nvim",
    config = function()
      -- conform 配置...
    end
  },

  -- 语法高亮增强
  {
    "nvim-treesitter/nvim-treesitter",
    build = ":TSUpdate",
    config = function()
      -- treesitter 配置...
    end
  }
}
```

代码编辑器的配置就告一段落了,真写规范的代码还是建议vscode(虽然有点偏离终端的主题了)。如果你爱折腾vim那当然也可以,工具自己用的趁手就是最好的。



## Oh-My-Zsh

Oh My Zsh 是基于 zsh 命令行的一个扩展工具集,提供了丰富的扩展功能。

### 安装**ZSH**

`sudo pacman -S zsh`

设置默认终端为zsh

`chsh -s /bin/zsh`

### 安装Oh-My-Zsh

国内镜像

`sh -c "$(curl -fsSL https://gitee.com/pocmon/ohmyzsh/raw/master/tools/install.sh)"`

我个人觉得这个主题就挺好的了,不多改动。

#### 插件安装

#### zsh -autosuggestions 

预测命令

```bash
git clone https://github.moeyy.xyz/https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
```

#### zsh-syntax-highlighting

语法检查,命令正确时为绿色,否则红色。

```
git clone https://github.moeyy.xyz/https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
```


#### 启用插件

`nvim ~/.zshrc`

修改:

```
plugins=(
  git
  zsh-autosuggestions
  zsh-syntax-highlighting
  )
```

重启终端即可启用。

#### 别名设置

别名可以更快地帮助我们启用命令

`nvim ~/.zshrc`

```
alias s="neofetch"
alias vim="nvim"
alias c="clear"
alias gcl = "git clone"
```

这里根据我的习惯改了几个别名。