diff options
Diffstat (limited to 'content/posts/从0开始搭建自己的终端开发环境.md')
| -rw-r--r-- | content/posts/从0开始搭建自己的终端开发环境.md | 355 |
1 files changed, 355 insertions, 0 deletions
diff --git a/content/posts/从0开始搭建自己的终端开发环境.md b/content/posts/从0开始搭建自己的终端开发环境.md new file mode 100644 index 0000000..14009c9 --- /dev/null +++ b/content/posts/从0开始搭建自己的终端开发环境.md @@ -0,0 +1,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了。,官方文档永远都是最好的教程。这里有一段代码 + +```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" +``` + +这里根据我的习惯改了几个别名。 |
