<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Eric&apos;s Blog</title>
    <description>专注于编程技术、开源项目与生活感悟的独立博客</description>
    <link>https://wsdjeg.net/</link>
    <atom:link href="https://wsdjeg.net/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Tue, 21 Jul 2026 06:09:08 +0000</pubDate>
    <lastBuildDate>Tue, 21 Jul 2026 06:09:08 +0000</lastBuildDate>
    <generator>Jekyll v4.4.1</generator>
    
      <item>
        <title>别拿 X 当创作主阵地：被幽灵禁言后我才明白</title>
        <description>X 的 ghost ban 悄无声息，非登录状态下内容完全不可见，搜索引擎收录归零。别把创作主阵地放在别人的平台上。</description>
        <pubDate>Tue, 14 Jul 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/dont-use-x-as-primary-platform/</link>
        <guid isPermaLink="true">https://wsdjeg.net/dont-use-x-as-primary-platform/</guid>
        
        <category>X</category>
        
        <category>Twitter</category>
        
        <category>shadowban</category>
        
        <category>ghost-ban</category>
        
        <category>社交平台</category>
        
        
        <category>随笔</category>
        
      </item>
    
      <item>
        <title>chat.nvim v1.8.0：自动重试、用户画像与定时任务重写</title>
        <description>chat.nvim v1.8.0 正式发布，新增 LLM 请求自动重试、用户画像、Markdown 渲染、定时任务架构重写等重大更新。</description>
        <pubDate>Fri, 10 Jul 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/chat-nvim-v1-8-0-release/</link>
        <guid isPermaLink="true">https://wsdjeg.net/chat-nvim-v1-8-0-release/</guid>
        
        <category>neovim</category>
        
        <category>chat.nvim</category>
        
        <category>release</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>chat.nvim 定时任务的设计与实现</title>
        <description>chat.nvim 定时任务功能的设计思路与实现方案，让 AI 助手具备主动执行任务的能力。</description>
        <pubDate>Sat, 27 Jun 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/chat-nvim-scheduled-tasks/</link>
        <guid isPermaLink="true">https://wsdjeg.net/chat-nvim-scheduled-tasks/</guid>
        
        <category>neovim</category>
        
        <category>chat.nvim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>回乡记：老学校与旧友</title>
        <description>一座被时间封存的老学校，一段寻访旧友的意外旅程。</description>
        <pubDate>Wed, 24 Jun 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/back-to-hometown/</link>
        <guid isPermaLink="true">https://wsdjeg.net/back-to-hometown/</guid>
        
        <category>回乡</category>
        
        <category>童年</category>
        
        <category>寻友</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>AI 越狱新姿势：当 Makefile 成为沙箱逃逸的后门</title>
        <description>AI 通过 Makefile 绕过沙箱限制的安全分析，揭示 Tool Use 中的潜在风险。</description>
        <pubDate>Tue, 16 Jun 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/ai-jailbreak-makefile-sandbox-escape/</link>
        <guid isPermaLink="true">https://wsdjeg.net/ai-jailbreak-makefile-sandbox-escape/</guid>
        
        <category>AI安全</category>
        
        <category>沙箱逃逸</category>
        
        <category>chat.nvim</category>
        
        <category>LLM</category>
        
        <category>Tool Use</category>
        
        
        <category>安全思考</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第二十四章：Vim Runtime</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

Vim会自动查找一些特殊的路径，这些都是Vim runtime路径的例子。本章向您展示它们什么时候被调用，帮助您更进一步理解和定制Vim。

&lt;h2 id=&quot;runtime路径&quot;&gt;Runtime路径&lt;/h2&gt;

在Unix机器中，vim runtime路径是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/&lt;/code&gt;。运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h runtimepath&lt;/code&gt; 查看不同操作系统的路径。

&lt;h2 id=&quot;plugin脚本&quot;&gt;Plugin脚本&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/plugin/&lt;/code&gt; 中的脚本每次Vim启动时都会执行，可用于执行初始化脚本。

&lt;h2 id=&quot;文件类型检测&quot;&gt;文件类型检测&lt;/h2&gt;

确保 vimrc 中包含：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;filetype plugin indent on
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;文件名检测&quot;&gt;文件名检测&lt;/h3&gt;

使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ftdetect/&lt;/code&gt; 目录：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot; ~/.vim/ftdetect/chocodonut.vim
autocmd BufNewFile,BufRead *.chocodonut set filetype=chocodonut
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

或使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filetype.vim&lt;/code&gt; 文件：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot; ~/.vim/filetype.vim
if exists(&quot;did_load_filetypes&quot;)
  finish
endif

augroup donutfiletypedetection
  autocmd! BufRead,BufNewFile *.plaindonut setfiletype plaindonut
augroup END
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;基于文件内容检测&quot;&gt;基于文件内容检测&lt;/h3&gt;

使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scripts.vim&lt;/code&gt;：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot; ~/.vim/scripts.vim
if did_filetype()
  finish
endif

if getline(1) =~ &apos;^\\&amp;lt;donutify\\&amp;gt;&apos;
  setfiletype donut
endif
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;文件类型插件&quot;&gt;文件类型插件&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/ftplugin/&lt;/code&gt; 中的脚本仅在打开对应文件类型时执行。例如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/ftplugin/chocodonut.vim&lt;/code&gt; 仅在打开 chocodonut 文件时运行。

&lt;h2 id=&quot;缩进文件&quot;&gt;缩进文件&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/indent/&lt;/code&gt; 存储缩进相关代码，工作方式与ftplugin类似。

&lt;h2 id=&quot;颜色&quot;&gt;颜色&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/colors/&lt;/code&gt; 存储颜色主题。此目录中的文件会在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:color&lt;/code&gt; 命令中显示。

&lt;h2 id=&quot;语法高亮&quot;&gt;语法高亮&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/syntax/&lt;/code&gt; 定义语法高亮：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot; ~/.vim/syntax/chocodonut.vim
syntax keyword donutKeyword donut
highlight link donutKeyword Keyword
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

推荐 &lt;a href=&quot;https://github.com/sheerun/vim-polyglot&quot;&gt;vim-polyglot&lt;/a&gt; 插件提供多语言语法高亮。

&lt;h2 id=&quot;文档&quot;&gt;文档&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/doc/&lt;/code&gt; 存储文档。运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:helptags ~/.vim/doc/&lt;/code&gt; 生成帮助入口。

&lt;h2 id=&quot;延时加载脚本&quot;&gt;延时加载脚本&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/autoload/&lt;/code&gt; 中的脚本仅在被显式调用时才加载：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot; ~/.vim/autoload/tasty.vim
function tasty#donut()
  echo &quot;tasty#donut&quot;
endfunction
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

调用：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:call tasty#donut()&lt;/code&gt;

命名惯例：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fileName#functionName()&lt;/code&gt;，井号(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#&lt;/code&gt;)是必须的。

&lt;h2 id=&quot;after脚本&quot;&gt;After脚本&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/after/&lt;/code&gt; 的结构是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/&lt;/code&gt;的镜像，其中的脚本最后执行，用于重载脚本。

&lt;h2 id=&quot;vimruntime&quot;&gt;$VIMRUNTIME&lt;/h2&gt;

Vim有环境变量 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$VIMRUNTIME&lt;/code&gt; 用来加载默认脚本和支持文件。运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:e $VIMRUNTIME&lt;/code&gt; 查看。

&lt;h2 id=&quot;runtimepath选项&quot;&gt;Runtimepath选项&lt;/h2&gt;

运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:set runtimepath?&lt;/code&gt; 查看runtime路径。插件管理器会将每个插件添加到runtime路径中。

添加自定义目录到runtime路径：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;set rtp+=$HOME/box/of/donuts/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;聪明地学习runtime&quot;&gt;聪明地学习Runtime&lt;/h2&gt;

浏览您最喜欢的Vim插件仓库，仔细研究一下它的目录结构。现在您已经理解了Vim的目录结构，您可以准备学习Vimscript了。

</description>
        <pubDate>Sat, 16 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch24-vim-runtime/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch24-vim-runtime/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>runtime</category>
        
        <category>目录结构</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第二十三章：Vim 软件包</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

从Vim 8开始，Vim自带了一个内置的插件管理器，名叫 &lt;em&gt;软件包（packages）&lt;/em&gt;。在本章，您将学习如何使用Vim软件包来安装插件。

要查看是否支持软件包，运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:echo has(&apos;packages&apos;)&lt;/code&gt;（返回1表示支持）。

&lt;h2 id=&quot;包目录&quot;&gt;包目录&lt;/h2&gt;

在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/&lt;/code&gt; 中创建 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pack/&lt;/code&gt; 子目录（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/pack/&lt;/code&gt;）。Vim会在这个子文件夹内自动搜索插件。

&lt;h2 id=&quot;两种加载方式&quot;&gt;两种加载方式&lt;/h2&gt;

&lt;h3 id=&quot;自动加载&quot;&gt;自动加载&lt;/h3&gt;

将插件放置在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;start/&lt;/code&gt; 子目录中：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;~/.vim/pack/*/start/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; 可以是任意名字。例如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;packdemo/&lt;/code&gt;：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;~/.vim/pack/packdemo/start/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

克隆插件：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;cd ~/.vim/pack/packdemo/start/
git clone https://github.com/preservim/nerdtree.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

下一次启动Vim时，插件自动可用。

&lt;h3 id=&quot;手动加载&quot;&gt;手动加载&lt;/h3&gt;

将插件放置在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;opt/&lt;/code&gt; 目录中：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;~/.vim/pack/packdemo/opt/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

需要手动加载：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:packadd killersheep
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

手动加载适用于不是所有时候都在用的插件，避免拖慢Vim启动。

也可以有条件地加载：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if has(&apos;nvim&apos;)
  packadd! neovim-only-plugin
else
  packadd! generic-vim-plugin
endif
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;组织管理软件包&quot;&gt;组织管理软件包&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; 可以用来按类型分组：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;~/.vim/pack/colors/start/
~/.vim/pack/syntax/start/
~/.vim/pack/games/start/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;聪明地添加插件&quot;&gt;聪明地添加插件&lt;/h2&gt;

如果您是极简主义者，可以尝试Vim软件包。如果您是一名插件重度使用者，可能仍需要插件管理器（如vim-plug），因为它可以方便地批量更新插件。

</description>
        <pubDate>Fri, 15 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch23-vim-packages/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch23-vim-packages/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>软件包</category>
        
        <category>packages</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第二十二章：Vimrc</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

在先前的章节中，您学习了如何使用Vim。在本章，您将学习如何组织和配置Vimrc。

&lt;h2 id=&quot;vim如何找到vimrc&quot;&gt;Vim如何找到Vimrc&lt;/h2&gt;

Vim在以下路径按顺序查找vimrc文件：
&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$VIMINIT&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.vimrc&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.vim/vimrc&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$EXINIT&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$HOME/.exrc&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$VIMRUNTIME/default.vim&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

第一个被找到的vimrc文件将被加载，其余的被忽略。

&lt;h2 id=&quot;应该把什么放在vimrc中&quot;&gt;应该把什么放在Vimrc中？&lt;/h2&gt;

答案是，”任何您想放的东西”。直接复制粘贴别人的vimrc文件的确是一个诱惑，但您应当抵制这个诱惑。确保您知道这个vimrc干了什么，以及它是否符合你的实际需要。

&lt;h2 id=&quot;vimrc基础内容&quot;&gt;Vimrc基础内容&lt;/h2&gt;

一个vimrc是以下内容的集合：
&lt;ul&gt;
  &lt;li&gt;插件&lt;/li&gt;
  &lt;li&gt;设置&lt;/li&gt;
  &lt;li&gt;自定义函数&lt;/li&gt;
  &lt;li&gt;自定义命令&lt;/li&gt;
  &lt;li&gt;键盘映射&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;插件&quot;&gt;插件&lt;/h3&gt;

使用 &lt;a href=&quot;https://github.com/junegunn/vim-plug&quot;&gt;vim-plug&lt;/a&gt; 管理插件：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;call plug#begin(&apos;~/.vim/plugged&apos;)
  Plug &apos;mattn/emmet-vim&apos;
  Plug &apos;preservim/nerdtree&apos;
call plug#end()
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:PlugInstall&lt;/code&gt; 安装插件，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:PlugClean&lt;/code&gt; 删除不使用的插件。

&lt;h3 id=&quot;设置&quot;&gt;设置&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;set relativenumber number
set tabstop=2
set nocompatible
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;自定义命令&quot;&gt;自定义命令&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:command! GimmeDate echo call(&quot;strftime&quot;, [&quot;%F&quot;])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-nargs&lt;/code&gt; 限定参数数目，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;args&amp;gt;&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;f-args&amp;gt;&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;q-args&amp;gt;&lt;/code&gt; 传递参数。

&lt;h3 id=&quot;键盘映射&quot;&gt;键盘映射&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nnoremap &amp;lt;silent&amp;gt; &amp;lt;C-f&amp;gt; :GFiles&amp;lt;CR&amp;gt;
inoremap jk &amp;lt;esc&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; 表示普通模式。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nore&lt;/code&gt; 表示禁止递归。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; 是键盘映射命令。&lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;务必使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nnoremap&lt;/code&gt; 而非 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nmap&lt;/code&gt;&lt;/strong&gt;，以避免无限循环。

设置 leader 键：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let mapleader = &quot;\&amp;lt;space&amp;gt;&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;组织管理vimrc&quot;&gt;组织管理Vimrc&lt;/h2&gt;

&lt;h3 id=&quot;划分您的vimrc&quot;&gt;划分您的vimrc&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;source $HOME/.vim/settings/plugins.vim
source $HOME/.vim/settings/configs.vim
source $HOME/.vim/settings/functions.vim
source $HOME/.vim/settings/mappings.vim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;折叠您的vimrc文件&quot;&gt;折叠您的vimrc文件&lt;/h3&gt;

使用标志折叠（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;}&lt;/code&gt;）保持单个文件有序。

&lt;h2 id=&quot;启动vim时加载不加载vimrc&quot;&gt;启动Vim时加载/不加载Vimrc&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim -u NONE          &quot; 不加载vimrc和插件
vim -u NORC          &quot; 不加载vimrc，但加载插件
vim --noplugin       &quot; 加载vimrc，但不加载插件
vim -u ~/.vimrc-backup  &quot; 加载其他vimrc
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;聪明地配置vimrc&quot;&gt;聪明地配置Vimrc&lt;/h2&gt;

学习构建您的Vimrc最好是首先阅读他人的vimrc文件，然后逐渐地建立自己的。最好的vimrc是最适合您的工作需要和编辑风格的。

</description>
        <pubDate>Thu, 14 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch22-vimrc/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch22-vimrc/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>vimrc</category>
        
        <category>配置</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第二十一章：多文件操作</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

多文件编辑更新是一个值得掌握、非常有用的编辑工具。本章，您将学到如何在Vim中进行多文件编辑的更多不同方法。

&lt;h2 id=&quot;在多个文件中执行命令的几种方法&quot;&gt;在多个文件中执行命令的几种方法&lt;/h2&gt;

要在多个文件中执行命令，Vim有8种方法：
&lt;ul&gt;
  &lt;li&gt;参数列表 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;argdo&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;缓冲区列表 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bufdo&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;窗口列表 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;windo&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;tab 列表(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tabdo&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;快速修复列表 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdo&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;文件方式的快速修复列表 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cfdo&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;位置列表 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ldo&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;文件方式的位置列表 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lfdo&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

实际上，大部分时间您可能只会用到1种或2种。它们的运行方式都大体相同：分别创建一个列表，然后向列表传递一个您想执行的命令。

&lt;h2 id=&quot;参数列表&quot;&gt;参数列表&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:args file1 file2 file3
:args *.js
:args **/*.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

遍历：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:first&lt;/code&gt; 跳至第一个文件&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:last&lt;/code&gt; 跳到最后一个文件&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:next&lt;/code&gt; / &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:prev&lt;/code&gt; 向前/向后移动&lt;/li&gt;
&lt;/ul&gt;

在所有匹配文件中执行替换：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:args **/*.yml
:argdo %s/donut/pancake/g | update
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:arga&lt;/code&gt; 添加文件到现有列表（不会覆盖）。

&lt;h2 id=&quot;缓冲区列表&quot;&gt;缓冲区列表&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:buffers          &quot; 显示缓冲区列表
:bufdo %s/donut/pancake/g | update
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;窗口列表和选项卡列表&quot;&gt;窗口列表和选项卡列表&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:windo &amp;lt;command&amp;gt;    &quot; 在每个窗口执行
:tabdo &amp;lt;command&amp;gt;    &quot; 在每个选项卡执行
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;快速修复列表&quot;&gt;快速修复列表&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:vimgrep /donut/ **/*.js
:copen              &quot; 打开快速修复窗口
:cclose             &quot; 关闭
:cnext / :cprev     &quot; 遍历
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdo&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cfdo&lt;/code&gt; 的区别：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdo&lt;/code&gt; 在修复列表中的每一个 &lt;strong&gt;元素&lt;/strong&gt; 上执行命令。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cfdo&lt;/code&gt; 在修复列表中的每一个 &lt;strong&gt;文件&lt;/strong&gt; 上执行命令。&lt;/li&gt;
&lt;/ul&gt;

如果命令作用域是整个文件（如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:%s&lt;/code&gt;），使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cfdo&lt;/code&gt;。如果命令作用域是某一行（如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:s&lt;/code&gt;），使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cdo&lt;/code&gt;。

&lt;h2 id=&quot;位置列表&quot;&gt;位置列表&lt;/h2&gt;

位置列表和快速修复列表很像，区别在于：任何时候都只能有1个快速修复列表，而位置列表每个窗口可以有一个。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:lvimgrep /bagel/ **/*.md
:lopen
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:ldo&lt;/code&gt; 对位置列表中每一个元素执行命令，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:lfdo&lt;/code&gt; 对位置列表中每一个文件执行命令。

&lt;h2 id=&quot;在vim中运行多文件操作命令&quot;&gt;在Vim中运行多文件操作命令&lt;/h2&gt;

当您需要在多个文件中改变一个变量名字的时候，您肯定想一个操作就全部搞定。建议从参数列表 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:argdo&lt;/code&gt; 开始并掌握它，然后再学下一个。

</description>
        <pubDate>Wed, 13 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch21-multiple-file-operations/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch21-multiple-file-operations/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>多文件</category>
        
        <category>argdo</category>
        
        <category>quickfix</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第二十章：视图、会话和 Viminfo</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

当您做了一段时间的项目后，您可能会发现这个项目逐渐形成了自己的设置、折叠、缓冲区、布局等。问题是，关闭 Vim 后，所有的这些更改都会丢失。本章中，您将学习如何使用 视图、会话 和 Viminfo 来保存项目的”快照”。

&lt;h2 id=&quot;视图&quot;&gt;视图&lt;/h2&gt;

视图是这三个部分中的最小子集，它是单个窗口相关设置的集合。如果您长时间在一个窗口上工作，并且想要保留其映射和折叠，您可以使用视图。

&lt;h3 id=&quot;配置视图属性&quot;&gt;配置视图属性&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set viewoptions?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

默认显示 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;viewoptions=folds,cursor,curdir&lt;/code&gt;。添加 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;localoptions&lt;/code&gt;：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set viewoptions+=localoptions
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;保存和加载视图&quot;&gt;保存和加载视图&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:mkview    &quot; 保存视图
:loadview  &quot; 加载视图
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim 允许您保存 9 个编号的视图（1-9）：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:mkview 1
:loadview 1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;自动创建视图&quot;&gt;自动创建视图&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;autocmd BufWinLeave *.txt mkview
autocmd BufWinEnter *.txt silent loadview
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;会话&quot;&gt;会话&lt;/h2&gt;

如果说视图保存了某个窗口的设置，那么会话则保存了所有窗口（包括布局）的信息。

&lt;h3 id=&quot;创建和加载会话&quot;&gt;创建和加载会话&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:mksession              &quot; 保存会话到 Session.vim
:source Session.vim     &quot; 加载会话
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

从终端加载：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim -S Session.vim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;配置会话属性&quot;&gt;配置会话属性&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set sessionoptions?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

默认值：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;blank,buffers,curdir,folds,help,tabpages,winsize,terminal&lt;/code&gt;

可存储的属性：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;blank&lt;/code&gt; 存储空窗口&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;buffers&lt;/code&gt; 存储缓冲区&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;folds&lt;/code&gt; 存储折叠&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;globals&lt;/code&gt; 存储全局变量&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;options&lt;/code&gt; 存储选项和映射&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;winsize&lt;/code&gt; 存储窗口大小&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tabpages&lt;/code&gt; 存储选项卡&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;viminfo&quot;&gt;Viminfo&lt;/h2&gt;

Viminfo 存储着：

&lt;ul&gt;
  &lt;li&gt;命令行历史记录。&lt;/li&gt;
  &lt;li&gt;字符串搜索历史记录。&lt;/li&gt;
  &lt;li&gt;输入行历史记录。&lt;/li&gt;
  &lt;li&gt;非空寄存器的内容。&lt;/li&gt;
  &lt;li&gt;多个文件的标记。&lt;/li&gt;
  &lt;li&gt;文件标记，它指向文件中的位置。&lt;/li&gt;
  &lt;li&gt;上次搜索 / 替换模式。&lt;/li&gt;
  &lt;li&gt;缓冲区列表。&lt;/li&gt;
  &lt;li&gt;全局变量。&lt;/li&gt;
&lt;/ul&gt;

通常，会话存储”外部”属性，Viminfo 存储”内部”属性。每个项目可以有一个会话文件，而 Viminfo 通常每台计算机只使用一个。

&lt;h3 id=&quot;读写-viminfo&quot;&gt;读写 Viminfo&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:wv ~/.viminfo_extra    &quot; 写入
:rv ~/.viminfo_extra    &quot; 读取
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

使用不同的 Viminfo 文件来启动 Vim：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim -i viminfo_extra
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;配置-viminfo-属性&quot;&gt;配置 Viminfo 属性&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set viminfo?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

默认值 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!,&apos;100,&amp;lt;50,s10,h&lt;/code&gt; 分解如下：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt; 保存以大写字母开头、却不包含小写字母的全局变量。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;100&lt;/code&gt; 保存最近 100 个文件的本地标记。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;50&lt;/code&gt; 每个寄存器最多保存 50 行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s10&lt;/code&gt; 为寄存器设置大小限制（10kb）。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h&lt;/code&gt; 禁用高亮显示。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;聪明地使用视图会话和-viminfo&quot;&gt;聪明地使用视图、会话和 Viminfo&lt;/h2&gt;

对于微型项目，可以使用视图；对于大型项目，可以使用会话。为您的编辑风格创建属于您自己的视图、会话和 Viminfo。如果您要换台计算机使用 Vim，只需加载您的设置，立刻就会感到就像在家里的工作环境一样！

</description>
        <pubDate>Tue, 12 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch20-views-sessions-viminfo/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch20-views-sessions-viminfo/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>视图</category>
        
        <category>会话</category>
        
        <category>viminfo</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第十九章：编译</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

编译是许多编程语言的重要主题。在本章中，您将学习如何在 Vim 中编译。此外，您将看到如何利用好 Vim 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:make&lt;/code&gt; 命令。

&lt;h2 id=&quot;从命令行编译&quot;&gt;从命令行编译&lt;/h2&gt;

您可以使用叹号运算符（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;）进行编译：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:!g++ hello.cpp -o hello
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;make命令&quot;&gt;Make命令&lt;/h2&gt;

Vim 有运行 makefile 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:make&lt;/code&gt; 命令。当您运行它时，Vim 会在当前目录查找 makefile 并执行它。

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:make&lt;/code&gt; 命令也接受终端中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make&lt;/code&gt; 命令的参数：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:make foo
:make list_pls
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果命令执行异常，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:make&lt;/code&gt; 命令将使用 Vim 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quickfix&lt;/code&gt; 来存储这些错误。运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:copen&lt;/code&gt; 可以打开 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quickfix&lt;/code&gt; 窗口来查看错误。

&lt;h2 id=&quot;使用-make-编译&quot;&gt;使用 Make 编译&lt;/h2&gt;

创建 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.cpp&lt;/code&gt; 文件和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;makefile&lt;/code&gt;：

&lt;div class=&quot;language-makefile highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nl&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;build, run&quot;&lt;/span&gt;
&lt;span class=&quot;nl&quot;&gt;build&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
	g++ hello.cpp &lt;span class=&quot;nt&quot;&gt;-o&lt;/span&gt; hello
&lt;span class=&quot;nl&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
	./hello
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:make build&lt;/code&gt; 编译，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:make run&lt;/code&gt; 执行。

&lt;h2 id=&quot;不同的make程序&quot;&gt;不同的Make程序&lt;/h2&gt;

当您运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:make&lt;/code&gt; 时，Vim 实际上会执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;makeprg&lt;/code&gt; 选项所设置的任何命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set makeprg=g++\ %
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&lt;/code&gt; 用于转义空格。Vim 中 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&lt;/code&gt; 符号代表当前文件。

使用去掉扩展名的原始文件名来命名编译后的输出：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set makeprg=g++\ %\ -o\ %&amp;lt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&amp;lt;&lt;/code&gt; 代表没有扩展名的当前文件名。

&lt;h2 id=&quot;保存时自动编译&quot;&gt;保存时自动编译&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:autocmd BufWritePost *.cpp make
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;切换编译器&quot;&gt;切换编译器&lt;/h2&gt;

Vim 有一个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:compiler&lt;/code&gt; 命令可以快速切换编译器：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:compiler ruby
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim 执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$VIMRUNTIME/compiler/ruby.vim&lt;/code&gt; 脚本，并将 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;makeprg&lt;/code&gt; 更改为使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ruby&lt;/code&gt; 命令。

&lt;h2 id=&quot;创建自定义编译器&quot;&gt;创建自定义编译器&lt;/h2&gt;

创建 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim/compiler/typescript.vim&lt;/code&gt;：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;CompilerSet makeprg=tsc
CompilerSet errorformat=%f:\ %m
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

第一行将 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;makeprg&lt;/code&gt; 设置为运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tsc&lt;/code&gt; 命令。第二行将错误格式设置为显示文件（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%f&lt;/code&gt;），后跟冒号和空格，最后是错误消息（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%m&lt;/code&gt;）。

&lt;h2 id=&quot;异步编译器&quot;&gt;异步编译器&lt;/h2&gt;

有时编译可能需要很长时间。有插件来运行异步进程：
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/tpope/vim-dispatch&quot;&gt;vim-dispatch&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/skywind3000/asyncrun.vim&quot;&gt;asyncrun.vim&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;异步make&quot;&gt;异步Make&lt;/h3&gt;

Vim-dispatch 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Make&lt;/code&gt; 命令与 Vim 的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:make&lt;/code&gt; 相似，但以异步方式运行。

&lt;h3 id=&quot;异步调度dispatch&quot;&gt;异步调度（Dispatch）&lt;/h3&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Dispatch&lt;/code&gt; 命令可以在Vim中运行任意外部命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:Dispatch rspec %
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h3 id=&quot;自动调度&quot;&gt;自动调度&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;autocmd BufEnter *_spec.rb let b:dispatch = &apos;bundle exec rspec %&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

每当进入一个以 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;_spec.rb&lt;/code&gt; 结尾的文件，运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Dispatch&lt;/code&gt; 将自动执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bundle exec rspec&lt;/code&gt;。

&lt;h2 id=&quot;聪明地学习编译&quot;&gt;聪明地学习编译&lt;/h2&gt;

在本章中，您了解到可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compiler&lt;/code&gt; 命令从Vim内部异步运行 &lt;em&gt;任何&lt;/em&gt; 进程，以完善您的编程工作流程。Vim 拥有通过其他程序来扩展自身的能力，这使其变得强大。

</description>
        <pubDate>Mon, 11 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch19-compile/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch19-compile/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>编译</category>
        
        <category>make</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第十八章：Git</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

Vim 和 Git 是两种实现不同功能的伟大工具。Vim 用于文本编辑，Git 用于版本控制。在本章中，您将学习如何将 Vim 和 Git 集成在一起。

&lt;h2 id=&quot;差异比较&quot;&gt;差异比较&lt;/h2&gt;

若要查看两个文件之间的差异，请运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vimdiff file1.txt file2.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vimdiff&lt;/code&gt; 并排显示两个缓冲区。不同的行会被高亮显示。

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;]c&lt;/code&gt; 跳转到下一处差异（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[c&lt;/code&gt; 跳回上一处差异）。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:diffput&lt;/code&gt; 将文本从当前缓冲区 &lt;em&gt;输出&lt;/em&gt; 到另一个缓冲区。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:diffget&lt;/code&gt; 从另一个缓冲区 &lt;em&gt;获取&lt;/em&gt; 文本到当前缓冲区。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;使用-vim-作为合并工具&quot;&gt;使用 Vim 作为合并工具&lt;/h2&gt;

将默认合并工具更改为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vimdiff&lt;/code&gt;：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git config merge.tool vimdiff
git config merge.conflictstyle diff3
git config mergetool.prompt false
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git mergetool&lt;/code&gt; 后，Vim 显示四个窗口：

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;LOCAL&lt;/code&gt; 包含本地的变化。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BASE&lt;/code&gt; 包含 LOCAL 和 REMOTE 的共同祖先。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;REMOTE&lt;/code&gt; 包含要被合并的内容。&lt;/li&gt;
&lt;/ul&gt;

使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:diffget LOCAL&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:diffget BASE&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:diffget REMOTE&lt;/code&gt; 来获取相应变化。完成后 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:wqall&lt;/code&gt; 保存退出。

&lt;h2 id=&quot;在-vim-中使用-git&quot;&gt;在 Vim 中使用 Git&lt;/h2&gt;

使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt; 叹号运算符运行 Git 命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:!git status
:!git commit
:!git diff
:!git add %         &quot; git add current file
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

添加不同Vim窗口中的多个文件：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;windo !git add %
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;插件&quot;&gt;插件&lt;/h2&gt;

流行的 Git 相关插件：
&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/airblade/vim-gitgutter&quot;&gt;vim-gitgutter&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/tpope/vim-fugitive&quot;&gt;vim-fugitive&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/junegunn/gv.vim&quot;&gt;gv.vim&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;vim-fugitive&quot;&gt;Vim-Fugitive&lt;/h2&gt;

&lt;h3 id=&quot;git-status&quot;&gt;Git Status&lt;/h3&gt;

运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Git&lt;/code&gt; 显示 git 概要窗口：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt; 暂存或取消暂存文件。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt; 暂存文件。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; 取消暂存文件。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;git-blame&quot;&gt;Git Blame&lt;/h3&gt;

运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Git blame&lt;/code&gt; 显示拆分的问责窗口：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; 关闭。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; 调整作者列大小。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; 调整提交列大小。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;gdiffsplit&quot;&gt;Gdiffsplit&lt;/h3&gt;

运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Gdiffsplit&lt;/code&gt; 执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vimdiff&lt;/code&gt;，比对索引或工作树中的版本与当前文件最新更改的区别。

&lt;h3 id=&quot;gwrite-和-gread&quot;&gt;Gwrite 和 Gread&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Gwrite&lt;/code&gt; 暂存更改（类似 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git add&lt;/code&gt;）。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Gread&lt;/code&gt; 还原文件至更改前的状态（类似 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git checkout&lt;/code&gt;），且操作可撤销。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;gclog&quot;&gt;Gclog&lt;/h3&gt;

运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Gclog&lt;/code&gt; 显示提交历史记录，使用 quickfix 遍历。

&lt;h2 id=&quot;聪明地学习-vim-和-git&quot;&gt;聪明地学习 Vim 和 Git&lt;/h2&gt;

每个人都有不同的 git 工作流，强烈建议试试上面列出的所有插件。要让 Vim-git 的集成工作得更好，一个显而易见的办法就是去深入了解 git。

</description>
        <pubDate>Sun, 10 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch18-git/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch18-git/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>Git</category>
        
        <category>vim-fugitive</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第十七章：折叠</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

在阅读文件时，经常会有一些不相关的文本会妨碍您理解。使用 Vim 折叠可以隐藏这些不必要的信息。

本章中，您将学习如何使用不同的折叠方法。

&lt;h2 id=&quot;手动折叠&quot;&gt;手动折叠&lt;/h2&gt;

想象您正在折叠一张纸来覆盖一些文本，实际的文本不会消失，它仍在那儿。Vim 折叠的工作方式与此相同，它&lt;em&gt;折叠&lt;/em&gt;一段文本，在显示时会隐藏起来，但实际上并不会真的删除它。

折叠操作符是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt;。

假设有如下文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Fold me
Hold me
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

把光标放在第一行，输入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zfj&lt;/code&gt;。Vim 将这两行折叠成一行。

上面的命令分解如下：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zf&lt;/code&gt; 是折叠操作符。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt; 是用于折叠操作符的动作。&lt;/li&gt;
&lt;/ul&gt;

您可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zo&lt;/code&gt; 打开/展开已折叠文本，使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zc&lt;/code&gt; 关闭/收缩文本。

折叠是一个操作符，所以它遵循语法规则（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;动词+名词&lt;/code&gt;）。例如，使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zfip&lt;/code&gt; 可以折叠内部段落；使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zfG&lt;/code&gt; 可以折叠至文件末尾。

最有用的一些命令是：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zR&lt;/code&gt; 展开所有折叠。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;zM&lt;/code&gt; 收缩所有折叠。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;za&lt;/code&gt; 切换折叠状态。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;不同的折叠方法&quot;&gt;不同的折叠方法&lt;/h2&gt;

Vim 有六种不同的折叠方法：
&lt;ol&gt;
  &lt;li&gt;手动折叠&lt;/li&gt;
  &lt;li&gt;缩进折叠&lt;/li&gt;
  &lt;li&gt;表达式折叠&lt;/li&gt;
  &lt;li&gt;语法折叠&lt;/li&gt;
  &lt;li&gt;差异折叠&lt;/li&gt;
  &lt;li&gt;标志折叠&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;缩进折叠&quot;&gt;缩进折叠&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set foldmethod=indent
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim 将会查看每行的开头有多少空格，并将它与 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;shiftwidth&apos;&lt;/code&gt; 选项进行比较，以此来决定该行可折叠性。

&lt;h2 id=&quot;表达式折叠&quot;&gt;表达式折叠&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set foldmethod=expr
:set foldexpr=getline(v:lnum)[0]==\\&quot;p\\&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim 会计算每行的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;foldexpr&apos;&lt;/code&gt; 值。如果返回 0，则不折叠行。如果返回 1，则该行的折叠级别为 1。

&lt;h2 id=&quot;语法折叠&quot;&gt;语法折叠&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set foldmethod=syntax
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

语法折叠是由Vim的语法高亮决定的。如果您使用了语法高亮插件，装上插件就可以直接使用语法折叠。

&lt;h2 id=&quot;差异折叠&quot;&gt;差异折叠&lt;/h2&gt;

运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vimdiff file1.txt file2.txt&lt;/code&gt; 时，Vim 会自动使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foldmethod=diff&lt;/code&gt;。

&lt;h2 id=&quot;标志折叠&quot;&gt;标志折叠&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set foldmethod=marker
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim 会寻找由 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;foldmarker&apos;&lt;/code&gt; 选项定义的特殊标志（默认 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;}&lt;/code&gt;），并标记折叠区域。

&lt;h2 id=&quot;持久化折叠&quot;&gt;持久化折叠&lt;/h2&gt;

要在关闭文件后保留折叠信息：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:mkview
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

重新打开后加载：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:loadview
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

自动保存和加载：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;autocmd BufWinLeave *.txt mkview
autocmd BufWinEnter *.txt silent loadview
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;聪明地学习折叠&quot;&gt;聪明地学习折叠&lt;/h2&gt;

当您学习折叠时，请从手动折叠开始，然后逐渐学习不同的技巧来使用缩进和标志折叠。最后，学习如何使用语法和表达式折叠。

</description>
        <pubDate>Sat, 09 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch17-fold/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch17-fold/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>折叠</category>
        
        <category>fold</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第十六章：标签</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

快速转到任意定义处，是文本编辑中一个非常有用的特性。在本章中，您将学习如何使用 Vim 标签来做到这一点。

&lt;h2 id=&quot;标签概述&quot;&gt;标签概述&lt;/h2&gt;

假设有人给了您一个新的代码库：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one = One.new
one.donut
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;One&lt;/code&gt;？&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;donut&lt;/code&gt;？跟随有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;One&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;donut&lt;/code&gt;定义的源代码，是帮助您理解的一个有效方法。您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fzf&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt;来搜索它们，但使用标签将更快。

把标签想象成地址簿，它存储的不是”姓名-地址”对，而是”定义-地址”对。

在普通模式下，您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-]&lt;/code&gt;跳转到定义。

&lt;h2 id=&quot;标签生成器&quot;&gt;标签生成器&lt;/h2&gt;

现代 Vim 不自带标签生成器，您需要额外下载它。推荐使用 &lt;a href=&quot;https://github.com/universal-ctags/ctags&quot;&gt;Universal ctags&lt;/a&gt;。

生成一个基本的标签文件：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ctags -R .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;R&lt;/code&gt; 选项告诉 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ctags&lt;/code&gt; 从当前位置 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;) 递归扫描文件。稍后，您应该在当前文件夹看到一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tags&lt;/code&gt; 文件。

&lt;h2 id=&quot;解剖标签文件&quot;&gt;解剖标签文件&lt;/h2&gt;

来看看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;donut&lt;/code&gt; 标签项：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;donut	one.rb	/^  def donut$/;&quot;	f	class:One
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

上面的标签项由四个部分组成：一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tagname&lt;/code&gt;、一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tagfile&lt;/code&gt;、一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tagaddress&lt;/code&gt;，以及标签选项。

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;donut&lt;/code&gt; 是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tagname&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;one.rb&lt;/code&gt; 是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tagfile&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/^  def donut$/&lt;/code&gt; 是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tagaddress&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f class:One&lt;/code&gt; 是标签选项，它告诉 Vim，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;donut&lt;/code&gt; 是一种函数 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt;)，并且是 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;One&lt;/code&gt; 类的一部分。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;标签文件&quot;&gt;标签文件&lt;/h2&gt;

如果运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:set tags?&lt;/code&gt;，您可能会看见 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tags=./tags,tags&lt;/code&gt;。Vim 会在当前文件所在路径和当前目录中查找标签文件。

要添加标签文件位置：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set tags+=path/to/my/tags/file
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;为大型项目生成标签&quot;&gt;为大型项目生成标签&lt;/h2&gt;

如果要排除 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node_modules&lt;/code&gt; 后执行 ctags：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ctags -R --exclude=node_modules .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

可以多次使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exclude&lt;/code&gt; 选项：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ctags -R --exclude=.git --exclude=vendor --exclude=node_modules --exclude=db --exclude=log .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;标签导航&quot;&gt;标签导航&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-]&lt;/code&gt; 跳转到光标下的定义。还有命令行模式：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:tag my-tag&lt;/code&gt;。

您还可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt; 来自动补全参数：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:tag d&amp;lt;Tab&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;标签优先级&quot;&gt;标签优先级&lt;/h2&gt;

如果有重复的标签项，Vim 会检查关键词的优先级。顺序是：

&lt;ol&gt;
  &lt;li&gt;当前文件中完全匹配的静态标签。&lt;/li&gt;
  &lt;li&gt;当前文件中完全匹配的全局标签。&lt;/li&gt;
  &lt;li&gt;其他文件中完全匹配的全局标签。&lt;/li&gt;
  &lt;li&gt;其他文件中完全匹配的静态标签。&lt;/li&gt;
  &lt;li&gt;当前文件中不区分大小写匹配的静态标签。&lt;/li&gt;
  &lt;li&gt;当前文件中不区分大小写匹配的全局标签。&lt;/li&gt;
  &lt;li&gt;其他文件中区分大小写匹配的全局标签。&lt;/li&gt;
  &lt;li&gt;当前文件中不区分大小写匹配的静态标签。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;选择性跳转标签&quot;&gt;选择性跳转标签&lt;/h2&gt;

使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:tselect&lt;/code&gt; 选择要跳转到哪个标签：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:tselect pancake
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:tjump&lt;/code&gt; 方法仅在有多个匹配项时才提示标签列表：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:tjump pancake
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tjump&lt;/code&gt; 在普通模式下有一个快捷键：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g Ctrl-]&lt;/code&gt;。

&lt;h2 id=&quot;标签的自动补全&quot;&gt;标签的自动补全&lt;/h2&gt;

在插入模式中输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-x Ctrl-]&lt;/code&gt;，Vim 将使用标签文件来自动补全。

&lt;h2 id=&quot;标签堆栈&quot;&gt;标签堆栈&lt;/h2&gt;

Vim 维持着一个标签堆栈。使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:tags&lt;/code&gt; 可以看到这个堆栈。要”弹出”堆栈，运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:pop&lt;/code&gt;，或按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-t&lt;/code&gt;。

&lt;h2 id=&quot;自动生成标签&quot;&gt;自动生成标签&lt;/h2&gt;

&lt;h3 id=&quot;在保存时生成标签&quot;&gt;在保存时生成标签&lt;/h3&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:autocmd BufWritePost *.rb silent !ctags -R .
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;autocmd&lt;/code&gt; 是 Vim 的自动命令方法。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;BufWritePost&lt;/code&gt; 是保存缓冲区时的一个事件。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.rb&lt;/code&gt; 是 ruby 文件的一种文件模式。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;silent&lt;/code&gt; 避免每次触发时提示。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!ctags -R .&lt;/code&gt; 是要执行的命令。&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;使用插件&quot;&gt;使用插件&lt;/h3&gt;

推荐使用 &lt;a href=&quot;https://github.com/ludovicchabant/vim-gutentags&quot;&gt;vim-gutentags&lt;/a&gt;，它的使用方法很简单，装上就可以直接使用。

&lt;h2 id=&quot;聪明地学习标签&quot;&gt;聪明地学习标签&lt;/h2&gt;

只要配置得当，标签是非常有用的。假设在一个新的代码库中，您想要搞清楚 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;functionFood&lt;/code&gt; 干了什么，您可以通过跳转到它的定义来搞懂它们。在那儿可以看到，它又调用了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;functionBreakfast&lt;/code&gt;。继续跟踪，发现还调用了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;functionPancake&lt;/code&gt;。进一步可以知道，这段代码和早餐吃煎饼有关。

现在您已经知道如何使用标签，通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h tags&lt;/code&gt; 可以学习更多有关标签的知识。

</description>
        <pubDate>Fri, 08 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch16-tags/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch16-tags/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>标签</category>
        
        <category>ctags</category>
        
        <category>跳转</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第十五章：命令行模式</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

在前三章中，您已经学习了如何使用搜索命令(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt;)、替换命令(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:s&lt;/code&gt;)、全局命令(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:g&lt;/code&gt;)，以及外部命令(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;)。这些都是命令行模式命令的一些例子。

在本章中，您将学习命令行模式的更多技巧。

&lt;h2 id=&quot;进入和退出命令行模式&quot;&gt;进入和退出命令行模式&lt;/h2&gt;

命令行模式本身也是一种模式，就像普通模式、输入模式、可视模式一样。在这种模式中，光标将转到屏幕底部，此时您可以输入不同的命令。

有 4 种进入命令行模式的方式：
&lt;ul&gt;
  &lt;li&gt;搜索命令 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;命令行指令 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;外部命令 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;

您可以从正常模式或可视模式进入命令行模式。

若要离开命令行模式，您可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;esc&amp;gt;&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-c&lt;/code&gt;、&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-[&lt;/code&gt;。

&lt;h2 id=&quot;重复上一个命令&quot;&gt;重复上一个命令&lt;/h2&gt;

您可以用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@:&lt;/code&gt; 来重复上一个命令行指令或外部命令。

如果您刚运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:s/foo/bar/g&lt;/code&gt;，执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@:&lt;/code&gt; 将重复该替换。如果您刚运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:.!tr &apos;[a-z]&apos; &apos;[A-Z]&apos;&lt;/code&gt;，执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@:&lt;/code&gt; 将重复上一次外部命令转换过滤。

&lt;h2 id=&quot;命令行模式快捷键&quot;&gt;命令行模式快捷键&lt;/h2&gt;

在命令行模式中，您可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Left&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Right&lt;/code&gt; 方向键，来左右移动一个字符。

如果需要移动一个单词，使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Shift-Left&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Shift-Right&lt;/code&gt;。

使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-b&lt;/code&gt;移动到该行的开始，使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-e&lt;/code&gt;移动到该行的结束。

和输入模式类似，在命令行模式中，有三种方法可以删除字符：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-h    删除一个字符
Ctrl-w    删除一个单词
Ctrl-u    删除一整行
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

最后，如果您想像编辑文本文件一样来编辑命令，可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-f&lt;/code&gt;。这样还可以查看过往的命令，并在这种”命令行编辑的普通模式”中编辑它们，同时还能按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enter&lt;/code&gt; 来运行它们。

&lt;h2 id=&quot;寄存器和自动补全&quot;&gt;寄存器和自动补全&lt;/h2&gt;

当处于命令行模式时，您可以像在插入模式中一样使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r&lt;/code&gt; 从Vim寄存器中插入文本。如果您在寄存器 a 中存储了字符串 “foo” ，您可以执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r a&lt;/code&gt; 从寄存器a中插入该文本。

另外，您也可以按 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r Ctrl-w&lt;/code&gt; 获取当前光标下的单词（按 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r Ctrl-A&lt;/code&gt; 获取当前光标下的词组）。还可以按 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r Ctrl-l&lt;/code&gt; 获取当前光标所在行。按 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r Ctrl-f&lt;/code&gt; 获取光标下的文件名。

您也可以对已存在的命令使用自动补全。要自动补全 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo&lt;/code&gt; 命令，当处于命令行模式时，首先输入 “ec”，接着按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;，此时您应该能在左下角看到一些 “ec” 开头的 Vim 命令。按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-n&lt;/code&gt; 可以跳到下一个选项。按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Shift-Tab&amp;gt;&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-p&lt;/code&gt; 可以回到上一个选项。

&lt;h2 id=&quot;历史记录窗口&quot;&gt;历史记录窗口&lt;/h2&gt;

您可以查看命令行指令和搜索项的历史记录。

运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:his :&lt;/code&gt; 来查看命令行指令的历史记录。默认情况下，Vim 存储最后 50 个命令。运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:set history=100&lt;/code&gt; 可以将 Vim 记住的条目总数更改为 100。

一个更有用的做法是使用命令行历史记录窗口，按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q:&lt;/code&gt;将会打开一个可搜索、可编辑的历史记录窗口。

如果您当前任务是执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s/verylongsubstitutionpattern/donut/g&lt;/code&gt;，在历史记录中找到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s/verylongsubstitutionpattern/pancake/g&lt;/code&gt;，然后直接编辑它！将 “pancake” 改为 “donut” ，然后按 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Enter&amp;gt;&lt;/code&gt;。Vim立刻执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s/verylongsubstitutionpattern/donut/g&lt;/code&gt; 命令。

类似地，运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:his /&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:his ?&lt;/code&gt; 可以查看搜索记录。要想打开搜索历史记录窗口，您可以运行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q/&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q?&lt;/code&gt;。

要退出这个窗口，按 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-c&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-w c&lt;/code&gt;, 或输入 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:quit&lt;/code&gt;。

&lt;h2 id=&quot;更多命令行指令&quot;&gt;更多命令行指令&lt;/h2&gt;

Vim有几百个内置指令，要查看Vim的所有指令，执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h ex-cmd-index&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h :index&lt;/code&gt;。

&lt;h2 id=&quot;聪明地学习命令行模式&quot;&gt;聪明地学习命令行模式&lt;/h2&gt;

对比其他三种模式，命令行模式就像是文本编辑中的瑞士军刀。寥举几例，您可以编辑文本、修改文件和执行命令。本章是命令行模式的零碎知识的集合。同时，Vim 模式的介绍也走向尾声。现在，您已经知道如何使用普通、输入、可视以及命令行模式，您可以比以往更快地使用 Vim 来编辑文本了。

</description>
        <pubDate>Thu, 07 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch15-command-line-mode/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch15-command-line-mode/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>命令行模式</category>
        
        <category>Ex命令</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>手机移动端 AI 助手：Termux + chat.nvim + Nova</title>
        <description>在 Android 手机上通过 Termux 安装 chat.nvim 和 Nova，打造移动端 AI 编程环境。</description>
        <pubDate>Wed, 06 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/termux-chat-nvim-nova/</link>
        <guid isPermaLink="true">https://wsdjeg.net/termux-chat-nvim-nova/</guid>
        
        <category>Termux</category>
        
        <category>Neovim</category>
        
        <category>chat.nvim</category>
        
        <category>Nova</category>
        
        <category>AI</category>
        
        <category>移动开发</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第十四章：外部命令</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

在Unix系统内部，您会发现许多小型的，超专业化命令，每个命令只做一件事（而且能很好地完成）。您可以将这些命令链接在一起以共同解决一个复杂的问题。如果可以从Vim内部使用这些命令，那不是很好吗？

答案是肯定的!在本章中，您将学习如何扩展Vim以使其与外部命令无缝协作。

&lt;h2 id=&quot;bang-命令&quot;&gt;Bang 命令&lt;/h2&gt;

Vim有一个Bang(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;)命令，可以执行三件事：

&lt;ol&gt;
  &lt;li&gt;将外部命令的STDOUT读入当前缓冲区。&lt;/li&gt;
  &lt;li&gt;将缓冲区的内容作为STDIN写入外部命令。&lt;/li&gt;
  &lt;li&gt;从Vim内部执行外部命令。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;将外部命令的标准输出stdout读入vim&quot;&gt;将外部命令的标准输出STDOUT读入Vim&lt;/h2&gt;

将外部命令的STDOUT读入当前缓冲区的语法为：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:r !{cmd}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

要获取&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt;命令的结果，请运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:r !ls
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您可以从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;curl&lt;/code&gt;命令读取数据：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:r !curl -s &apos;https://jsonplaceholder.typicode.com/todos/1&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

r命令也接受一个地址：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:10r !cat file1.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;将缓冲区内容写入外部命令&quot;&gt;将缓冲区内容写入外部命令&lt;/h2&gt;

语法为：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:w !cmd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果您具有以下表达式：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;console.log(&quot;Hello Vim&quot;);
console.log(&quot;Vim is awesome&quot;);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

确保在计算机中安装了&lt;a href=&quot;https://nodejs.org/&quot;&gt;node&lt;/a&gt;，然后运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:w !node
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:w !node&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:w! node&lt;/code&gt;形式上区别很小，但功能上相隔千里。使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:w !node&lt;/code&gt;，您是将当前缓冲区中的文本”写入”到外部命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;node&lt;/code&gt;中。用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:w! node&lt;/code&gt;，则您将强制保存文件并将其命名为”node”。

&lt;h2 id=&quot;执行外部命令&quot;&gt;执行外部命令&lt;/h2&gt;

语法为：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:!cmd
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

要以长格式查看当前目录的内容，请运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:!ls -ls
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

要终止在PID 3456上运行的进程，可以运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:!kill -9 3456
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;过滤文本&quot;&gt;过滤文本&lt;/h2&gt;

如果给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;范围，则可用于过滤文本。假设您有：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hello vim
hello vim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

让我们使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tr&lt;/code&gt; (translate)命令将当前行大写。运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:.!tr &apos;[:lower:]&apos; &apos;[:upper:]&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

结果：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;HELLO VIM
hello vim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

命令分解：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.!&lt;/code&gt; 在当前行执行filter命令。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!tr &apos;[:lower:]&apos; &apos;[:upper:]&apos;&lt;/code&gt; 调用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tr&lt;/code&gt;外部命令将所有小写字符替换为大写字符。&lt;/li&gt;
&lt;/ul&gt;

假设您需要使用awk命令删除两行的第二列：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:%!awk &quot;{print $1}&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

结果：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hello
hello
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您可以使用管道运算符（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&lt;/code&gt;）链接多个命令，就像在终端中一样。假设您有一个包含这些美味早餐的文件：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;name price
chocolate pancake 10
buttermilk pancake 9
blueberry pancake 12
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果您需要根据价格对它们进行排序，并且仅以均匀的间距显示菜单，则可以运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:%!awk &apos;NR &amp;gt; 1&apos; | sort -nk 3 | column -t
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

结果：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;buttermilk pancake 9
chocolate pancake 10
blueberry pancake 12
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;普通模式命令&quot;&gt;普通模式命令&lt;/h2&gt;

在普通模式下，Vim有一个过滤运算符（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;）。如果您有以下问候：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hello vim
hola vim
bonjour vim
salve vim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

要大写当前行和下面的行，可以运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;!jtr &apos;[a-z]&apos; &apos;[A-Z]&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

命令分解：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!j&lt;/code&gt; 运行常规命令过滤器运算符（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;），目标是当前行及其下方的行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tr &apos;[a-z]&apos; &apos;[A-Z]&apos;&lt;/code&gt;将小写字母替换为大写字母。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;聪明地学习外部命令&quot;&gt;聪明地学习外部命令&lt;/h2&gt;

Vim不是IDE。它是一种轻量级的模式编辑器，通过设计可以高度扩展。由于这种可扩展性，您可以轻松访问系统中的任何外部命令。这样，Vim离成为IDE仅一步之遥。有人说Unix系统是有史以来的第一个IDE。

Bang 命令的有用程度与您知道多少个外部命令相关。如果您的外部命令知识有限，请不要担心。以此作为持续学习的动力。每当您需要过滤文本时，请查看是否存在可以解决问题的外部命令。

</description>
        <pubDate>Wed, 06 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch14-external-commands/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch14-external-commands/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>外部命令</category>
        
        <category>shell</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第十三章：全局命令</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

到目前为止，您已经了解了如何使用点命令(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;)重复上一次更改，如何使用宏(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt;)重复动作以及将文本存储在寄存器中(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&lt;/code&gt;)。

在本章中，您将学习如何在全局命令中重复命令行命令。

&lt;h2 id=&quot;全局命令概述&quot;&gt;全局命令概述&lt;/h2&gt;

Vim的全局命令用于同时在多行上运行命令行命令。

全局命令具有以下语法：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/pattern/command
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pattern&lt;/code&gt;匹配包含该模式串的所有行，类似于替代命令中的模式串。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;command&lt;/code&gt;可以是任何命令行命令。全局命令通过对与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pattern&lt;/code&gt;匹配的每一行执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;command&lt;/code&gt;来工作。

如果您具有以下表达式：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const one = 1;
console.log(&quot;one: &quot;, one);

const two = 2;
console.log(&quot;two: &quot;, two);

const three = 3;
console.log(&quot;three: &quot;, three);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

要删除所有包含”console”的行，可以运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/console/d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

结果：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const one = 1;

const two = 2;

const three = 3;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

全局命令在与”console”模式串匹配的所有行上执行删除命令(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt;)。

&lt;h2 id=&quot;逆向匹配&quot;&gt;逆向匹配&lt;/h2&gt;

要在不匹配的行上运行全局命令，可以运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g!/{pattern}/{command}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

或者

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:v/{pattern}/{command}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:v/console/d&lt;/code&gt;，它将删除 &lt;strong&gt;不&lt;/strong&gt; 包含”console”的所有行。

&lt;h2 id=&quot;模式串&quot;&gt;模式串&lt;/h2&gt;

全局命令使用与替代命令相同的模式串系统。

要删除包含”one”或”two”的行，请运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/one\|two/d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

要删除包含任何一位数字的行，请运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/\d/d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;传递范围参数&quot;&gt;传递范围参数&lt;/h2&gt;

您可以在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt;命令之前传递一个范围：

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:1,5g/console/d&lt;/code&gt;  删除第1行和第5行之间匹配字符串”console”的行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:,5g/console/d&lt;/code&gt; 如果逗号前没有地址，则从当前行开始。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:3,g/console/d&lt;/code&gt; 如果逗号后没有地址，则在当前行结束。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:3g/console/d&lt;/code&gt; 如果只传递一个地址而不带逗号，则仅在第3行执行命令。&lt;/li&gt;
&lt;/ul&gt;

除了数字，您还可以将这些符号用作范围：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; 表示当前行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt; 表示文件的最后一行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+n&lt;/code&gt; 表示当前行之后的n行。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;普通模式命令&quot;&gt;普通模式命令&lt;/h2&gt;

您可以将全局命令和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:normal&lt;/code&gt;命令行命令一起运行。

如果您有以下文字：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const one = 1
console.log(&quot;one: &quot;, one)

const two = 2
console.log(&quot;two: &quot;, two)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

要添加”;”运行到每一行的末尾：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/./normal A;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:g&lt;/code&gt; 是全局命令。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/./&lt;/code&gt; 是”非空行”的模式。它匹配至少包含1个字符的行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;normal A;&lt;/code&gt; 运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:normal&lt;/code&gt;命令行命令。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A;&lt;/code&gt; 是普通模式命令，用于在该行的末尾插入”;”。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;执行宏&quot;&gt;执行宏&lt;/h2&gt;

您也可以使用全局命令执行宏。如果您有以下表达式：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const one = 1
console.log(&quot;one: &quot;, one);

const two = 2
console.log(&quot;two: &quot;, two);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

创建一个宏，以在寄存器”a”的这些行的末尾添加分号：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;qa0A;&amp;lt;esc&amp;gt;q
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

现在运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/const/normal @a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

所有带有”const”的行的末尾将带有”;”。

&lt;h2 id=&quot;递归全局命令&quot;&gt;递归全局命令&lt;/h2&gt;

全局命令本身是命令行命令的一种，因此您可以从技术上在全局命令中运行全局命令。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/console/g/two/d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

首先，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt;将查找包含模式”console”的行。然后，第二个”g”将从那三个匹配项中查找包含模式”two”的行。最后，它将删除该匹配项。

您也可以将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt;与&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;结合使用以找到正负模式：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/console/v/two/d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

它将查找 &lt;strong&gt;不&lt;/strong&gt; 包含”two”的行。

&lt;h2 id=&quot;更改定界符&quot;&gt;更改定界符&lt;/h2&gt;

您可以像替代命令一样更改全局命令的定界符。规则是相同的：您可以使用任何单字节字符，但字母，数字，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&lt;/code&gt;, 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&lt;/code&gt;除外。

要删除包含”console”的行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g@console@d
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果在全局命令中使用替代命令，则可以有两个不同的定界符：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;g@one@s+const+let+g
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;默认命令&quot;&gt;默认命令&lt;/h2&gt;

如果在全局命令中未指定任何命令行命令，全局命令将使用打印(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:p&lt;/code&gt;)命令来打印当前行的文本。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/console
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

它将在屏幕底部打印所有包含”console”的行。

顺便说一句，这是一个有趣的事实。因为全局命令使用的默认命令是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;，所以这使&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt;语法为：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/re/p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt; = 全局命令&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;re&lt;/code&gt; = 正则表达式模式&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; = 打印命令&lt;/li&gt;
&lt;/ul&gt;

这三个元素连起来拼写为 &lt;strong&gt;“grep”&lt;/strong&gt;，与命令行中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt; 相同。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g/re/p&lt;/code&gt;命令最初来自Ed编辑器。

&lt;h2 id=&quot;反转整个缓冲区&quot;&gt;反转整个缓冲区&lt;/h2&gt;

要翻转整个文件，请运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/^/m 0 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果只需要反转几行，请将其传递一个范围。要将第5行到第10行之间的行反转，请运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:5,10g/^/m 0
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;汇总所有待办事项&quot;&gt;汇总所有待办事项&lt;/h2&gt;

要将所有TODO复制到文件末尾以便于自省，请运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/TODO/t $
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果不想复制，而是将所有的 TODO 移动到末尾，可以使用移动命令 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m&lt;/code&gt;：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/TODO/m $
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;黑洞删除&quot;&gt;黑洞删除&lt;/h2&gt;

为了避免删除的行存储到寄存器中，您可以使用黑洞寄存器（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;_&lt;/code&gt;）：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/console/d _
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;将多条空行减少为一条空行&quot;&gt;将多条空行减少为一条空行&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/^$/,/./-1j
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/^$/&lt;/code&gt; 表示一个空行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/./&lt;/code&gt; 表示一个非空行。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-1&lt;/code&gt; 意思是向上偏移1行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt; 是联接命令。&lt;/li&gt;
&lt;/ul&gt;

如果想要将多个空行全部删去：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/^$/,/./j
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;高级排序&quot;&gt;高级排序&lt;/h2&gt;

如果需要排序数组中的元素，而不是数组本身，可以运行以下命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:g/\[/+1,/\]/-1sort
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/\[/+1&lt;/code&gt; 匹配左方括号”[“的下面1行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/\]/-1&lt;/code&gt; 匹配右方括号”]”的上面1行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sort&lt;/code&gt; 是命令行命令：排序。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;聪明地学习全局命令&quot;&gt;聪明地学习全局命令&lt;/h2&gt;

全局命令针对所有匹配的行执行命令行命令。有了它，您只需要运行一次命令，Vim就会为您完成其余的工作。要精通全局命令，需要做两件事：良好的命令行命令词汇表和正则表达式知识。

每当需要在多个位置应用命令时，请暂停并查看是否可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g&lt;/code&gt;命令。寻找最适合工作的命令，并编写一个模式串以同时定位多个目标。

</description>
        <pubDate>Tue, 05 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch13-the-global-command/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch13-the-global-command/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>全局命令</category>
        
        <category>global</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第十二章：搜索和替换</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

本章涵盖两个独立但相关的概念:搜索和替代。很多时候，您得基于文本的共同模式搜索大量的内容。通过学习如何在搜索和替换中使用正则表达式而不是字面字符串，您将能够快速定位任何文本。

附带说明一下，在本章中，当谈论搜索时，我将主要使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;。您使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;进行的所有操作也可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt;完成。

&lt;h2 id=&quot;智能区分大小写&quot;&gt;智能区分大小写&lt;/h2&gt;

尝试匹配搜索词的大小写可能会很棘手。如果要搜索文本”Learn Vim”，则很容易把字母的大小写输错，从而得到错误的搜索结果。如果可以匹配任何情况，会不会更轻松，更安全？这是选项&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ignorecase&lt;/code&gt;闪亮的地方。只需在 vimrc 中添加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set ignorecase&lt;/code&gt;，所有搜索词就不区分大小写。

但是，有时您需要搜索特定大小写的短语。为避免反复开关&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ignorecase&lt;/code&gt;选项，Vim 有一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;smartcase&lt;/code&gt;选项。您可以将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ignorecase&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;smartcase&lt;/code&gt;选项结合起来，当您输入的搜索词全部是小写时，进行大小写不敏感搜索；而当搜索词 &lt;strong&gt;至少有1个大写字母时&lt;/strong&gt;，进行大小写敏感搜索。

在您的 vimrc 中，添加:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;set ignorecase smartcase
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果您有这些文字:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hello
HELLO
Hello
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/hello&lt;/code&gt; 匹配”hello”，”HELLO”和”Hello”。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/HELLO&lt;/code&gt; 仅匹配”HELLO”。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/Hello&lt;/code&gt; 仅匹配”Hello”。&lt;/li&gt;
&lt;/ul&gt;

有一个缺点。如果只需要搜索小写字符串怎么办？您可以在搜索词前使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\C&lt;/code&gt;模式来告诉 Vim，后续搜索词将区分大小写。如果执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/\Chello&lt;/code&gt;，它将严格匹配”hello”，而不是”HELLO”或”Hello”。

&lt;h2 id=&quot;一行中的第一个和最后一个字符&quot;&gt;一行中的第一个和最后一个字符&lt;/h2&gt;

您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt;匹配行中的第一个字符，并使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt;匹配行中的最后一个字符。

如果您有以下文字:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hello hello
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/^hello&lt;/code&gt;来定位第一个”hello”。要定位最后一个”hello”，请运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/hello$&lt;/code&gt;。

如果您有以下文字:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hello hello friend
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/hello$&lt;/code&gt;将匹配不到任何内容，因为”friend”是该行的最后一项，而不是”hello”。

&lt;h2 id=&quot;重复搜索&quot;&gt;重复搜索&lt;/h2&gt;

您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;//&lt;/code&gt;重复上一个搜索。如果您只是搜索&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/hello&lt;/code&gt;，则运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;//&lt;/code&gt;等同于运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/hello&lt;/code&gt;。另外，回想一下前面的章节，您还可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt;分别以相同方向和相反方向重复上一次搜索。

如果您想快速回忆起第&lt;em&gt;n&lt;/em&gt;个最近使用的搜索字怎么办？您可以先按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;，然后按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;up&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;down&lt;/code&gt;方向键（或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-N&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-P&lt;/code&gt;），快速遍历搜索历史，直到找到所需的搜索词。要查看所有搜索历史，可以运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:history /&lt;/code&gt;。

在搜索过程中到达文件末尾时，Vim 会抛出一个错误。您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set wrapscan&lt;/code&gt;选项使 Vim 在到达文件末尾时回到文件顶部进行搜索。

&lt;h2 id=&quot;使用候选词搜索&quot;&gt;使用候选词搜索&lt;/h2&gt;

一次搜索多个单词属于日常操作。如果您需要搜索”hello vim”或”hola vim”，则可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&lt;/code&gt;或运算符。

给予这样一段文本:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hello vim
hola vim
salve vim
bonjour vim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;要同时匹配”hello”和”hola”，可以执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/hello\|hola&lt;/code&gt;。您必须使用（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&lt;/code&gt;）转义（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&lt;/code&gt;）或运算符，否则 Vim 将按字面意义搜索字符串”&lt;/td&gt;
      &lt;td&gt;“。&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

如果您不想每次都输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\|&lt;/code&gt;，则可以在搜索开始时使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;magic&lt;/code&gt;语法（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\v&lt;/code&gt;）:&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/\vhello|hola&lt;/code&gt;。有了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\v&lt;/code&gt;，您就不必再转义特殊字符了。

&lt;h2 id=&quot;设置模式匹配的开始位置和结束位置&quot;&gt;设置模式匹配的开始位置和结束位置&lt;/h2&gt;

也许您需要搜索的文本是复合词的一部分。如果您有这些文字:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;11vim22
vim22
11vim
vim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果您仅需要选择以”11”开始、以”22”结束的”vim”，您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\zs&lt;/code&gt;（开始匹配）和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\ze&lt;/code&gt;（结束匹配）运算符。 执行:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/11\zsvim\ze22
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim仍然会匹配整个模式”11vim22”，但是仅高亮显示介于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\zs&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\ze&lt;/code&gt;之间的内容。

&lt;h2 id=&quot;搜索字符组&quot;&gt;搜索字符组&lt;/h2&gt;

最基本的模式是字符组&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[ ]&lt;/code&gt;。

如果您需要搜索任何数字，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/[0-9]&lt;/code&gt;来匹配一位数字。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/[a-z]&lt;/code&gt;来搜索小写字母，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/[A-Z]&lt;/code&gt;来搜索大写字母。

您可以将这些范围组合在一起。如果您需要搜索数字 0-9 以及从 a 到 f（十六进制）的小写字母和大写字母，可以执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/[0-9a-fA-F]&lt;/code&gt;。

要进行否定搜索，可以在字符范围括号内添加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt;。要搜索非数字，请运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/[^0-9]&lt;/code&gt;。

&lt;h2 id=&quot;搜索重复字符&quot;&gt;搜索重复字符&lt;/h2&gt;

您可以将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt;传递给您的搜索。它具有以下语法:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{n,m}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这些&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt;周围的花括号需要被转义。这是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;count&lt;/code&gt;语法的四种不同变体:
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{n}&lt;/code&gt;是精确匹配。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/[0-9]\{2\}&lt;/code&gt;匹配两个数字。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{n,m}&lt;/code&gt;是范围匹配。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/[0-9]\{2,3\}&lt;/code&gt;匹配 2 到 3 位数字。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{,m}&lt;/code&gt;是上限匹配。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/[0-9]\{,3\}&lt;/code&gt;匹配最多 3 个数字。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{n,}&lt;/code&gt;是下限匹配。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/[0-9]\{2,\}&lt;/code&gt;匹配最少 2 个或多个数字。&lt;/li&gt;
&lt;/ul&gt;

计数参数&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\{0,\}&lt;/code&gt;（零或多个）和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\{1,\}&lt;/code&gt;（一个或多个）是最常见的搜索模式，Vim 为它们提供了特殊的操作符:&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;。

&lt;h2 id=&quot;预定义的字符组&quot;&gt;预定义的字符组&lt;/h2&gt;

Vim 为常见字符组提供了简写:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;\d    数字[0-9]
\D    非数字[^0-9]
\s    空格字符（空格和制表符）
\S    非空白字符
\w    单词字符[0-9A-Za-z_]
\l    小写字母[a-z]
\u    大写字符[A-Z]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;搜索示例在一对相似字符之间捕获文本&quot;&gt;搜索示例：在一对相似字符之间捕获文本&lt;/h2&gt;

如果要搜索由双引号引起来的短语:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot;Vim is awesome!&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

运行这个:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/&quot;[^&quot;]\+&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这是一个通用模式用于捕获 &lt;strong&gt;由一对类似的定界符包围的短语&lt;/strong&gt;。

&lt;h2 id=&quot;搜索示例捕获电话号码&quot;&gt;搜索示例：捕获电话号码&lt;/h2&gt;

如果要匹配以连字符(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&lt;/code&gt;)分隔的美国电话号码，例如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;123-456-7890&lt;/code&gt;，则可以使用:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/\v\d{3}-\d{3}-\d{4}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;基本替换&quot;&gt;基本替换&lt;/h2&gt;

Vim 的替代命令语法为:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:s/{old-pattern}/{new-pattern}/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;替换范围&quot;&gt;替换范围&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:[range]s/old/new/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:,3s/let/const/&lt;/code&gt; - 从当前行替换到第 3 行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:1,s/let/const/&lt;/code&gt; - 从第 1 行替换到当前行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:3s/let/const/&lt;/code&gt; - 仅在第3行进行替换。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:%s/let/const/&lt;/code&gt; - 在所有行上进行替换。&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;模式匹配&quot;&gt;模式匹配&lt;/h2&gt;

要交换”let”和变量名位置:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:%s/\v(\w+) (\w+)/\2 \1/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\1&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\3&lt;/code&gt;等代表捕获的分组。

&lt;h2 id=&quot;替换标志&quot;&gt;替换标志&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;g    替换行中的所有匹配项。
c    要求替代确认。
e    防止替换失败时显示错误消息。
i    执行不区分大小写的替换
I    执行区分大小写的替换
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;特殊替换&quot;&gt;特殊替换&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;\U    将后面跟着的字符变为大写
\u    将后面跟着的第一个字符变为大写
\L    将后面跟着的字符变为小写
\l    将后面跟着的第一个字符变为小写
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;贪婪与非贪婪&quot;&gt;贪婪与非贪婪&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;表示贪婪匹配，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{-}&lt;/code&gt;表示非贪婪匹配。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:s/a.*1/\U&amp;amp;/g     &quot; 贪婪模式，得到 ABC1DE1
:s/a.\{-}1/\U&amp;amp;/g  &quot; 非贪婪模式，得到 ABC1de1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;跨多个文件替换&quot;&gt;跨多个文件替换&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:args *.txt
:argdo %s/dog/chicken/
:argdo update
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;用宏跨多个文件替换&quot;&gt;用宏跨多个文件替换&lt;/h2&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:args *.txt
qq
:%s/dog/chicken/g
:wnext
q
99@q
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;以聪明的方式学习搜索和替换&quot;&gt;以聪明的方式学习搜索和替换&lt;/h2&gt;

良好的搜索能力是编辑的必要技能。掌握搜索功能使您可以利用正则表达式的灵活性来搜索文件中的任何模式。花些时间学习这些。要想掌握正则表达式，您必须在实践中去不断地使用它。

</description>
        <pubDate>Mon, 04 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch12-search-and-substitute/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch12-search-and-substitute/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>搜索</category>
        
        <category>替换</category>
        
        <category>正则表达式</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Nova v1.0.0 正式发布</title>
        <description>Nova v1.0.0 正式发布，chat.nvim 的 Android 客户端迎来首个稳定版本。</description>
        <pubDate>Sun, 03 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/nova-v1-0-0-release/</link>
        <guid isPermaLink="true">https://wsdjeg.net/nova-v1-0-0-release/</guid>
        
        <category>android</category>
        
        <category>neovim</category>
        
        <category>chat.nvim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第十一章：可视模式</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

高亮显示文本块并对其进行更改，是很多文本编辑器中的常见功能。 Vim也可以使用可视模式实现这一功能。在本章中，您将学习如何使用可视模式来有效地处理文本块。

&lt;h2 id=&quot;三种可视模式&quot;&gt;三种可视模式&lt;/h2&gt;

Vim有三种可视模式，分别是：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;v         逐字符可视模式
V         逐行可视模式
Ctrl-v    逐块可视模式
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果您有下列文字：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
two
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

逐字符可视模式用于选择单个字符。在第一行的第一个字符上按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;。然后使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt;跳转至下一行。它高亮显示从”one”到光标位置的所有文本。现在，如果您按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gU&lt;/code&gt;，Vim将高亮显示的字符转为大写。

逐行可视模式适用于整行。按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;并观看Vim选择光标的所在行。就像逐字符可视模式一样，如果您运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gU&lt;/code&gt;，Vim将高亮显示的字符转为大写。

逐块可视模式适用于行和列。与其他两种模式相比，它为您提供了更大的移动自由度。按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-V&lt;/code&gt;，Vim像逐字符可视模式一样高亮显示光标下的字符，但向下移动时，除非光标已经在行尾，否则不会高亮显示光标上方的整行，它跳转至下一行时高亮显示尽可能少的字符。尝试用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h/j/k/l&lt;/code&gt;移动，并观察光标的移动。

在Vim窗口的左下方，您会看到显示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-- VISUAL --&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-- VISUAL LINE --&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-- VISUAL BLOCK --&lt;/code&gt;以提示您所处的可视模式。

当您处于可视模式时，可以通过按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-V&lt;/code&gt;键切换到另一种可视模式。例如，如果您处于逐行可视模式，并且想要切换为逐块可视模式，请运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-V&lt;/code&gt;。试试吧！

有三种退出可视模式的方法：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-C&lt;/code&gt;和与当前可视模式相同的键。后者的意思是，如果您当前处于逐行可视模式(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;)，则可以通过再次按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;退出它。如果您处于字符可视模式，则可以通过按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;退出它。如果您处于逐块可视模式，请按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-V&lt;/code&gt;。

实际上，还有另一种进入可视模式的方式：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gv    转到上一个可视模式
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

它将在与上次相同的高亮显示的文本块上启动相同的可视模式。

&lt;h2 id=&quot;可视模式导航&quot;&gt;可视模式导航&lt;/h2&gt;

在可视模式下，您可以使用Vim动作(motion)扩展高亮显示的文本块。

让我们使用之前使用的相同文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
two
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这次让我们从”two”行开始。按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;进入字符可视模式(这里的方括号&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;[]&lt;/code&gt;表示高亮显示的字符)：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
[t]wo
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt;，Vim将高亮显示从”two”行到”three”行的第一个字符的所有文本。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
[two
t]hree
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

假设您刚刚意识到还需要高亮显示”one”行，因此按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt;。令您沮丧的是，它现在排除了”three”高亮。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
[t]wo
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

有没有一种方法可以自由地扩展视觉选择范围，以向您想要的任何方向发展？

答案是肯定的。高亮区域跟随光标移动。如果要将其向上扩展到行”one”，则需要将光标移动到”two”，现在您的光标在”three”行上。这时可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;o&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O&lt;/code&gt;切换光标位置。

在可视模式中使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;o&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O&lt;/code&gt;，光标会在高亮选择区域的开头和结尾跳转，以便与您扩展高亮区域。

&lt;h2 id=&quot;可视模式语法&quot;&gt;可视模式语法&lt;/h2&gt;

可视模式与普通模式使用相同的操作符(operations)。

例如，如果您有以下文字，然后您想在可视模式中删除前两行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
two
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

用逐行可视模式(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;)高亮显示”one”和”two”行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[one
two]
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt;键将删除选择，类似于普通模式。请注意，与普通模式的语法规则有所不同，动词+名词不适用可视模式。虽然存在相同的动词(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt;)，但在可视模式下没有名词。可视模式下的语法规则是名词+动词(反过来了)，其中名词是高亮显示的文本。首先选择文本块，然后进行操作。

在普通模式下，有一些命令不需要名词(motion)，例如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;删除光标下方的单个字符，还有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r&lt;/code&gt;替换光标下方的字符(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rx&lt;/code&gt;将当前光标下的字符替换为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;)。在可视模式下，这些命令现在将应用于整个高亮显示的文本，而不是单个字符。回到高亮显示的文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[one
two]
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;会删除所有高亮显示的文本。

您可以使用此行为在markdown文本中快速创建标题。假设您需要快速下面的文本转换为一级markdown标题(“===”):

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Chapter One
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

首先，您使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yy&lt;/code&gt;复制文本，然后使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;粘贴文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Chapter One
Chapter One
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

现在转到第二行，以逐行可视模式选择它：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Chapter One
[Chapter One]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

在markdown中，您可以通过在文本下方添加一系列&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;来创建标题，因此您可以通过运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;r=&lt;/code&gt;来替换整个高亮显示的文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Chapter One
===========
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

要了解有关可视模式下的运算符的更多信息，请查看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h visual-operators&lt;/code&gt;。

&lt;h2 id=&quot;可视模式和ex命令&quot;&gt;可视模式和Ex命令&lt;/h2&gt;

您可以有选择地在高亮显示的文本块上应用Ex命令。如果您具有以下表达式,并想将前两行的”const”替换为”let”：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const one = &quot;one&quot;;
const two = &quot;two&quot;;
const three = &quot;three&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

用 &lt;em&gt;任意&lt;/em&gt; 可视模式高亮显示前两行，然后运行替换命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:s/const/let/g&lt;/code&gt;：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let one = &quot;one&quot;;
let two = &quot;two&quot;;
const three = &quot;three&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

请注意，我说过您可以使用 &lt;em&gt;任何&lt;/em&gt; 可视模式执行此操作。您不必高亮显示整个行即可在该行上运行Ex命令。只要您在每行上至少选择一个字符，就会应用Ex命令。

&lt;h2 id=&quot;跨多行编辑&quot;&gt;跨多行编辑&lt;/h2&gt;

您可以使用逐块可视模式在Vim中跨多行编辑文本。如果需要在每行末尾添加分号：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const one = &quot;one&quot;
const two = &quot;two&quot;
const three = &quot;three&quot;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

将光标放在第一行上：
&lt;ul&gt;
  &lt;li&gt;进入逐块可视模式，并向下两行(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-V jj&lt;/code&gt;)。&lt;/li&gt;
  &lt;li&gt;高亮显示到行尾(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt;)。&lt;/li&gt;
  &lt;li&gt;按(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;) ，然后键入”;”。&lt;/li&gt;
  &lt;li&gt;退出可视模式(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt;)。&lt;/li&gt;
&lt;/ul&gt;

您应该看到在每一行后面附加的 “;”。有两种方法可以从逐块可视模式进入输入模式：可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;在光标后输入文本，也可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I&lt;/code&gt;在光标前输入文本。请勿将它们与普通模式下的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;I&lt;/code&gt;混淆。

另外，您也可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:normal&lt;/code&gt;命令在多行添加内容：
&lt;ul&gt;
  &lt;li&gt;高亮显示所有3行(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vjj&lt;/code&gt;)。&lt;/li&gt;
  &lt;li&gt;输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:normal! A;&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

记住，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:normal&lt;/code&gt;命令执行普通模式命令。您可以指示它运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A;&lt;/code&gt;在该行的末尾添加文本”;”。

&lt;h2 id=&quot;递增数字&quot;&gt;递增数字&lt;/h2&gt;

Vim有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-X&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-A&lt;/code&gt;命令来减少和增加数字。与可视模式一起使用时，可以跨多行递增数字。

如果您具有以下HTML元素：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;div id=&quot;app-1&quot;&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id=&quot;app-1&quot;&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id=&quot;app-1&quot;&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id=&quot;app-1&quot;&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id=&quot;app-1&quot;&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

有多个具有相同名称的id是一个不好的做法，因此让我们对其进行递增以使其唯一：
&lt;ul&gt;
  &lt;li&gt;将光标移动到 &lt;strong&gt;第二行&lt;/strong&gt;的 “1”。&lt;/li&gt;
  &lt;li&gt;启动逐块可视模式，并向下移动3行(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-V 3j&lt;/code&gt;)。这高亮显示剩余的”1”，现在除了第一行，所有的”1”应该已经高亮。&lt;/li&gt;
  &lt;li&gt;运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g Ctrl-A&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

您应该看到以下结果：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;div id=&quot;app-1&quot;&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id=&quot;app-2&quot;&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id=&quot;app-3&quot;&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id=&quot;app-4&quot;&amp;gt;&amp;lt;/div&amp;gt;
&amp;lt;div id=&quot;app-5&quot;&amp;gt;&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g Ctrl-A&lt;/code&gt;在多行上递增数字。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-X/Ctrl-A&lt;/code&gt;也可以增加字母。如果您运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set nrformats+=alpha
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nrformats&lt;/code&gt;选项指示Vim将哪个基数视为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-A&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-X&lt;/code&gt;递增和递减的”数字”。通过添加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;alpha&lt;/code&gt;，现在将字母字符视为数字。

&lt;h2 id=&quot;选择最后一个可视模式区域&quot;&gt;选择最后一个可视模式区域&lt;/h2&gt;

前面章节中我提到了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gv&lt;/code&gt;可以快速高亮显示上一个可视模式选择的内容。您还可以使用以下两个特殊标记转到最后一个可视模式的开始和结束位置：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&apos;&amp;lt;    转到上一个可视模式高亮显示的第一个位置（行）
&apos;&amp;gt;    转到上一个可视模式高亮显示的最后位置（行）
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

之前，我提到过您可以在高亮显示的文本上有选择地执行Ex命令，例如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:s/const/let/g&lt;/code&gt;。当您这样做时，您应该看到以下内容：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:&apos;&amp;lt;,&apos;&amp;gt;s/const/let/g
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您实际上是在使用(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;&amp;lt;, &apos;&amp;gt;&lt;/code&gt;) 标记作为范围来执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s/const/let/g&lt;/code&gt;命令。

您随时可以随时编辑这些标记。比如，如果您需要从高亮显示的文本的开头到文件的末尾进行替换，则只需将命令行更改为：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:&apos;&amp;lt;,$s/const/let/g
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;从插入模式进入可视模式&quot;&gt;从插入模式进入可视模式&lt;/h2&gt;

您也可以从插入模式进入可视模式。在插入模式下进入字符可视模式：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-O v
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

回想一下，在插入模式下运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-O&lt;/code&gt;可以使您执行普通模式命令。在普通模式命令挂起模式下，运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;进入逐字可视模式。请注意，在屏幕的左下方，它显示为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;--(insert) VISUAL--&lt;/code&gt;。该技巧适用于任何可视模式运算符：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;v&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;，和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-V&lt;/code&gt;。

&lt;h2 id=&quot;选择模式&quot;&gt;选择模式&lt;/h2&gt;

Vim具有类似于可视模式的模式，称为&lt;em&gt;选择模式&lt;/em&gt;。与可视模式一样，它也具有三种不同的模式：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gh         逐字符选择模式
gH         逐行选择模式
gCtrl-h    逐块选择模式
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

选择模式比Vim的可视模式更接近常规编辑器的文本高亮显示行为。

在常规编辑器中，高亮显示文本块并键入字母（例如字母”y”）后，它将删除高亮显示的文本并插入字母”y”。如果您使用逐行选择模式(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gH&lt;/code&gt;)高亮显示一行文本并键入”y”，它将删除高亮显示的文本并插入字母”y”，这与常规文本编辑器非常相似。

将此行为与可视模式进行对比：如果您使用逐行可视模式(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;V&lt;/code&gt;)高亮显示一行文本并键入”y”，则高亮显示的文本不会被删除且被字母”y”代替，而是仅将其复制(yank)。在选择模式中，你不能执行对高亮文本执行普通模式的命令。

我个人从未使用过选择模式，但是很高兴知道它的存在。

&lt;h2 id=&quot;以聪明的方式学习可视模式&quot;&gt;以聪明的方式学习可视模式&lt;/h2&gt;

可视模式是Vim高亮显示文本的过程。

如果发现使用可视模式操作的频率比正常模式操作的频率高得多，请当心。我认为这是一种反模式。运行可视模式操作所需的击键次数要多于普通模式下的击键次数。假设您需要删除一个内部单词(inner word)，如果可以只用三个按键(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diw&lt;/code&gt;)，为什么要使用四个按键&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;viwd&lt;/code&gt;呢？前者更为直接和简洁。当然，有时使用可视模式是合适的，但总的来说，更倾向于直接的方法。

</description>
        <pubDate>Sun, 03 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch11-visual-mode/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch11-visual-mode/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>可视模式</category>
        
        <category>visual</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第十章：撤销</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

所有人都会犯各种各样的输入错误。因此对于任何一个现代的软件来说，撤销都是一个很基本的功能。 Vim 的撤销系统不仅支持撤销和取消撤销任何修改，而且支持存取不同的文本形态，让你能控制你输入的所有文本。在本章中，你将会学会如何执行撤销和 取消撤销文本，浏览撤销分支，反复撤销, 以及浏览改动时间线。

&lt;h2 id=&quot;撤销undo重做和行撤销undo&quot;&gt;撤销(undo)，重做和行撤销(UNDO)&lt;/h2&gt;

对于一个基本的 undo 操作，你可以执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; 或者 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:undo&lt;/code&gt;。

假设你有如下文本(注意”one”下面有一个空行)：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

然后添加另一个文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
two
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果你执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，Vim 会删除 “two”。

Vim 是如何知道应该恢复多少修改呢？ 答案是，Vim每次仅恢复一次修改，这有点类似于点命令的操作（和 点命令不同之处在于，命令行命令也会被算作一次修改）。

要取消上一次的撤销，可以执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r&lt;/code&gt; 或者 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:redo&lt;/code&gt;。例如上面的例子中，当你执行撤销来删除 “two” 以后，你可以执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r&lt;/code&gt; 来恢复被删除掉的文本。

Vim 也有另一个命令 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; 可以实现 行撤销 (UNDO) 的功能，执行这个命令会撤销所有最新的修改。

那么，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; 的区别是什么呢？首先，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; 会删除 &lt;em&gt;最近修改的行中所有的&lt;/em&gt; 的修改，而 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; 一次仅删除一次修改。 其次，执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; 不会被算作一次修改操作，而执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; 则会被算作一次修改。

让我们回到之前的例子：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
two
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

修改第二行的内容为 “three” (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ciwthree&amp;lt;esc&amp;gt;&lt;/code&gt;):

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

再次修改第二行的例子为 “four” (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ciwfour&amp;lt;esc&amp;gt;&lt;/code&gt;):

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
four
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

此时，如果你按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，你会看到 “three”。如果你再次按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，你会看到 “two”。然而，在第二行仍为 “four” 的时候，如果你按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt;，你会看到

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; 会跳过中间所有修改，直接恢复到文件最初的状态（第二行为空）。另外，由于 UNDO 实际上是执行了一个新的修改，因此你可以 UNDO 执行过的 UNDO。 执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; 后 再次执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt; 会撤销 自己。假如你连续执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt;，那么你将看到第二行的文本不停地出现和消失。

就我个人而言，我几乎不会使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;U&lt;/code&gt;，因为很难记住文本最初的样子。

Vim 可以通过变量 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undolevels&lt;/code&gt; 来选择最多可执行 undo 的次数。你可以通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:echo &amp;amp;undolevels&lt;/code&gt; 来查看当前的配置。我一般设置为 1000。如果你也想设置为 1000 的话，你可以执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:set undolevels=1000&lt;/code&gt;。

&lt;h2 id=&quot;断点插入操作&quot;&gt;断点插入操作&lt;/h2&gt;

在上文中我提到，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; 每次恢复一个修改，类似于点命令。在每次进入 插入模式和退出插入模式之间的任何修改都被定义为一次修改。

如果你执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ione two three&amp;lt;esc&amp;gt;&lt;/code&gt; 之后，按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，Vim 会同时删除 “one two three”，因为这是一笔修改。如果你每次只输入较短的文本，那这是可接受的；可假设你在一次插入模式中输入了大量的文本，而后退出了插入模式，可很快你意识到这中间有部分错误。此时，如果你按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，你会丢失上一次输入的所有内容。 因此，假设你按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; 只删除你上一次输入的一部分文本岂不是会更好。

幸运的是，你可以拆分它。当你在插入模式时，按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-G u&lt;/code&gt; 会生成一个断点。例如，如果你执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ione &amp;lt;Ctrl-G u&amp;gt;two &amp;lt;Ctrl-G u&amp;gt;three&amp;lt;esc&amp;gt;&lt;/code&gt;，之后你按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，你仅会失去文本 “three”，再次执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，会删除 “two”。当你想要输入一长段内容时，应该有选择性地执行断点插入操作。在每一句话的末尾，两个段落的中间，或者每一行代码结束时插入断点是一个很好的选择，这可以帮助你快速从错误中恢复出来。

在插入模式中，执行删除操作时插入断点也非常有用。例如通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-W&lt;/code&gt; 删除光标前的单词时，以及 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-U&lt;/code&gt;删除光标前的所有文本时。一个朋友建议我使用如下的映射：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;inoremap &amp;lt;c-u&amp;gt; &amp;lt;c-g&amp;gt;u&amp;lt;c-u&amp;gt;
inoremap &amp;lt;c-w&amp;gt; &amp;lt;c-g&amp;gt;u&amp;lt;c-w&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

通过上述命令，你可以很轻松地恢复被删除的文本。

&lt;h2 id=&quot;撤销树&quot;&gt;撤销树&lt;/h2&gt;

Vim 将每一次修改存储在一个撤销树中。你打开一个空白文件，然后添加一段新文本:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

再插入一段新文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
two
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

undo一次:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

插入一段不同的话：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

再次 undo

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

再次插入另一段话：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
four
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

现在如果你执行 undo,您将丢失刚刚添加的文本 “four” :

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果你再次执行 undo 操作：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
文本 “one” 也会丢失。对于大部分编辑器来说，找回文本 “two” 和 “three” 都是不可能的事情，但是对于 Vim 来说却不是这样。执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g+&lt;/code&gt;，你会得到：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

再次执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g+&lt;/code&gt; ，你将会看到一位老朋友:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
two
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

让我们继续执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g+&lt;/code&gt;:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

再一次执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g+&lt;/code&gt; :

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
four
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

在 Vim 中，你每一次执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; 去做一次修改时，Vim都会通过创建一个”撤销分支”来保存之前的文本内容。在本例中，你输入”two”后, 执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，然后输入”three”，你就创建了一个叶子分支,保存了含有”two”的文本状态。此时，撤销树已经包含了至少两个叶子节点，主节点包含文本”three”（最新），而另一undo分支节点包含文本”two”。假如你执行了另一次撤销操作并且输入了”four”，那么此时会生成三个节点，一个主节点包含文本”four”, 以及另外两个节点分别存储了”three”和”two”。

为了在几个不同的节点状态间进行切换，你可以执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g+&lt;/code&gt; 去获取一个较新的状态，以及执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g-&lt;/code&gt; 去获取一个教旧的状态。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;， &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-R&lt;/code&gt;， &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g+&lt;/code&gt;， 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g-&lt;/code&gt; 之间的区别是，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-R&lt;/code&gt; 只可以在 &lt;em&gt;main&lt;/em&gt; 节点之间进行切换，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g+&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g-&lt;/code&gt; 可以在 &lt;em&gt;所有&lt;/em&gt; 节点之间进行切换。

Undo 树并不可以很轻松地可视化。我发现一个插件 &lt;a href=&quot;https://github.com/simnalamburt/vim-mundo&quot;&gt;vim-mundo&lt;/a&gt; 对于理解 undo 树很有帮助。花点时间去与它玩耍吧。

&lt;h2 id=&quot;保存撤销记录&quot;&gt;保存撤销记录&lt;/h2&gt;

当你通过 Vim 打开一个文件，并且立即按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，Vim 很可能会显示 “&lt;em&gt;Already at oldest change&lt;/em&gt;” 的警告。

要想从最近的一次编辑工作中（在vim关闭文件再打开，算做一次新的编辑工作），在撤销历史中回滚，可以通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:wundo&lt;/code&gt;命令使Vim 保存一份你的 undo 历史记录。

创建一个文件 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mynumbers.txt&lt;/code&gt;. 输入:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

插入另一行文件 (确保你要么退出并重新进入插入模式，要么创建了断点):

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
two
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

插入新的一行:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
two
three
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

现在，创建你的撤销记录文件。 语法为 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:wundo myundofile&lt;/code&gt;。 如果你需要覆盖一个已存在的文件，在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wundo&lt;/code&gt; 之后添加 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;.

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:wundo! mynumbers.undo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

退出 Vim。

此时，在目录下，应该有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mynumbers.txt&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mynumbers.undo&lt;/code&gt; 两个文件。再次打开 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mynumbers.txt&lt;/code&gt; 文件并且按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，这是没有响应的。因为自打开文件后，你没有执行任何的修改。现在，通过执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:rundo&lt;/code&gt; 来加载 undo 历史。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:rundo mynumbers.undo
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

此时，如果你按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;，Vim 会删除 “three”。再次按下 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt;可以删除 “two”。这就好像你从来没有关闭过 Vim 一样。

如果你想要自动加载 undo 历史文件，你可以通过在你的 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.vimrc&lt;/code&gt; 文件中添加如下代码：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;set undodir=~/.vim/undo_dir
set undofile
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我认为将所有的 undo 文件集中保存在一个文件夹中最好，例如在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vim&lt;/code&gt; 目录下。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undo_dir&lt;/code&gt; 是随意的。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set undofile&lt;/code&gt; 告诉 Vim 打开 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undofile&lt;/code&gt; 这个特性，因为该特性默认是关闭的。现在，无论你何时保存，Vim 都会自动创建和保存撤销的历史记录（在使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undo_dir&lt;/code&gt;目录前，请确保你已经创建了它）。

&lt;h2 id=&quot;时间旅行&quot;&gt;时间旅行&lt;/h2&gt;

是谁说时间旅行不存在。 Vim 可以通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:earlier&lt;/code&gt; 命令将文本恢复为之前的状态。

假如有如下文本:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
之后你输入了另一行:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;one
two
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果你输入 “two” 的时间少于10秒，那么你可以通过如下命令恢复到 “two” 还没被输入前的状态:
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:earlier 10s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:undolist&lt;/code&gt; 去查看之前所做的修改。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:earlier&lt;/code&gt; 可以加上分钟 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m&lt;/code&gt;), 小时 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h&lt;/code&gt;), and 天 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt;) 作为参数。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:earlier 10s    恢复到10秒前的状态
:earlier 10m    恢复到10分钟前的状态
:earlier 10h    恢复到10小时前的状态
:earlier 10d    恢复到10天前的状态

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

另外，它同样接受一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;计数&lt;/code&gt;作为参数，告诉vim恢复到老状态的次数。比如，如果运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:earlier 2&lt;/code&gt;,Vim将恢复到2次修改前的状态。功能类似于执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g-&lt;/code&gt;两次。同样，你可以运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:earlier 10f&lt;/code&gt;命令告诉vim恢复到10次保存前的状态。

这些参数同样作用于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:earlier&lt;/code&gt;命令的对应版本:&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:later&lt;/code&gt;。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:later 10s    恢复到10秒后的状态
:later 10m    恢复到10分钟后的状
:later 10h    恢复到10小时后的状
:later 10d    恢复到10天后的状态
:later 10     恢复到新状态10次
:later 10f    恢复到10次保存后的状态
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;聪明地学习撤销操作&quot;&gt;聪明地学习撤销操作&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;u&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-R&lt;/code&gt; 是两个不可缺少的 Vim 参数。请先学会它们。在我的工作流中，我并不使用 UNDO，然而我认为承认它存在是很好的。下一步，学会如何使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:earlier&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:later&lt;/code&gt;，以及时间参数。在这之后，请花些时间理解 undo 树。 插件 &lt;a href=&quot;https://github.com/simnalamburt/vim-mundo&quot;&gt;vim-mundo&lt;/a&gt; 对我的帮助很大。单独输入本章中展示的文本，并且查看撤销树的每一次改变。一旦你掌握它，你看待撤销系统的眼光一定不同。

在本章之前，你学习了如何在项目内查找任何文本，配合撤销，你可以在时间维度上查找任何一个文本。你现在可以通过位置和写入时间找到任何一个你想找的文本。你已经对 Vim 无所不能了。

</description>
        <pubDate>Sat, 02 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch10-undo/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch10-undo/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>撤销</category>
        
        <category>undo</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第九章：宏命令</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

在编辑文件的时候，你会发现有时候你在反复地做一些相同的动作。如果你仅做一次，并在需要的时候调用这些动作岂不是会更好吗。通过 Vim 的宏命令，你可以将一些动作记录到 Vim 寄存器。

在本章中，你将会学习到如何通过宏命令自动完成一些普通的任务（另外，看你的文件在自动编辑是一件很酷的事情）。

&lt;h2 id=&quot;基本宏命令&quot;&gt;基本宏命令&lt;/h2&gt;

宏命令的基本语法如下：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;qa                     开始记录动作到寄存器 a
q (while recording)    停止记录
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你可以使用小写字母 （a-z）去存储宏命令。并通过如下的命令去调用：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;@a    Execute macro from register a
@@    Execute the last executed macros
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

假设你有如下的文本，你打算将每一行中的所有字母都变为大写。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;hello
vim
macros
are
awesome
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

将你的光标移动到 “hello” 栏的行首，并执行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;qa0gU$jq
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

上面命令的分解如下：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qa&lt;/code&gt; 开始记录一个宏定义并存储在 a 寄存器。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; 移动到行首。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gU$&lt;/code&gt; 将从光标到行尾的字母变为大写。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt; 移动到下一行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; 停止记录。&lt;/li&gt;
&lt;/ul&gt;

调用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt; 去执行该宏命令。就像其他的宏命令一样，你也可以为该命令加一个计数。例如，你可以通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3@a&lt;/code&gt; 去执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; 命令3次。你也可以执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3@@&lt;/code&gt; 去执行上一次执行过的宏命令3次。

&lt;h2 id=&quot;安全保护&quot;&gt;安全保护&lt;/h2&gt;

在执行遇到错误的时候，宏命令会自动停止。假如你有如下文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a. chocolate donut
b. mochi donut
c. powdered sugar donut
d. plain donut
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你想将每一行的第一个词变为大写，你可以使用如下的宏命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;qa0W~jq
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

上面命令的分解如下：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qa&lt;/code&gt; 开始记录一个宏定义并存储在 a 寄存器。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; 移动到行首。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;W&lt;/code&gt; 移动到下一个单词。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt; 将光标选中的单词变为大写。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt; 移动到下一行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; 停止记录。&lt;/li&gt;
&lt;/ul&gt;

我喜欢对宏命令进行超过所需次数的调用，所以我通常使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;99@a&lt;/code&gt; 命令去执行该宏命令99次。使用该命令，Vim并不会真正执行这个宏99次，当 Vim 到达最后一行执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt;时，它会发现无法再向下了，然后会抛出一个错误，并终止宏命令的执行。

实际上，遇到错误自动停止运行是一个很好的特性。否则，Vim 会继续执行该命令99次，尽管它已经执行到最后一行了。

&lt;h2 id=&quot;命令行执行宏&quot;&gt;命令行执行宏&lt;/h2&gt;

在正常模式执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt; 并不是宏命令调用的唯一方式。你也可以在命令行执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;：normal @a&lt;/code&gt; 。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;：normal&lt;/code&gt; 会将任何用户添加的参数作为命令去执行。例如添加 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt;，和在 normal mode 执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt; 的效果是一样的。

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:normal&lt;/code&gt; 命令也支持范围参数。你可以在选择的范围内去执行宏命令。如果你只想在第二行和第三行执行宏命令，你可以执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;：2,3 normal @a&lt;/code&gt;。

&lt;h2 id=&quot;在多个文件中执行宏命令&quot;&gt;在多个文件中执行宏命令&lt;/h2&gt;

假如你有多个 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.txt&lt;/code&gt; 文件，每一个文件包含不同的内容。并且你只想将包含有 “donut” 单词的行的第一个单词变为大写。假设，您的寄存器a中存储的内容是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0W~j&lt;/code&gt;(就是前面例子中用到的宏命令),那么，您该如何快速完成这个操作呢？

第一个文件:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## savory.txt
a. cheddar jalapeno donut
b. mac n cheese donut
c. fried dumpling
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

第二个文件:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## sweet.txt
a. chocolate donut
b. chocolate pancake
c. powdered sugar donut
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

第三个文件:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;## plain.txt
a. wheat bread
b. plain donut
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你可以这么做:
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:args *.txt&lt;/code&gt; 查找当前目录下的所有 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.txt&lt;/code&gt; 文件。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:argdo g/donut/normal @a&lt;/code&gt; 在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:args&lt;/code&gt; 中包含的每一个文件里执行一个全局命令 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g/donut/normal @a&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:argdo update&lt;/code&gt; 在 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:args&lt;/code&gt; 中包含的每一个文件里执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;update&lt;/code&gt; 命令,保存修改后的内容。&lt;/li&gt;
&lt;/ul&gt;

也许你对全局命令 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:g/donut/normal @a&lt;/code&gt; 不是很了解，该命令会执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/donut/&lt;/code&gt;搜索命令，然后在所有匹配的行中执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;normal @a&lt;/code&gt; 命令。我会在后面的章节中介绍全局命令。

&lt;h2 id=&quot;递归执行宏命令&quot;&gt;递归执行宏命令&lt;/h2&gt;

你可以递归地执行宏命令，通过在记录宏命令时调用相同的宏寄存器来实现。假如你有如下文本，你希望改变第一个单词的大小写：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a. chocolate donut
b. mochi donut
c. powdered sugar donut
d. plain donut
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如下命令会递归地执行:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;qaqqa0W~j@aq
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

上面命令的分解如下：

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qaq&lt;/code&gt; 记录一个空白的宏命令到 “a” 。把宏命令记录在一个空白的命令中是必须的，因为你不会想将该命令包含有任何其他的东西。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qa&lt;/code&gt; 开始录入宏命令到寄存器 “a”。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; 移动到行首。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;W&lt;/code&gt; 移动到下一个单词。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt; 改变光标选中的单词的大小写。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt; 移动到下一行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt; 执行宏命令 “a”。当你记录该宏命令时，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt; 应该是空白的，因为你刚刚调用了 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qaq&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; 停止记录。&lt;/li&gt;
&lt;/ul&gt;

现在，让我们调用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt; 来查看 Vim 如何递归的调用该宏命令。

宏命令是如何知道何时停止呢？当宏执行到最后一行并尝试 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt; 命令时，发现已经没有下一行了，就会停止执行。

&lt;h2 id=&quot;增添一个已知宏&quot;&gt;增添一个已知宏&lt;/h2&gt;

如果你想在一个已经录制好的宏定义中添加更多的操作，与其重新录入它，不如选择修改它。在寄存器一章中，你学习了如何使用一个已知寄存器的大写字母来想该寄存器中添加内容。同样的，为了在寄存器”a”中添加更多的操作，你也可以使用大写字母”A”。

假设寄存器a中已经存储了这个宏命令:&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qa0W~q&lt;/code&gt;(该宏命令将某行的第二个词组的头一个字母执行改变大小写操作)，假设你想在这个基础上添加一些操作命令序列，使得每一行末尾添加一个句点，运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;qAA.&amp;lt;esc&amp;gt;q
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

分解如下:
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qA&lt;/code&gt; 开始在寄存器 “A” 中记录宏命令。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A.&amp;lt;esc&amp;gt;&lt;/code&gt; 在行的末尾加上一个句点(这里的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;是进入插入模式，不要和宏A搞混淆)，然后退出插入模式。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; 停止记录宏命令。&lt;/li&gt;
&lt;/ul&gt;

现在，当你执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt;时，它不仅将第二个词组的首字母转变大小写，同时还在行尾添加一个句点。

&lt;h2 id=&quot;修改一个已知宏&quot;&gt;修改一个已知宏&lt;/h2&gt;

如果想在一个宏的中间添加新的操作该怎么办呢？

假设您在寄存器a中已经存有一个宏命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0W~A.&amp;lt;Esc&amp;gt;&lt;/code&gt;，即改变首字母大小写，并在行尾添加句号。如果您想在改变首字母大小写和行尾添加句号之间，在单词”dount”前面加入”deep fried”。（因为唯一比甜甜圈好的东西就是炸甜甜圈）。

我会重新使用上一节使用过的文本:
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a. chocolate donut
b. mochi donut
c. powdered sugar donut
d. plain donut
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

首先，让我们通过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:put a&lt;/code&gt; 调用一个已经录制好的宏命令（假设你上一节中保存在寄存器a中的宏命令还在）：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0W~A.^[
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^[&lt;/code&gt; 是什么意思呢？不记得了吗，你之前执行过 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0W~A.&amp;lt;esc&amp;gt;&lt;/code&gt;。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^[&lt;/code&gt; 是 Vim 的 &lt;strong&gt;内部指令&lt;/strong&gt;，表示 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;esc&amp;gt;&lt;/code&gt;。通过这些指定的特殊键值组合，Vim 知道这些是内部代码的一些替代。一些常见的内部指令具有类似的替代，例如 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;esc&amp;gt;&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;backspace&amp;gt;&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;enter&amp;gt;&lt;/code&gt;。还有一些其他的键值组合，但这不是本章的内容。

回到宏命令，在改变大小写之后的键后面（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~&lt;/code&gt;），让我们添加（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt;）来移动光标到行末，回退一个单词（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;），进入插入模式（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt;），输入”deep fried “ （别忽略”fried “后面的这个空格），之后退出插入模式（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;esc&amp;gt;&lt;/code&gt;）。

完整的命令如下:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0W~$bideep fried &amp;lt;esc&amp;gt;A.^[
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这里有一个问题，Vim 不能理解 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;esc&amp;gt;&lt;/code&gt;。您不能依葫芦画瓢输入”&lt;Esc&gt;&quot;，所以你需要将`&lt;Esc&gt;`写成内部代码的形式。在插入模式，在按下`Ctrl-v`后按下`&lt;esc&gt;`，Vim 会打印 `^[`。 `Ctrl-v` 是一个插入模式的操作符，可以逐字地插入一个非数字字符。你的宏命令应该如下:&lt;/esc&gt;&lt;/Esc&gt;&lt;/Esc&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0W~$bideep fried ^[A.^[
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

为了在寄存器”a”中添加修改后的指令，你可以通过在一个已知命名寄存器中添加一个新条目的方式来实现。在一行的行首，执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;ay$&lt;/code&gt;，使用寄存器 “a”来存储复制的文本。

现在，但你执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt; 时，你的宏命令会自动改变第一个单词的大小写，在”donut”前面添加”deep fried “，之后在行末添加”.”。

另一个修改宏命令的方式是通过命令行表达式。执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:let @a=&quot;&lt;/code&gt;，之后执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r a&lt;/code&gt;，这会将寄存器”a”的命令逐字打印出来。最后，别忘记在闭合的引号（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&lt;/code&gt;）。如果你希望在编辑命令行表达式时插入内部码来使用特定的字符，你可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-v&lt;/code&gt;。

&lt;h2 id=&quot;拷贝宏&quot;&gt;拷贝宏&lt;/h2&gt;

你可以很轻松的将一个寄存器的内容拷贝到另一个寄存器。例如，你可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:let @z = @a&lt;/code&gt; 将寄存器”a” 中的命令拷贝到寄存器”z”。 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt; 表示寄存器”a”中存储的内容，你现在执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@z&lt;/code&gt;，将会执行和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt; 一样的指令。

我发现对常用的宏命令创建冗余是很有用的。在我的工作流程中，我通常在前7个字母（a-g）上创建宏命令，并且我经常不加思索地把它们替换了。因此，如果我将很有用的宏命令移动到了字母表的末尾，就不用担心我在无意间把他们替换了。

&lt;h2 id=&quot;串行宏和并行宏&quot;&gt;串行宏和并行宏&lt;/h2&gt;

Vim 可以连续和同时运行宏命令，假设你有如下的文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;import { FUNC1 } from &quot;library1&quot;;
import { FUNC2 } from &quot;library2&quot;;
import { FUNC3 } from &quot;library3&quot;;
import { FUNC4 } from &quot;library4&quot;;
import { FUNC5 } from &quot;library5&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

假如你希望把所有的 “FUNC” 字符变为小写，那么宏命令为如下：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;qa0f{gui{jq
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

分解如下：
&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qa&lt;/code&gt; 开始记录宏命令到 “a” 寄存器。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;移动到第一行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f{&lt;/code&gt; 查找第一个 “{“ 字符。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gui{&lt;/code&gt; 把括号内的文本（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i{&lt;/code&gt;）变为小写（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gu&lt;/code&gt;）。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt; 移动到下一行。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; 停止记录宏命令。&lt;/li&gt;
&lt;/ul&gt;

现在，执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;99@a&lt;/code&gt; 在剩余的行修改。然而，假如在你的文本里有如下 import 语句会怎么样呢？

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;import { FUNC1 } from &quot;library1&quot;;
import { FUNC2 } from &quot;library2&quot;;
import { FUNC3 } from &quot;library3&quot;;
import foo from &quot;bar&quot;;
import { FUNC4 } from &quot;library4&quot;;
import { FUNC5 } from &quot;library5&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;99@a&lt;/code&gt;，会只在前三行执行。而最后两行不会被执行，因为在执行第四行（包含”foo”）时&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f{&lt;/code&gt;命令会遇到错误而停止，当宏串行执行时就会发生这样的情况。当然，你仍然可以移动到包含（”FUNC4”）的一行，并重新调用该命令。但是假如你希望仅调用一次命令就完成所有操作呢？

你可以并行地执行宏命令。

如本章前面所说，可以使用 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:normal&lt;/code&gt; 去执行宏命令，（例如： &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:3,5 normal @a&lt;/code&gt; 会在 3-5行执行 a 寄存器中的宏命令）。如果执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:1,$ normal @a&lt;/code&gt;，会在所有除了包含有 “foo” 的行执行，而且它不会出错。

尽管本质上来说，Vim 并不是在并行地执行宏命令，但表面上看，它是并行运行的。 Vim 会独立地在从第一行开始（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1,$&lt;/code&gt;）每一行执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt; 。由于 Vim 独立地在每一行执行命令，每一行都不会知道有一行（包含”foo”）会遇到执行错误。

&lt;h2 id=&quot;聪明地学习宏命令&quot;&gt;聪明地学习宏命令&lt;/h2&gt;

你在编辑器里做的很多事都是重复的。为了更好地编辑文件，请乐于发现这些重复性的行为。执行宏命令或者点命令，而不是做相同的动作两次。几乎所有你在 Vim 所作的事情都可以变为宏命令。

刚开始的时候，我发现宏命令时很棘手的，但是请不要放弃。有了足够的练习，你可以找到这种文本自动编辑的快乐。

使用某种助记符去帮助你记住宏命令是很有帮助的。如果你有一个创建函数（function）的宏命令，你可以使用 “f” 寄存器去录制它(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qf&lt;/code&gt;)。如果你有一个宏命令去操作数字，那么使用寄存器 “n” 去记住它是很好的(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qn&lt;/code&gt;)。用你想执行的操作时想起的第一个字符给你的宏命令命名。另外，我发现 “q” 是一个很好的宏命令默认寄存器，因为执行 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qq&lt;/code&gt; 去调用宏命令是很快速而简单的。最后，我喜欢按照字母表的顺序去添加我的宏命令，例如从 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qa&lt;/code&gt; 到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qb&lt;/code&gt; 再到 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qc&lt;/code&gt;。

去寻找最适合你的方法吧。

</description>
        <pubDate>Fri, 01 May 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch09-macros/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch09-macros/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>宏</category>
        
        <category>自动化</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第八章：寄存器</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

学习Vim中的寄存器就像第一次学习线性代数一样，除非你学习了他们，否则你会觉得自己根本不需要它们。

你可能已经在复制或删除文本并用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt;粘贴它们到别处的时候使用过Vim的寄存器了。但是，你知道Vim总共有10种不同类型的寄存器吗？如果正确地使用Vim寄存器，将帮助您从重复的输入中解放出来。

在这一章节中，我会介绍Vim的所有寄存器类型，以及如何有效地使用它们。

&lt;h2 id=&quot;寄存器的10种类型&quot;&gt;寄存器的10种类型&lt;/h2&gt;

下面是Vim所拥有的10种寄存器类型：

&lt;ol&gt;
  &lt;li&gt;匿名寄存器（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&quot;&lt;/code&gt;）.&lt;/li&gt;
  &lt;li&gt;编号寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;0-9&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;小删除寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;-&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;命名寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;a-z&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;只读寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;:&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;.&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;%&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;Buffer交替文件寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;#&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;表达式寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;=&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;选取和拖放寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;*&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;+&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;黑洞寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;_&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;搜索模式寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;/&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;寄存器命令&quot;&gt;寄存器命令&lt;/h2&gt;

要使用寄存器，您必须先使用命令将内容存储到寄存器，以下是一些存值到寄存器中的操作：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;y    复制
c    删除文本并进入输入模式
d    删除文本
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

其实还有更多的寄存器写入操作（比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;s&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;），但是上面列出的是最常用的一些。根据经验看来，如果一个操作删除了文本，那么很有可能这个操作将移除的文本存入寄存器中了。

想要从寄存器中取出（粘贴）文本，你可以用以下的命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;p    在光标位置之后粘贴文本
P    在光标位置之前粘贴文本
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt;都可以接受计数和一个寄存器标志作为参数。比如，想要把最近复制的文本粘贴10次的话可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10p&lt;/code&gt;。想粘贴寄存器”a”中的文本，可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;ap&lt;/code&gt;。想将寄存器”a”中的文本粘贴10次的话，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10&quot;ap&lt;/code&gt;。注意，从技术层面讲，命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;实际上表示的是”put”(放置)，而不是”paste”(粘贴)，使用粘贴只是因为它更符合传统习惯。

从某个特定寄存器中读取文本的通用语法是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;x&lt;/code&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;是这个寄存器的标志。

&lt;h2 id=&quot;在输入模式中使用寄存器&quot;&gt;在输入模式中使用寄存器&lt;/h2&gt;

在这一章节中你学到的东西在输入模式中也同样适用。想要获取寄存器”a”中的文本，通常可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;ap&lt;/code&gt;来进行。不过当你在输入模式下时，你需要运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r a&lt;/code&gt;。在输入模式下使用寄存器的语法是：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-r x
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;是寄存器标志。既然你现在已经知道如何存储和访问寄存器了，让我们学点更深入的吧。

&lt;h2 id=&quot;匿名寄存器&quot;&gt;匿名寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&quot;&lt;/code&gt;)&lt;/h2&gt;

想从匿名寄存器中获取文本，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&quot;p&lt;/code&gt;。 匿名寄存器默认存储着你最近一次复制，修改或删除的文本。如果再进行另一次复制，修改或删除，Vim会自动替换匿名寄存器中的文本。匿名寄存器和电脑上粘贴板的功能很接近。

默认情况下，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;(或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt;)是和匿名寄存器相关联的（从现在起我将使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;而不是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&quot;p&lt;/code&gt;来指代匿名寄存器）。

&lt;h2 id=&quot;编号寄存器0-9&quot;&gt;编号寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;0-9&lt;/code&gt;)&lt;/h2&gt;

编号寄存器会自动以升序来进行填充。一共有两种不同的编号寄存器：复制寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;)和其他编号寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1-9&lt;/code&gt;)。让我们先来讨论复制寄存器。

&lt;h3 id=&quot;复制寄存器-0&quot;&gt;复制寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;0&lt;/code&gt;)&lt;/h3&gt;

如果你使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yy&lt;/code&gt;来复制一整行文本，事实上Vim会将文本存放两个寄存器中：

&lt;ol&gt;
  &lt;li&gt;匿名寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;复制寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;0p&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

在你又复制其他不同的文本后，Vim会自动替换匿名寄存器和复制寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;)中的内容。其他的任何操作都不会被存放在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;号寄存器中。这可以为你提供方便，因为除非你再进行另一次复制，否则你已经复制的内容会一直在寄存器中，无论你进行多少次修改和删除。

比如，如果你：

&lt;ol&gt;
  &lt;li&gt;复制一整行 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yy&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;删除一整行(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dd&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;再删除另一行 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dd&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

复制寄存器中的文本仍然是第一步中复制的文本。

如果你:

&lt;ol&gt;
  &lt;li&gt;复制一整行 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yy&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;删除一整行 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dd&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;复制另一行 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yy&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

复制寄存器中的内容则是第三步中复制的内容。

还有一个小技巧，在输入模式下，你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r 0&lt;/code&gt;快速地粘贴你刚才复制的内容。

&lt;h3 id=&quot;编号寄存器-1-9&quot;&gt;编号寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;1-9&lt;/code&gt;)&lt;/h3&gt;

当你修改或者删除至少一整行的文本时，这部分文本会按时间顺序被存储在1-9号编号寄存器中。（编号越小时间距离越近）

比如，你有以下这些文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;line three
line two
line one
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

当你的光标在文本”line three”上时,使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dd&lt;/code&gt;来一行一行地删除这些文本。在所有文本都已经删除后，1号寄存器中的内容应该是”line one”（时间上最近的文本）， 2号寄存器则包含”line two”(时间上第二近的文本)，3号寄存器中则包含”line three”（最早删除的文本）。普通模式下可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;1p&lt;/code&gt;来获取1号寄存器中的内容。

编号寄存器的编号在使用点命令时会自动增加。比如，如果你的1号编号寄存器（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;1&lt;/code&gt;）中的内容为”line one”， 2号寄存器（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;2&lt;/code&gt;）为”line two”, 三号寄存器（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;3&lt;/code&gt;）”line three”,你可以使用以下的技巧来连续地粘贴他们：

&lt;ul&gt;
  &lt;li&gt;使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;1p&lt;/code&gt;来粘贴1号寄存器中的内容。&lt;/li&gt;
  &lt;li&gt;使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; (点命令)来粘贴2号寄存器（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;2&lt;/code&gt;）中的内容。&lt;/li&gt;
  &lt;li&gt;使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; (点命令)来粘贴3号寄存器（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;3&lt;/code&gt;）中的内容。&lt;/li&gt;
&lt;/ul&gt;

在连续地使用点命令时，Vim会自动的增加编号寄存器的编号。这个技巧对于所有的编号寄存器都适用。如果你从5号寄存器开始(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;5P&lt;/code&gt;), 点命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;会执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;6P&lt;/code&gt;,再次使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;则会执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;7P&lt;/code&gt;,等等。

小型的删除比如单词删除（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dw&lt;/code&gt;)或者单词修改(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cw&lt;/code&gt;)不会被存储在编号寄存器中，它们被存储在小删除寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;-&lt;/code&gt;)中，我将在接下来的一小节讨论小删除寄存器。

&lt;h2 id=&quot;小删除寄存器-&quot;&gt;小删除寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;-&lt;/code&gt;)&lt;/h2&gt;

不足一行的修改或者删除都不会被存储在0-9号编号寄存器中，而是会被存储在小删除寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;-&lt;/code&gt;)中。

比如:

&lt;ol&gt;
  &lt;li&gt;删除一个单词 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diw&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;删除一行文本 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dd&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;删除一行文本 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dd&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;-p&lt;/code&gt; 会给你第一步中删除的单词。

另一个例子:

&lt;ol&gt;
  &lt;li&gt;删除一个单词(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diw&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;删除一行文本 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dd&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;删除一个单词 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diw&lt;/code&gt;)&lt;/li&gt;
&lt;/ol&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;-p&lt;/code&gt; 会给出第三步中删除的单词。类似地, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;1p&lt;/code&gt; 会给出第二步中删除的一整行文本。不幸的是我们没有办法获取第一步中删除的单词，因为小删除寄存器只能存储一个文本。然而，如果你想保存第一步中删除的文本，你可以使用命名寄存器来完成。

&lt;h2 id=&quot;命名寄存器-a-z&quot;&gt;命名寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;a-z&lt;/code&gt;)&lt;/h2&gt;

命名寄存器是Vim中用法最丰富的寄存器。a-z命名寄存器可以存储复制的，修改的和被删除的文本。不像之前介绍的3种寄存器一样，它们会自动将文本存储到寄存器中，你需要显式地告诉Vim你要使用命名寄存器，你拥有完整的控制权。

为了复制一个单词到寄存器”a”中，你可以使用命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;ayiw&lt;/code&gt;。

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;a&lt;/code&gt;告诉Vim下一个动作（删除/修改/复制）会被存储在寄存器”a”中&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yiw&lt;/code&gt;复制这个单词&lt;/li&gt;
&lt;/ul&gt;

为了从寄存器”a”中获取文本，可以使用命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;ap&lt;/code&gt;。你可以使用以26个字母命名的寄存器来存储26个不同的文本。

有时你可能会想要往已有内容的命名寄存器中继续添加内容，这种情况下，你可以追加文本而不是全部重来。你可以使用大写版本的命名寄存器来进行文本的追加。比如，假设你的”a”寄存器中已经存有文本”Hello”，如果你想继续添加”world”到寄存器”a”中，你可以先找到文本”world”然后使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Aiw&lt;/code&gt;来进行复制,即可完成追加。

&lt;h2 id=&quot;只读寄存器--&quot;&gt;只读寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;:&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;.&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;%&lt;/code&gt;)&lt;/h2&gt;

Vim有三个只读寄存器：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;,&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&lt;/code&gt;，它们的用法非常简单：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;.    存储上一个输入的文本
:    存储上一次执行的命令
%    存储当前文件的文件名
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果你写入”Hello Vim”,之后再运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;.p&lt;/code&gt;就会打印出文本”Hello Vim”。如果你想要获得当前文件的文件名，可以运行命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;%p&lt;/code&gt;。如果你运行命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:s/foo/bar/g&lt;/code&gt;，再运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;:p&lt;/code&gt;的话则会打印出文本”s/foo/bar/g”。

&lt;h2 id=&quot;buffer交替文件寄存器-&quot;&gt;Buffer交替文件寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;#&lt;/code&gt;)&lt;/h2&gt;

在Vim中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#&lt;/code&gt;通常代表交替文件。交替文件指的是你上一个打开的文件，想要插入交替文件的名字的话，可以使用命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;#p&lt;/code&gt;。

&lt;h2 id=&quot;表达式寄存器-&quot;&gt;表达式寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;=&lt;/code&gt;)&lt;/h2&gt;

Vim有一个表达式寄存器，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;=&lt;/code&gt;,用于计算表达式的结果。

你可以使用以下命令计算数学表达式&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1+1&lt;/code&gt;的值：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot;=1+1&amp;lt;Enter&amp;gt;p
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

在这里，你在告诉Vim你正在使用表达式寄存器&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;=&lt;/code&gt;，你的表达式是（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1+1&lt;/code&gt;），你还需要输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;来得到结果。正如之前所提到的，你也可以在输入模式中访问寄存器。想要在输入模式中计算数学表达式的值，你可以使用：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-r =1+1
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt;来从任何寄存器中获取表达式并用表达式寄存器计算其值。如果你希望从寄存器”a”中获取文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot;=@a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

之后输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;enter&amp;gt;&lt;/code&gt;，再输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;。类似地，想在输入模式中得到寄存器”a”中的值可以使用：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-r =@a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

表达式是Vim中非常宏大的一个话题，所以我只会在这里介绍一些基础知识，我将会在之后的VimScript章节中进一步讲解更多关于表达式的细节。

&lt;h2 id=&quot;选取和拖放寄存器--&quot;&gt;选取和拖放寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;*&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;+&lt;/code&gt;)&lt;/h2&gt;

你难道不觉得有些时候你需要从某些外部的程序中复制一些文本并粘贴到Vim中吗，或者反过来操作？有了Vim的选取和拖放寄存器你就能办到。Vim有两个选取寄存器：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quotestar&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;*&lt;/code&gt;) 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quoteplus&lt;/code&gt; (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;+&lt;/code&gt;)。你可以用它们来访问从外部程序中复制的文本。

如果你在运行一个外部程序（比如Chrome浏览器），然后你使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-c&lt;/code&gt;(或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cmd-c&lt;/code&gt;,取决于你的操作系统)复制了一部分文本，通常你是没有办法在Vim里使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;来粘贴这部分文本的。但是，Vim的两个寄存器&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;+&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;*&lt;/code&gt;都是和你系统的粘贴板相连接的，所以你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;+p&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;*p&lt;/code&gt;来粘贴这些文本。反过来，如果你使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;+yiw&lt;/code&gt;或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;*yiw&lt;/code&gt;在Vim中复制了一些文本，你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-v&lt;/code&gt;（或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Cmd-v&lt;/code&gt;）。值得注意的是这个方法只在你的Vim开启了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+clipboard&lt;/code&gt;选项时才有用，可以在命令行中运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vim --version&lt;/code&gt;查看这一选项。如果你看见&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-clipboard&lt;/code&gt;的话，则需要安装一下支持Vim粘贴板的配置。

我发觉使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=*p&lt;/code&gt;或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=+p&lt;/code&gt;的话比较麻烦，为了使Vim仅使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;就能粘贴从外部程序复制的文本，你可以在你的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vimrc&lt;/code&gt;配置文件中加入下面一行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;set clipboard=unnamed
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;黑洞寄存器-_&quot;&gt;黑洞寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;_&lt;/code&gt;)&lt;/h2&gt;

你每次删除或修改文本的时候，这部分文本都会自动保存在Vim的寄存器中。有些时候你并不希望把什么东西都往寄存器里存，这该怎么办到呢？

你可以使用黑洞寄存器（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;_&lt;/code&gt;）。想要删除一行并且不将其存储在任何寄存器中时，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;_dd&lt;/code&gt;命令.

它是和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/dev/null&lt;/code&gt; 类似的寄存器。

&lt;h2 id=&quot;搜索模式寄存器-&quot;&gt;搜索模式寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;/&lt;/code&gt;)&lt;/h2&gt;

为了粘贴你的上一个搜索询问（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt; 或 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt;），你可以使用搜索模式寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;/&lt;/code&gt;)。使用命令 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;/p&lt;/code&gt;就能粘贴上一个搜索的条目。

&lt;h2 id=&quot;查看所有的寄存器&quot;&gt;查看所有的寄存器&lt;/h2&gt;

你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:register&lt;/code&gt;命令来查看你的所有寄存器。如果你只想查看”a”,”1”和”-“寄存器的内容的话则可以使用命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:register a 1 -&lt;/code&gt;。

有一个Vim的插件叫做 &lt;a href=&quot;https://github.com/junegunn/vim-peekaboo&quot;&gt;vim-peekaboo&lt;/a&gt; ,可以让你查看到寄存器的内容，在普通模式下输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt; 即可，或者在输入模式中输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r&lt;/code&gt;。我发现这个插件相当的有用，因为大多数时候我是记不住我的寄存器中的内容的。值得一试！

&lt;h2 id=&quot;执行寄存器&quot;&gt;执行寄存器&lt;/h2&gt;

命名寄存器不只可以用来存放文本，你还可以借助&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@&lt;/code&gt;来执行宏命令。我会在下一章节中介绍宏命令。

注意，因为宏命令时存储在Vim寄存器中的，使用宏时可能会覆盖存储的内容。如果你将文本”Hello Vim”存放在寄存器”a”中，并且之后你在同一个寄存器里记录了一个宏命令 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qa{macro-commands}q&lt;/code&gt;),那么这个宏命令将会覆盖之前存储的文本”Hello Vim”（你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@a&lt;/code&gt;来执行寄存器中存储的宏命令）。

&lt;h2 id=&quot;清除寄存器&quot;&gt;清除寄存器&lt;/h2&gt;

从技术上来说，我们没有必要来清除任何寄存器，因为你下一个使用来存储文本的寄存器会自动覆盖该寄存器中之前的内容。然而，你可以通过记录一个空的宏命令来快速地清除任何命名寄存器。比如，如果你运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;qaq&lt;/code&gt;，Vim就会在寄存器”a”中记录一个空的宏命令。

还有一种方法就是运行命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:call setreg(&apos;a&apos;,&apos;hello register a&apos;)&lt;/code&gt;,其中’a’代表的就是寄存器”a”。而”hello register a”就是你想存储的内容。

还有一种清除寄存器的方法就是使用表达式&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:let @a = &apos;&apos;&lt;/code&gt;来将寄存器”a 的值设为空的字符串。

&lt;h2 id=&quot;获取寄存器中的内容&quot;&gt;获取寄存器中的内容&lt;/h2&gt;

你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:put&lt;/code&gt;命令来粘贴任何寄存器的内容。比如，如果你运行命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:put a&lt;/code&gt;,Vim就会打印出寄存器”a”的内容，这和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;ap&lt;/code&gt;非常像，唯一的区别在于在普通模式下命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;在当前光标位置之后打印寄存器的内容，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:put&lt;/code&gt;新起一行来打印寄存器的内容。

因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:put&lt;/code&gt;是一个命令行命令，您可以传一个地址给它。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:+10put a&lt;/code&gt;将会在当前光标下数10行，然后插入新行，内容为寄存器a中的内容。

一个很酷的技巧是将黑洞寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;_&lt;/code&gt;)传给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:put&lt;/code&gt;命令。因为黑洞寄存器不保存任何值，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:put _&lt;/code&gt;命令将插入一个新的空白行。您可将这个与全局命令联合起来，插入多个空行。比如，要在所有以文本”end”结尾的行下插入空行，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:g/end/put _&lt;/code&gt;。在后面您将了解关于全局命令的知识。

&lt;h2 id=&quot;聪明地学习寄存器&quot;&gt;聪明地学习寄存器&lt;/h2&gt;

恭喜你成功地坚持到了最后！这一章有非常多的内容需要消化。如果你感觉被新的知识淹没，你要知道你并不孤单，当我最初开始学习Vim寄存器时也有这种感觉。

我并不认为你必须现在就记得所有的知识点。为了提高我们的生产效率，你可以从使用以下三类寄存器开始：

&lt;ol&gt;
  &lt;li&gt;匿名寄存器(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;&quot;&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;命名寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;a-z&lt;/code&gt;).&lt;/li&gt;
  &lt;li&gt;编号寄存器 (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;0-9&lt;/code&gt;).&lt;/li&gt;
&lt;/ol&gt;

既然匿名寄存器是默认和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt;，你只需要学习两个寄存器：命名寄存器和编号寄存器。之后如果你需要用到其他的寄存器时你再逐渐地学习其他寄存器的用法，不用急，慢慢来。

普通人的短期记忆都是有极限的，大概每次只能记住5-7个信息。这就是为什么在我的日常编辑中，我只用3到7个命名寄存器的原因，我没有办法记住整整26个寄存器的内容。我通常从寄存器”a”开始用，之后用寄存器”b”,以字母表升序的顺序来使用。尝试一下各种方法，看看哪种最适合你。

Vim寄存器非常强大，合理使用的话能够避免你输入数不清的重复文本。但是现在，是时候学习一下宏命令了。

</description>
        <pubDate>Thu, 30 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch08-registers/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch08-registers/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>寄存器</category>
        
        <category>复制粘贴</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第七章：点命令</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

在编辑文本时，我们应该尽可能地避免重复的动作。在这一章节中，你将会学习如何使用点命令来重放上一个修改操作。点命令是最简单的命令，然而又是减少重复操作最为有用的命令。

&lt;h2 id=&quot;用法&quot;&gt;用法&lt;/h2&gt;

正如这个命令的名字一样，你可以通过按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;键来使用点命令。

比如，如果你想将下面文本中的所有”let”替换为”const”：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let one = &quot;1&quot;;
let two = &quot;2&quot;;
let three = &quot;3&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;首先，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/let&lt;/code&gt;来进行匹配。&lt;/li&gt;
  &lt;li&gt;接着，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cwconst&amp;lt;esc&amp;gt;&lt;/code&gt;来将”let”替换成”const”。&lt;/li&gt;
  &lt;li&gt;第三步，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;来找到下一个匹配的位置。&lt;/li&gt;
  &lt;li&gt;最后，使用点命令(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;)来重复之前的操作。&lt;/li&gt;
  &lt;li&gt;持续地使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n . n .&lt;/code&gt;直到每一个匹配的词都被替换。&lt;/li&gt;
&lt;/ul&gt;

在这个例子里面，点命令重复的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cwconst&amp;lt;esc&amp;gt;&lt;/code&gt;这一串命令，它能够帮你将需要8次输入的命令简化到只需要敲击一次键盘。

&lt;h2 id=&quot;什么才算是修改操作&quot;&gt;什么才算是修改操作？&lt;/h2&gt;

如果你查看点命令的定义的话(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h .&lt;/code&gt;),文档中说点命令会重复上一个修改操作，那么什么才算是一个修改操作呢？

当你使用普通模式下的命令来更新（添加，修改或者删除）当前缓冲区中的内容时，你就是在执行一个修改操作了。其中的例外是使用命令行命令进行的修改（以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;：&lt;/code&gt;开头的命令），这些命令不算作修改操作。

在第一个例子中，你看到的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cwconst&amp;lt;esc&amp;gt;&lt;/code&gt;就是一个修改操作。现在假设你有以下这么一个句子：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pancake, potatoes, fruit-juice,
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我们来删除从这行开始的位置到第一个逗号出现的位置。你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;df,&lt;/code&gt;来完成这个操作,使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;来重复两次直到你将整个句子删除。

让我们再来试试另一个例子：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pancake, potatoes, fruit-juice,
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这一次你只需要删除所有的逗号，不包括逗号前面的词。我们可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f,&lt;/code&gt;来找到第一个逗号，再使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;来删除光标下的字符。然后使用用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;来重复两次，很简单对不对？等等！这样做行不通(只会重复删除光标下的一个字符，而不是删除逗号)！为什么会这样呢？

在Vim里，修改操作是不包括移动（motions）的，因为移动(motions)不会更新缓冲区的内容。当你运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f,x&lt;/code&gt;，你实际上是在执行两个独立的操作：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f,&lt;/code&gt;命令只移动光标，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;更新缓冲区的内容，只有后者算作修改动作。和之前例子中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;df,&lt;/code&gt;进行一下对比的话，你会发现&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;df,&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f,&lt;/code&gt;告诉删除操作&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt;哪里需要删除，是整个删除命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;df,&lt;/code&gt;的一部分。

让我们想想办法完成这个任务。在你运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f,&lt;/code&gt;并执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;来删除第一个逗号后，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;;&lt;/code&gt;来继续匹配&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt;的下一个目标（下一个逗号）。之后再使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;来重复修改操作，删除光标下的字符。重复&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;; . ; .&lt;/code&gt;直到所有的逗号都被删除。完整的命令即为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f,x;.;.&lt;/code&gt;。

再来试试下一个例子：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;pancake
potatoes
fruit-juice
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我们的目标是给每一行的结尾加上逗号。从第一行开始，我们执行命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A,&amp;lt;esc&amp;gt;j&lt;/code&gt;来给结尾加上逗号并移动到下一行。现在我们知道了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt;是不算作修改操作的，只有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A,&lt;/code&gt;算作修改操作。你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j . j . &lt;/code&gt;来移动并重复修改操作。完整的命令是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A,&amp;lt;esc&amp;gt;j&lt;/code&gt;。

从你按下输入命令（A）开始到你退出输入模式（&lt;esc&gt;）之间的所有输入都算作是一整个修改操作。&lt;/esc&gt;

&lt;h2 id=&quot;重复多行修改操作&quot;&gt;重复多行修改操作&lt;/h2&gt;

假设你有如下的文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let one = &quot;1&quot;;
let two = &quot;2&quot;;
let three = &quot;3&quot;;
const foo = &quot;bar&quot;;
let four = &quot;4&quot;;
let five = &quot;5&quot;;
let six = &quot;6&quot;;
let seven = &quot;7&quot;;
let eight = &quot;8&quot;;
let nine = &quot;9&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你的目标是删除除了含有”foo”那一行以外的所有行。首先，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d2j&lt;/code&gt;删除前三行。之后跳过”foo”这一行，在其下一行使用点命令两次来删除剩下的六行。完整的命令是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d2jj..&lt;/code&gt;。

这里的修改操作是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d2j&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2j&lt;/code&gt;不是一个移动(motion)操作，而是整个删除命令的一部分。

我们再来看看下一个例子：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;zlet zzone = &quot;1&quot;;
zlet zztwo = &quot;2&quot;;
zlet zzthree = &quot;3&quot;;
let four = &quot;4&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我们的目标是删除所有的’z’。从第一行第一个字符开始，首先，在块可视化模式下使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-vjj&lt;/code&gt;来选中前三行的第一个’z’字母。如果你对块可视化模式不熟悉的话也不用担心，我会在下一章节中进行介绍。在选中前三行的第一个’z’后，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt;来删除它们。接着用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt;移动到下一个z字母上，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;..&lt;/code&gt;重复两次之前选中加删除的动作。完整的命令为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-vjjdw..&lt;/code&gt;。

你删除一列上的三个’z’的操作(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-vjjd&lt;/code&gt;)被看做一整个修改操作。可视化模式中的选择操作可以用来选中多行，作为修改动作的一部分。

&lt;h2 id=&quot;在修改中包含移动操作&quot;&gt;在修改中包含移动操作&lt;/h2&gt;

让我们来重新回顾一下本章中的第一个例子。这个例子中我们使用了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/letcwconst&amp;lt;esc&amp;gt;&lt;/code&gt;紧接着&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n . n .&lt;/code&gt;将下面的文本中的’let’都替换成了’const’。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let one = &quot;1&quot;;
let two = &quot;2&quot;;
let three = &quot;3&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

其实还有更快的方法来完成整个操作。当你使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/let&lt;/code&gt;搜索后，执行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cgnconst&amp;lt;Esc&amp;gt;&lt;/code&gt;，然后&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;. . .&lt;/code&gt;。

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gn&lt;/code&gt;是一个移动并选择的动作，它向前搜索和上一个搜索的模式（本例中为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/let&lt;/code&gt;）匹配的位置，并且 &lt;strong&gt;自动对匹配的文本进行可视化模式下的选取&lt;/strong&gt;。想要对下一个匹配的位置进行替换的话，你不再需要先移动在重复修改操作（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n . n .&lt;/code&gt;），而是简单地使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;. .&lt;/code&gt;就能完成。你不需要再进行移动操作了，因为找到下一个匹配的位置并进行选中成为了修改操作的一部分了。

当你在编辑文本时，应该时刻关注像&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gn&lt;/code&gt;命令这种能一下子做好几件事的移动操作。

&lt;blockquote&gt;
  译者总结规律：单独的motion(第4章中所说的名词)不算修改操作，而operator(动词)+motion(名词)时，motion被视为一个完整的修改操作中的一部分。再看一个例子，看看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;命令是如何被包含在一个修改操作中的：
&lt;/blockquote&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;a
b
foo
c
d
foo
e
f
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;blockquote&gt;
  假设你的光标在第一行的a上，执行命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d/foo&amp;lt;Esc&amp;gt;&lt;/code&gt;，Vim会删除a,b。然后&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;，Vim会删除foo, c, d，再按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;，Vim什么也不做，因为后面没有”foo”了。在这个例子中，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/foo&lt;/code&gt;是一个motion（名词），是Vim语法(动词+名词：operator + motion)的一部分，前面的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt;则是动词。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d/foo&amp;lt;Esc&amp;gt;&lt;/code&gt;这条命令的功能是：从当前光标所在位置开始删除，直到遇到”foo”为止。后面的点命令就重复这个功能，第二次按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;之所以Vim什么也不做，是因为找不到下一个匹配了，所以这条命令就失效了。
&lt;/blockquote&gt;

&lt;h2 id=&quot;聪明地学习点命令&quot;&gt;聪明地学习点命令&lt;/h2&gt;

点命令的强大之处在于使用仅仅1次键盘敲击代替好几次敲击。对于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;这种只需一次敲击键盘就能完成的修改操作来说，点命令或许不会带来什么收益。但是如果你的上一个修改操作是像&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cgnconst&amp;lt;esc&amp;gt;&lt;/code&gt;这种复杂命令的话，使用点命令来替代就有非常可观的收益了。

在进行编辑时，思考一下你正将进行的操作是否是可以重复的。举个例子，如果我需要删除接下来的三个单词，是使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d3w&lt;/code&gt;更划算，还是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dw&lt;/code&gt;再使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;两次更划算？之后还会不会再进行删除操作？如果是这样的话，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dw&lt;/code&gt;好几次确实比&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d3w&lt;/code&gt;更加合理，因为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dw&lt;/code&gt;更加有复用性。在编辑时应该养成”修改操作驱动”的观念。

点命令非常简单但又功能强大，帮助你开始自动化处理简单的任务。在后续的章节中，你将会学习到如何使用Vim的宏命令来自动化处理更多复杂的操作。但是首先，还是让我们来学习一下如何使用寄存器来存取文本吧。

</description>
        <pubDate>Wed, 29 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch07-the-dot-command/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch07-the-dot-command/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>点命令</category>
        
        <category>重复</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第六章：输入模式</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

输入模式是大部分文本编辑器的默认模式，在这个模式下，所敲即所得。

尽管如此，这并不代表输入模式没什么好学的。Vim的输入模式包含许多有用功能。在这一章节中，你将能够学到如何利用Vim输入模式中的特性来提升你的输入效率。

&lt;h2 id=&quot;进入输入模式的方法&quot;&gt;进入输入模式的方法&lt;/h2&gt;

我们有很多方式从普通模式进入输入模式，下面列举出了其中的一些方法：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;i    从光标之前的位置开始输入文本
I    从当前行第一个非空字符之前的位置之前开始输入文本
a    在光标之后的位置追加文本
A    在当前行的末尾追加文本
o    在光标位置下方新起一行并开始输入文本
O    在光标位置的上方新起一行并开始输入文本
s    删除当前光标位置的字符并开始输入文本
S    删除当前行并开始输入文本
gi   从当前缓冲区上次结束输入模式的地方开始输入文本
gI   在当前行的第一列的位置开始输入文本
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

值得注意的是这些命令的小写/大写模式，每一个小写命令都有一个与之对应的大写命令。如果你是初学者，不用担心记不住以上整个命令列表，可以从 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt; 和 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;两条命令开始，这两条命令足够在入门阶段使用了，之后再逐渐地掌握更多其他的命令。

&lt;h2 id=&quot;退出输入模式的方法&quot;&gt;退出输入模式的方法&lt;/h2&gt;

下面列出了一些从输入模式退出到普通模式的方法：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&amp;lt;esc&amp;gt;     退出输入模式进入普通模式
Ctrl-[    退出输入模式进入普通模式
Ctrl-c    与 Ctrl-[ 和 &amp;lt;esc&amp;gt;功能相同, 但是不检查缩写
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我发现&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt; esc&lt;/code&gt;键在键盘上太远了，很难够到，所以我在我的机器上将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt; caps lock&lt;/code&gt; 映射成了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt;键。 如果你搜索Bill Joy（Vi的作者）的ADM-3A 键盘， 你会发现&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt;键并不是像现在流行的键盘布局一样在键盘的最左上方，而是在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt;键的左边，所以我认为将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;caps lock&lt;/code&gt; 映射成&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt;键是合理的。

另一个Vim用户中常见的习惯是用以下的配置方法在输入模式中把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;esc&lt;/code&gt;映射到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jj&lt;/code&gt;或者&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;jk&lt;/code&gt;。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;inoremap jj &amp;lt;esc&amp;gt;
inoremap jk &amp;lt;esc&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;重复输入模式&quot;&gt;重复输入模式&lt;/h2&gt;

你可以在进入输入模式之前传递一个计数参数. 比如：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;10i
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果你输入”hello world!”然后退出输入模式， Vim将重复这段文本10次。这个方法对任意一种进入输入模式的方式都有效（如：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10I&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;11a&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12o&lt;/code&gt;）

&lt;h2 id=&quot;在输入模式中删除大块文本&quot;&gt;在输入模式中删除大块文本&lt;/h2&gt;

当你输入过程中出现一些输入错误时，一直重复地用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;backspace&lt;/code&gt;来删除的话会非常地繁琐。更为合理的做法是切换到普通模式并使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt;来删除错误。或者，你能用以下命令在输入模式下就删除一个或者多个字符：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-h    删除一个字符
Ctrl-w    删除一个单词
Ctrl-u    删除一整行
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

此外，这些快捷键也支持在 命令行模式 和 Ex模式 中使用（命令行模式和Ex模式将会在之后的章节中介绍）

&lt;h2 id=&quot;用寄存器进行输入&quot;&gt;用寄存器进行输入&lt;/h2&gt;

寄存器就像是内存里的暂存器一样，可供存储和取出文本。在输入模式下，可以使用快捷键&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-r&lt;/code&gt;加上寄存器的标识来从任何有标识的寄存器输入文本。有很多标识可供使用，但是在这一章节中你只需要知道以（a-z)命名的寄存器是可以使用的就足够了。

让我们在一个具体的例子中展示寄存器的用法，首先你需要复制一个单词到寄存器a中，这一步可以用以下这条命令来完成：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&quot;ayiw
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;a&lt;/code&gt; 告诉Vim你下一个动作的目标地址是寄存器a&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yiw&lt;/code&gt; 复制一个内词（inner word），可以回顾Vim语法章节查看具体语法。&lt;/li&gt;
&lt;/ul&gt;

现在寄存器a存放着你刚复制的单词。在输入模式中，使用以下的快捷键来粘贴存放在寄存器a中文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-r a
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim中存在很多种类型的寄存器，我会在后面的章节中介绍更多他们的细节。

&lt;h2 id=&quot;页面滚动&quot;&gt;页面滚动&lt;/h2&gt;

你知道在输入模式中也是可以进行页面滚动的吗？在输入模式下，如果你使用快捷键&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-x&lt;/code&gt;进入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-x&lt;/code&gt;子模式，你可以进行一些额外操作，页面滚动正是其中之一。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-x Ctrl-y    向上滚动页面
Ctrl-x Ctrl-e    向下滚动页面
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;自动补全&quot;&gt;自动补全&lt;/h2&gt;

Vim在进入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-x&lt;/code&gt;子模式后（和页面滚动一样），有一个自带的自动补全功能。尽管它不如intellisense或者其他的语言服务器协议（LSP）一样好用，但是也算是一个锦上添花的内置功能了。

下面列出了一些适合入门时学习的自动补全命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-x Ctrl-l	   补全一整行
Ctrl-x Ctrl-n	   从当前文件中补全文本
Ctrl-x Ctrl-i	   从引用（include）的文件中补全文本
Ctrl-x Ctrl-f	   补全一个文件名
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

当你触发自动补全时，Vim会显示一个选项弹窗，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-n&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-p&lt;/code&gt;来分别向上和向下浏览选项。

Vim也提供了两条不需要进入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-x&lt;/code&gt;模式就能使用的命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-n             使用下一个匹配的单词进行补全
Ctrl-p             使用上一个匹配的单词进行补全
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

通常Vim会关注所有缓冲区（buffer）中的文本作为自动补全的文本来源。如果你打开了一个缓冲区，其中一行是”Chocolate donuts are the best”：

&lt;ul&gt;
  &lt;li&gt;当你输入”Choco”然后使用快捷键&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-x Ctrl-l&lt;/code&gt;， Vim会进行匹配并输出这一整行的文本。&lt;/li&gt;
  &lt;li&gt;当你输入”Choco”然后使用快捷键&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-p&lt;/code&gt;，Vim会进行匹配并输出”Chocolate”这个单词。&lt;/li&gt;
&lt;/ul&gt;

Vim的自动补全是一个相当大的话题，以上只是冰山一角，想要进一步学习的话可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h ins-completion&lt;/code&gt;命令进行查看。

&lt;h2 id=&quot;执行普通模式下的命令&quot;&gt;执行普通模式下的命令&lt;/h2&gt;

你知道Vim可以在输入模式下执行普通模式的命令吗？

在输入模式下， 如果你按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-o&lt;/code&gt;，你就会进入到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;insert-normal&lt;/code&gt;（输入-普通）子模式。如果你关注一下左下角的模式指示器，通常你将看到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-- INSERT --&lt;/code&gt; ，但是按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-o&lt;/code&gt;后就会变为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-- (insert) --&lt;/code&gt;。 在这一模式下，你可以执行一条普通模式的命令，比如你可以做以下这些事：

&lt;strong&gt;设置居中以及跳转&lt;/strong&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-o zz       居中窗口
Ctrl-o H/M/L    跳转到窗口的顶部/中部/底部
Ctrl-o &apos;a       跳转到标志&apos;a处
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;重复文本&lt;/strong&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-o 100ihello    输入 &quot;hello&quot; 100 次
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;执行终端命令&lt;/strong&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-o !! curl https://google.com    运行curl命令
Ctrl-o !! pwd                        运行pwd命令
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;快速删除&lt;/strong&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-o dtz    从当前位置开始删除文本，直到遇到字母&quot;z&quot;
Ctrl-o D      从当前位置开始删除文本，直到行末
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;聪明地学习输入模式&quot;&gt;聪明地学习输入模式&lt;/h2&gt;

如果你和我一样是从其他文本编辑器转到Vim的，你或许也会觉得一直待在输入模式下很有诱惑力，但是我强烈反对你在没有输入文本时，却仍然待在输入模式下。应该养成当你的双手没有在输入时，就退出到普通模式的好习惯。

当你需要进行输入时，先问问自己将要输入的文本是否已经存在。如果存在的话，试着复制或者移动这段文本而不是手动输入它。再问问自己是不是非得进入输入模式，试试能不能尽可能地使用自动补全来进行输入。尽量避免重复输入同一个单词。

</description>
        <pubDate>Tue, 28 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch06-insert-mode/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch06-insert-mode/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>插入模式</category>
        
        <category>自动补全</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第五章：在文件中移动</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

一开始，通过键盘移动会让你感觉特别慢特别不自在，但是不要放弃！一旦你习惯了它，比起鼠标你可以更快的在文件中去到任何地方。

这一章，你将学习必要的移动以及如何高效的使用它们。 记住，这一章所讲的并不是Vim的全部移动命令(motions)，我们的目标是介绍有用的移动来快速提高效率。 如果你需要学习更多的移动命令，查看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h motion.txt&lt;/code&gt;。

&lt;h2 id=&quot;字符导航&quot;&gt;字符导航&lt;/h2&gt;

最基本的移动单元是上下左右移动一个字符。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;h   左
j   下
k   上
l   右
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你也可以通过方向键进行移动，如果你只是初学者，使用任何你觉得最舒服的方法都没有关系。

我更喜欢&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hjkl&lt;/code&gt;因为我的右手可以保持在键盘上的默认姿势，这样做可以让我更快的敲到周围的键。 为了习惯它，我实际上在刚开始的时候通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vimrc&lt;/code&gt;关闭了方向键：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;noremap &amp;lt;Up&amp;gt; &amp;lt;NOP&amp;gt;
noremap &amp;lt;Down&amp;gt; &amp;lt;NOP&amp;gt;
noremap &amp;lt;Left&amp;gt; &amp;lt;NOP&amp;gt;
noremap &amp;lt;Right&amp;gt; &amp;lt;NOP&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

也有一些插件可以帮助改掉这个坏习惯，其中有一个叫&lt;a href=&quot;https://github.com/takac/vim-hardtime&quot;&gt;vim-hardtime&lt;/a&gt;。 让我感到惊讶的是，我只用了几天就习惯了使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hjkl&lt;/code&gt;。

另外，如果你想知道为什么Vim使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hjkl&lt;/code&gt;进行移动，&lt;em&gt;这实际上是因为Bill Joy写VI用的Lear-Siegler ADM-3A终端没有方向键，而是把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hjkl&lt;/code&gt;当做方向键&lt;/em&gt;。

如果你想移动到附近的某个地方，比如从一个单词的一个部分移动到另一个部分，我会使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt;。 如果我需要在可见的范围内上下移动几行，我会使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt;。 如果我想去更远的地方，我倾向于使用其他移动命令。

&lt;h2 id=&quot;相对行号&quot;&gt;相对行号&lt;/h2&gt;

我觉得设置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;number&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;relativenumber&lt;/code&gt;非常有用，你可以在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vimrc&lt;/code&gt;中设置:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;set relativenumber number
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这将会展示当前行号和其他行相对当前行的行号。

为什么这个功能有用呢？这个功能能够帮助我知道我离我的目标位置差了多少行，有了它我可以很轻松的知道我的目标行在我下方12行，因此我可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12j&lt;/code&gt;去前往。 否则，如果我在69行，我的目标是81行，我需要去计算81-69=12行，这太费劲了，当我需要去一个地方时，我需要思考的部分越少越好。

这是一个100%的个人偏好，你可以尝试&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;relativenumber&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;norelativenumber&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;number&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nonumber&lt;/code&gt; 然后选择自己觉得最有用的。

&lt;h2 id=&quot;对移动计数&quot;&gt;对移动计数&lt;/h2&gt;

在继续之前，让我们讨论一下”计数”参数。 一个移动(motion)可以接受一个数字前缀作为参数，上面我提到的你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12j&lt;/code&gt;向下移动12行，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12j&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12&lt;/code&gt;就是计数数字。

你使用带计数的移动的语法如下：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[计数] + 移动
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你可以把这个应用到所有移动上，如果你想向右移动9个字符，你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;9l&lt;/code&gt;来代替按9次&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt;。 当你学到了更多的动作时，你都可以试试给定计数参数。

&lt;h2 id=&quot;单词导航&quot;&gt;单词导航&lt;/h2&gt;

我们现在移动一个更长的单元：单词(word)。 你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt;移动到下一个单词的开始，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt;移动到下一个单词的结尾，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b&lt;/code&gt;移动到上一个单词的开始，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ge&lt;/code&gt;移动到前一个单词的结尾。

另外，为了和上面说的单词(word)做个区分，还有一种移动的单元：词组(WORD)。 你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;W&lt;/code&gt;移动到下一个词组的开始，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;E&lt;/code&gt;移动到下一个词组的结尾，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt;移动到前一个词组的开头，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gE&lt;/code&gt;移动到前一个词组的结尾。 为了方便记忆，所以我们选择了词组和单词这两个词，相似但有些区分。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;w		移动到下一个单词的开头
W		移动到下一个词组的开头
e		移动到下一个单词的结尾
E		移动到下一个词组的结尾
b		移动到前一个单词的开头
B		移动到前一个词组的开头
ge	移动到前一个单词的结尾
gE	移动到前一个词组的结尾
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

词组和单词到底有什么相同和不同呢？单词和词组都按照非空字符被分割，一个单词指的是一个只包含&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a-zA-Z0-9&lt;/code&gt;字符串，一个词组指的是一个包含除了空字符(包括空格，tab，EOL)以外的字符的字符串。 你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h word&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h WORD&lt;/code&gt;了解更多。

例如，假如你有下面这段内容：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const hello = &quot;world&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

当你光标位于这行的开头时，你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt;走到行尾，但是你需要按21下，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt;，你需要6下，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;W&lt;/code&gt;只需要4下。 单词和词组都是短距离移动的很好的选择。

然而，之后你可以通过当前行导航只按一次从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt;移动到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;;&lt;/code&gt;。

&lt;h2 id=&quot;当前行导航&quot;&gt;当前行导航&lt;/h2&gt;

当你在进行编辑的时候，你经常需要水平地在一行中移动，你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;跳到本行第一个字符，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt;跳到本行最后一个字符。 另外，你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt;跳到本行第一个非空字符，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g_&lt;/code&gt;跳到本行最后一个非空字符。 如果你想去当前行的第n列，你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n|&lt;/code&gt;。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;0		跳到本行第一个字符
^		跳到本行第一个非空字符
g_      跳到本行最后一个非空字符
$		跳到本行最后一个字符
n|      跳到本行第n列
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你也可以在本行通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt;进行行内搜索，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt;的区别在于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt;会停在第一个匹配的字母上，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt;会停在第一个匹配的字母前。 因此如果你想要搜索并停留在”h”上，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fh&lt;/code&gt;。 如果你想搜索第一个”h”并停留在它的前一个字母上，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;th&lt;/code&gt;。 如果你想去下一个行内匹配的位置，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;;&lt;/code&gt;，如果你想去前一个行内匹配的位置，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;,&lt;/code&gt;。

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;T&lt;/code&gt;是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;t&lt;/code&gt;对应的向后搜索版本。如果想向前搜索”h”，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Fh&lt;/code&gt;，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;;&lt;/code&gt;，保持相同的搜索方向搜索下一个匹配的字母。 注意，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;;&lt;/code&gt;不是总是向后搜索，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;;&lt;/code&gt;表示的是上一次搜索的方向，因此如果你使用的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt;，那么使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;;&lt;/code&gt;时将会向前搜索使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;,&lt;/code&gt;时向后搜索。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;f   在同一行向后搜索第一个匹配
F   在同一行向前搜索第一个匹配
t   在同一行向后搜索第一个匹配，并停在匹配前
T   在同一行向前搜索第一个匹配，并停在匹配前
;   在同一行重复最近一次搜索
,   在同一行向相反方向重复最近一次搜索
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

回到上一个例子：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;const hello = &quot;world&quot;;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

当你的光标位于行的开头时，你可以通过按一次键&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt;去往行尾的最后一个字符”;”。 如果想去往”world”中的”w”，你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fw&lt;/code&gt;。 一个建议是，在行内目标附近通过寻找重复出现最少的字母例如”j”，”x”，”z”来前往行中的该位置更快。

&lt;h2 id=&quot;句子和段落导航&quot;&gt;句子和段落导航&lt;/h2&gt;

接下来两个移动的单元是句子和段落。

首先我们来聊聊句子。 一个句子的定义是以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.!?&lt;/code&gt;和跟着的一个换行符或空格，tab结尾的。 你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;)&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&lt;/code&gt;跳到下一个和上一个句子。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(   跳到前一个句子
)   跳到下一个句子
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

让我们来看一些例子，你觉得哪些字段是句子哪些不是? 可以尝试在Vim中用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;)&lt;/code&gt;感受一下。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;I am a sentence. I am another sentence because I end with a period. I am still a sentence when ending with an exclamation point! What about question mark? I am not quite a sentence because of the hyphen - and neither semicolon ; nor colon :

There is an empty line above me.

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

另外，如果你的Vim中遇到了无法将一个以&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;结尾的字段并且后面跟着一个空行的这种情况判断为一个句子的问题，你可能处于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compatible&lt;/code&gt;的模式。 运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:set nocompatible&lt;/code&gt;可以修复。 在Vi中，一个句子是以&lt;strong&gt;两个&lt;/strong&gt;空格结尾的，你应该总是保持的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nocompatible&lt;/code&gt;的设置。

接下来，我们将讨论什么是段落。 一个段落可以从一个空行之后开始，也可以从段落选项(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;paragraphs&lt;/code&gt;)中”字符对”所指定的段落宏的每个集合开始。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;{   跳转到上一个段落
}   跳转到下一个段落
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果你不知道什么是段落宏，不用担心，重要的是一个段落总是以一个空行开始和结尾， 在大多数时候总是对的。

&lt;h2 id=&quot;匹配导航&quot;&gt;匹配导航&lt;/h2&gt;

程序员经常编辑含有代码的文件，这种文件内容会包含大量的小括号，中括号和大括号，并且可能会把你搞迷糊你当前到底在哪对括号里。 许多编程语言都用到了小括号，中括号和大括号，你可能会迷失于其中。 如果你在它们中的某一对括号中，你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&lt;/code&gt;跳到其中一个括号或另一个上(如果存在)。 你也可以通过这种方法弄清你是否各个括号都成对匹配了。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;%    Navigate to another match, usually works for (), [], {}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我们来看一段Scheme代码示例因为它用了大量的小括号。 你可以在括号中用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&lt;/code&gt;移动

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(define (fib n)
  (cond ((= n 0) 0)
        ((= n 1) 1)
        (else
          (+ (fib (- n 1)) (fib (- n 2)))
        )))
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我个人喜欢使用类似&lt;a href=&quot;https://github.com/frazrepo/vim-rainbow&quot;&gt;vim-rainbow&lt;/a&gt;这样的可视化指示插件来作为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%&lt;/code&gt;的补充。 通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h %&lt;/code&gt;了解更多。

&lt;h2 id=&quot;行号导航&quot;&gt;行号导航&lt;/h2&gt;

你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nG&lt;/code&gt;调到行号为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;的行，例如如果你想跳到第7行，你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;7G&lt;/code&gt;，跳到第一行使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gg&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1G&lt;/code&gt;，跳到最后一行使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;G&lt;/code&gt;。

有时你不知道你想去的位置的具体行号，但是知道它大概在整个文件的70%左右的位置，你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;70%&lt;/code&gt;跳过去，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;50%&lt;/code&gt;跳到文件的中间。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;gg      跳转到第一行
G       跳转到最后一行
nG      跳转到第n行
n%      跳到文件的n%
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

另外，如果你想看文件总行数，可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CTRL-g&lt;/code&gt;查看。

&lt;h2 id=&quot;窗格导航&quot;&gt;窗格导航&lt;/h2&gt;

为了移动到当前窗格的顶部，中间，底部，你可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;H&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L&lt;/code&gt;。

你也可以给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;H&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;L&lt;/code&gt;传一个数字前缀。 如果你输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10H&lt;/code&gt;你会跳转到窗格顶部往下数10行的位置，如果你输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3L&lt;/code&gt;，你会跳转到距离当前窗格的底部一行向上数3行的位置。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;H   跳转到屏幕的顶部
M   跳转到屏幕的中间
L   跳转到屏幕的底部
nH  跳转到距离顶部n行的位置
nL  跳转到距离底部n行的位置
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;滚动&quot;&gt;滚动&lt;/h2&gt;

在文件中滚动，你有三种速度可以选择： 滚动一整页(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CTRL-F&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CTRL-B&lt;/code&gt;)，滚动半页(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CTRL-D&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CTRL-U&lt;/code&gt;)，滚动一行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CTRL-E&lt;/code&gt;/&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;CTRL-Y&lt;/code&gt;)。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-e    向下滚动一行
Ctrl-d    向下滚动半屏
Ctrl-f    向下滚动一屏
Ctrl-y    向上滚动一行
Ctrl-u    向上滚动半屏
Ctrl-b    向上滚动一屏
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你也可以相对当前行进行滚动

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;zt    将当前行置于屏幕顶部附近
zz    将当前行置于屏幕中央
zb    将当前行置于屏幕底部
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;搜索导航&quot;&gt;搜索导航&lt;/h2&gt;

通常，你已经知道这个文件中有一个字段，你可以通过搜索导航非常快速的定位你的目标。 你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;向下搜索，也可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt;向上搜索一个字段。 你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;重复最近一次搜索，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt;向反方向重复最近一次搜索。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;/   向后搜索一个匹配
?   向前搜素一个匹配
n   重复上一次搜索(和上一次方向相同)
N   重复上一次搜索(和上一次方向相反)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

假设你有一下文本：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;let one = 1;
let two = 2;
one = &quot;01&quot;;
one = &quot;one&quot;;
let onetwo = 12;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/let&lt;/code&gt;搜索”let”，然后通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;快速的重复搜索下一个”let”，如果需要向相反方向搜索，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt;。 如果你用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?let&lt;/code&gt;搜索，会得到一个向前的搜索，这时你使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;，它会继续向前搜索，就和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?&lt;/code&gt;的方向一致。(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt;将会向后搜索”let”)。

你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:set hlsearch&lt;/code&gt;设置搜索高亮。 这样，当你搜索&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/let&lt;/code&gt;，它将高亮文件中所有匹配的字段。 另外，如果你通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:set incsearch&lt;/code&gt;设置了增量搜索，它将在你输入时不断匹配的输入的内容。 默认情况下，匹配的字段会一直高亮到你搜索另一个字段，这有时候很烦人，如果你希望取消高亮，可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:nohlsearch&lt;/code&gt;。 因为我经常使用这个功能，所以我会设置一个映射：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nnoremap &amp;lt;esc&amp;gt;&amp;lt;esc&amp;gt; :noh&amp;lt;return&amp;gt;&amp;lt;esc&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;快速的向前搜索光标下的文本，通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#&lt;/code&gt;快速向后搜索光标下的文本。 如果你的光标位于一个字符串”one”上，按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;相当于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/\&amp;lt;one\&amp;gt;&lt;/code&gt;。
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/\&amp;lt;one\&amp;gt;&lt;/code&gt;中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&amp;lt;&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;\&amp;gt;&lt;/code&gt;表示整词匹配，使得一个更长的包含”one”的单词不会被匹配上，也就是说它会匹配”one”，但不会匹配”onetwo”。 如果你的光标在”one”上并且你想向后搜索完全或部分匹配的单词，例如”one”和”onetwo”，你可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;g*&lt;/code&gt;替代&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;*   向后查找光标所在的完整单词
#   向前查找光标所在的完整单词
g*  向后搜索光标所在的单词
g#  向前搜索光标所在的单词
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;位置标记&quot;&gt;位置标记&lt;/h2&gt;

你可以通过标记保存当前位置并在之后回到这个位置，就像文本编辑中的书签。 你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mx&lt;/code&gt;设置一个标记，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;可以是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a-zA-Z&lt;/code&gt;。 有两种办法能回到标记的位置： 用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;`x&lt;/code&gt;精确回到(行和列)，或者用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;x&lt;/code&gt;回到行级位置。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;ma    用a标签标记一个位置
`a    精确回到a标签的位置(行和列)
&apos;a    跳转到a标签的行
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a-z&lt;/code&gt;的标签和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A-Z&lt;/code&gt;的标签存在一个区别，小写字母是局部标签，大写字母是全局标签(也称文件标记)。

我们首先说说局部标记。 每个buffer可以有自己的一套局部标记，如果打开了两个文件，我可以在第一个文件中设置标记”a”(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ma&lt;/code&gt;)，然后在另一个文件中设置另一个标记”a”(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ma&lt;/code&gt;)。

不像你可以在每个buffer中设置一套局部标签，你只能设置一套全局标签。 如果你在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;myFile.txt&lt;/code&gt;中设置了标签&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mA&lt;/code&gt;，下一次你在另一个文件中设置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mA&lt;/code&gt;时，A标签的位置会被覆盖。 全局标签有一个好处就是，即使你在不同的项目中，你也可以跳转到任何一个全局标签上，全局标签可以帮助你在文件间切换。

使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:marks&lt;/code&gt;查看所有标签，你也许会注意到除了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a-zA-Z&lt;/code&gt;以外还有别的标签，其中有一些例如：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&apos;&apos;   在当前buffer中跳转回到上一次跳转前的最后一行
``  在当前buffer中跳转回到上一次跳转前的最后一个位置
`[  跳转到上一次修改或拷贝的文本的开头
`]  跳转到上一次修改或拷贝的文本的结尾
`&amp;lt;  跳转到最近一次可视模式下选择的部分的开头
`&amp;gt;  跳转到最近一次可视模式下选择的部分的结尾
`0  跳转到退出Vim前编辑的最后一个文件
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

除了上面列举的，还有更多标记，我不会在这一一列举因为我觉得它们很少用到，不过如果你很好奇，你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;: marks&lt;/code&gt;查看。

&lt;h2 id=&quot;跳转&quot;&gt;跳转&lt;/h2&gt;

最后，我们聊聊Vim中的跳转你通过任意的移动可以在不同文件中或者同一个的文件的不同部分间跳转。 然而并不是所有的移动都被认为是一个跳转。 使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;j&lt;/code&gt;向下移动一行就不被看做一个跳转，即使你使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10j&lt;/code&gt;向下移动10行，也不是一个跳转。 但是你通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10G&lt;/code&gt;去往第10行被算作一个跳转。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&apos;   跳转到标记的行
`   跳转到标记的位置(行和列)
G   跳转到行
/   向后搜索
?   向前搜索
n   重复上一次搜索，相同方向
N   重复上一次搜索，相反方向
%   查找匹配
(   跳转上一个句子
)   跳转下一个句子
{   跳转上一个段落
}   跳转下一个段落
L   跳转到当前屏幕的最后一行
M   跳转到当前屏幕的中间
H   跳转到当前屏幕的第一行
[[  跳转到上一个小节
]]  跳转到下一个小节
:s  替换
:tag  跳转到tag定义
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我不建议你把上面这个列表记下来，一个大致的规则是，任何大于一个单词或超过当前行导航的移动都可能是一个跳转。 Vim保留了你移动前位置的记录，你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:jumps&lt;/code&gt;查看这个列表，如果想了解更多，可以查看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h jump-motions&lt;/code&gt;。

为什么跳转有用呢? 因为你可以在跳转列表中通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-o&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-i&lt;/code&gt;在记录之间向上或向下跳转到对应位置。 你可以在不同文件中进行跳转，这将是我之后会讲的部分。

&lt;h2 id=&quot;聪明地学习导航&quot;&gt;聪明地学习导航&lt;/h2&gt;

如果你是Vim的新手，这有很多值得你学，我不期望任何人能够立刻记住每样知识点，做到不用思考就能执行这需要一些时间。

我想，最好的开始的办法就是从一些少量的必要的移动开始记。 我推荐你从&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h,j,k,l,w,b,G,/,?,n&lt;/code&gt;开始，不断地重复这10个移动直到形成肌肉记忆，这花不了多少时间。

为了让你更擅长导航，我有两个建议：
&lt;ol&gt;
  &lt;li&gt;注意重复的动作。 如果你发现你自己在重复的使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;l&lt;/code&gt;，你可以去找一个方法让你前进的更快，然后你会发现你可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt;在单词间移动。 如果你发现你自己的重复的使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt;，你可以看看是否有一种方法能让你直接到行尾，然后你会想到可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt;。 如果你可以口语化的表达你的需求，Vim中大概就会有一种方法去完成它。&lt;/li&gt;
  &lt;li&gt;当你学习任何一个新的移动时，多需要花一定的时间直到你可以不经过思考直接完成它。&lt;/li&gt;
&lt;/ol&gt;

最后，为了提高效率你不需要知道所有的Vim的命令，大多数Vim用户也都不知道，你只需要学习当下能够帮助你完成任务的命令。

慢慢来，导航技巧是Vim中很重要的技巧，每天学一点并且把它学好。

</description>
        <pubDate>Mon, 27 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch05-moving-in-file/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch05-moving-in-file/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>移动</category>
        
        <category>导航</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第四章：Vim 语法</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

刚接触Vim时很容易被Vim许多复杂的命令吓到，如果你看到一个Vim的用户使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gUfV&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1GdG&lt;/code&gt;，你可能不能立刻想到这些命令是在做什么。这一章中，我将把Vim命令的结构拆分成一个简单的语法规则进行讲解。

这一章将是本书中最重要的一章，一旦你理解了Vim命令的语法结构，你将能够和Vim”说话”。注意，在这一章中当我讨论Vim语言时，我讨论并不是Vimscript(Vim自带的插件编写和自定义设置的语言)，这里我讨论的是Vim中normal模式的下的命令的通用规则。

&lt;h2 id=&quot;如何学习一门语言&quot;&gt;如何学习一门语言&lt;/h2&gt;

我并不是一个英语为母语的人，当我13岁移民到美国时我学习的英语，我会通过做三件事情建立我的语言能力：
&lt;ol&gt;
  &lt;li&gt;学习语法规则&lt;/li&gt;
  &lt;li&gt;扩展我的词汇量&lt;/li&gt;
  &lt;li&gt;练习，练习，练习&lt;/li&gt;
&lt;/ol&gt;

同样的，为了说好Vim语言，你需要学习语法规则，增加词汇量，并且不断练习直到你可以把执行命令变成肌肉记忆。

&lt;h2 id=&quot;语法规则&quot;&gt;语法规则&lt;/h2&gt;

你只需要知道一个Vim语言的语法规则：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;verb + noun # 动词 + 名词
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这就类似与在英语中的祈使句：
&lt;ul&gt;
  &lt;li&gt;“Eat(verb) a donut(noun)”&lt;/li&gt;
  &lt;li&gt;“Kick(verb) a ball(noun)”&lt;/li&gt;
  &lt;li&gt;“Learn(verb) the Vim Editor(noun)”&lt;/li&gt;
&lt;/ul&gt;

现在你需要的就是用Vim中基本的动词和名字来建立你的词汇表

&lt;h2 id=&quot;名词动作-motion&quot;&gt;名词(动作 Motion)&lt;/h2&gt;

我们这里将 &lt;strong&gt;动作 Motion&lt;/strong&gt; 作为名词， &lt;strong&gt;动作Motion&lt;/strong&gt;用来在Vim中到处移动。下面列出了一些常见的&lt;strong&gt;动作&lt;/strong&gt;的例子：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;h	左
j	下
k	上
l	右
w	向前移动到下一个单词的开头
}	跳转到下一个段落
$	跳转到当前行的末尾
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

在之后的章节你将学习更多的关于&lt;strong&gt;动作&lt;/strong&gt;的内容，所以如果你不理解上面这些&lt;strong&gt;动作&lt;/strong&gt;也不必担心。

&lt;h2 id=&quot;动词操作符-operator&quot;&gt;动词(操作符 Operator)&lt;/h2&gt;
根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h operator&lt;/code&gt;，Vim共有16个&lt;strong&gt;操作符&lt;/strong&gt;，然而根据我的经验，学习这3个&lt;strong&gt;操作符&lt;/strong&gt;在80%的情况下就已经够用了
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;y	yank(复制)
d	delete(删除)
c	change 删除文本，将删除的文本存到寄存器中，进入插入模式
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

顺带说一句，当你yank一段文本后，您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;将它粘贴到光标后，或使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;P&lt;/code&gt;粘贴到光标前。

&lt;h2 id=&quot;动词操作符-operator和名词动作-motions的结合&quot;&gt;动词（操作符 Operator）和名词（动作 motions）的结合&lt;/h2&gt;

现在你已经知道了基本的动词和名词，我们来用一下我们的语法规则，动词和名词的结合！假设你有下面这段文本：

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;learn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Vim&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;复制当前位置到行尾的所有内容：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y$&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;删除当前位置到下一个单词的开头：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dw&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;修改当前位置到这个段落的结尾：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c}&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;strong&gt;动作 motions&lt;/strong&gt;也接受数字作为参数(这个部分我将在下个章节展开)，如果你需要向上移动3行，你可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3k&lt;/code&gt;代替按3次&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;k&lt;/code&gt;，数字可应用在Vim语法中。

&lt;ul&gt;
  &lt;li&gt;向左拷贝2个字符：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y2h&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;删除后两个单词：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d2w&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;修改后面两行：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c2j&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

目前，你也许需要想很久才能完成一个简单的命令，不过我刚开始时也是这样，我也经历过类似的挣扎的阶段但是不久我的速度就快了起来，你也一样。唯一途径就是重复、重复再重复。

作为补充，行级的 &lt;strong&gt;操作符 operations&lt;/strong&gt; (作用在整行中的操作符)在文本编辑中和其他的 &lt;strong&gt;操作符&lt;/strong&gt; 一样，Vim允许你通过按两次 &lt;strong&gt;操作符&lt;/strong&gt;使它执行行级的操作，例如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dd&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;yy&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;cc&lt;/code&gt;来执行删除，复制或修改整个行。您可以使用其他operations试一下（比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gUgU&lt;/code&gt;）。

从这可以看出Vim命令的一种执行模式。但是到目前为止还没有结束，Vim有另一种类型的名词：文本对象(text object)

&lt;h2 id=&quot;更多名词文本对象-text-objects&quot;&gt;更多名词(文本对象 Text Objects)&lt;/h2&gt;

想象一下你现在正在某个被括号包围的文本中例如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(hello Vim)&lt;/code&gt;，你现在想要删掉括号中的所有内容，你会怎样快速的完成它？是否有一种方法能够把括号中内容作为整体删除呢？

答案是有的。文本通常是结构化的，特别是代码中，文本经常被放置在小括号、中括号、大括号、引号等当中。Vim提供了一种处理这种结构的文本对象的方法。

文本对象可以被 &lt;strong&gt;操作符 operations&lt;/strong&gt; 使用，这里有两类文本对象：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;i + object  内部文本对象
a + object  外部文本对象
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;strong&gt;内部文本对象&lt;/strong&gt;选中的部分不包含包围文本对象的空白或括号等，&lt;strong&gt;外部文本对象&lt;/strong&gt;则包括了包围内容的空白或括号等对象。外部对象总是比内部对象选中的内容更多。如果你的光标位于一对括号内部，例如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(hello Vim)&lt;/code&gt;中：
&lt;ul&gt;
  &lt;li&gt;删除括号内部的内容但保留括号：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;di(&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;删除括号以及内部的内容：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;da(&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

让我们看一些别的例子，假设你有这样一段Javascript的函数，你的光标停留在”Hello”中的”H”上：

&lt;div class=&quot;language-javascript highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;const&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;hello&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;console&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;Hello Vim&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;删除整个”Hello Vim”：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;di(&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;删除整个函数(被{}包含)：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;di{&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;删除”Hello”这个词：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diw&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

文本对象很强大因为你可以在同一个位置指向不同的内容，可以删除一对小括号中的文本，也可以是当前大括号中的函数体，也可以是当前单词。这一点也很好记忆，当你看到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;di(&lt;/code&gt;，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;di{&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;diw&lt;/code&gt;时，你也可以很好的意识到他们表示的是什么：小括号，大括号，单词。

让我们来看最后一个例子。假设你有这样一些html的标签的文本：

&lt;div class=&quot;language-html highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Header1&lt;span class=&quot;nt&quot;&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Paragraph1&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nt&quot;&gt;&amp;lt;p&amp;gt;&lt;/span&gt;Paragraph2&lt;span class=&quot;nt&quot;&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果你的光标位于”Header1”文本上：
&lt;ul&gt;
  &lt;li&gt;删除”Header1”：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dit&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;删除&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;h1&amp;gt;Header1&amp;lt;/h1&amp;gt;&lt;/code&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dat&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

如果你的光标在”div”文本上：
&lt;ul&gt;
  &lt;li&gt;删除&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;h1&lt;/code&gt;和所有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;标签的行：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dit&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;删除所有文本：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dat&lt;/code&gt;&lt;/li&gt;
  &lt;li&gt;删除”div”：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;di&amp;lt;&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

下面列举的一些通常见到的文本对象：
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;w     一个单词
p     一个段落
s     一个句子
(或)  一对()
{或}  一对{}
[或]  一对[]
&amp;lt;或&amp;gt;  一对&amp;lt;&amp;gt;
t     XML标签
&quot;     一对&quot;&quot;
&apos;     一对&apos;&apos;
`     一对``
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

你可以通过&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h text-objects&lt;/code&gt;了解更多

&lt;h2 id=&quot;结合性和语法&quot;&gt;结合性和语法&lt;/h2&gt;

在学习Vim的语法之后，让我们来讨论一下Vim中的结合性以及为什么在文本编辑器中这是一个强大的功能。

结合性意味着你有很多可以组合起来完成更复杂命令的普通命令，就像你在编程中可以通过一些简单的抽象建立更复杂的抽象，在Vim中你可以通过简单的命令的组合执行更复杂的命令。Vim语法正是Vim中命令的可结合性的体现。

Vim的结合性最强大之处体现在它和外部程序结合时，Vim有一个 &lt;strong&gt;过滤操作符&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;可以用外部程序过滤我们的文本。假设你有下面这段混乱的文本并且你想把它用tab格式化的更好看的一些：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Id|Name|Cuteness
01|Puppy|Very
02|Kitten|Ok
03|Bunny|Ok
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这件事情通过Vim命令不太容易完成，但是你可以通过终端提供的命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;column&lt;/code&gt;很快的完成它，当你的光标位于”Id”上时，运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!}column -t -s &quot;|&quot;&lt;/code&gt;，你的文本就变得整齐了许多：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Id  Name    Cuteness
01  Puppy   Very
02  Kitten  Ok
03  Bunny   Ok
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

让我们分解一下上面那条命令，动词是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;(&lt;strong&gt;过滤操作符&lt;/strong&gt;)，名词是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;}&lt;/code&gt;(到下一个段落)。&lt;strong&gt;过滤操作符&lt;/strong&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;接受终端命令作为另一个参数，因此我把&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;column -t -s &quot;|&quot;&lt;/code&gt;传给它。我不想详细描述&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;column&lt;/code&gt;是如何工作的，但是总之它格式化了文本。

假设你不止想格式化你的文本，还想只展示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ok&lt;/code&gt;结尾的行，你知道&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;awk&lt;/code&gt;命令可以做这件事情，那么你可以这样做：
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;!}column -t -s &quot;|&quot; | awk &apos;NR &amp;gt; 1 &amp;amp;&amp;amp; /Ok/{print $0}&apos;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

结果如下：
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;02  Kitten  Ok
03  Bunny   Ok
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

管道竟然在Vim中也能起作用。

这就是Vim的结合性的强大之处。你知道的动词 &lt;strong&gt;操作符&lt;/strong&gt;，名词 &lt;strong&gt;动作&lt;/strong&gt;，终端命令越多，你组建复杂操作的能力成倍增长。

换句话说，假设你只知道四个&lt;strong&gt;动作&lt;/strong&gt;：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w, $, }, G&lt;/code&gt;和删除操作符(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;d&lt;/code&gt;)，你可以做8件事：按四种方式移动(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w, $, }, G&lt;/code&gt;)和删除4种文本对象(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dw, d$, d}, dG&lt;/code&gt;)。如果有一天你学习了小写变大写的&lt;strong&gt;操作符&lt;/strong&gt;(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gU&lt;/code&gt;)，你的Vim工具箱中多的不是1种工具，而是4种：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gUw, gU$, gU}, gUG&lt;/code&gt;。现在你的Vim工具箱中就有12种工具了。如果你知道10个&lt;strong&gt;动作&lt;/strong&gt;和5个&lt;strong&gt;操作符&lt;/strong&gt;，那么你就有60种工具(50个操作+10个移动)。另外，行号动作(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nG&lt;/code&gt;)给你了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;种&lt;strong&gt;动作&lt;/strong&gt;，其中&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;是你文件中的行数(例如前往第5行，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5G&lt;/code&gt;)。搜索动作(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/&lt;/code&gt;)实际上给你带来无限数量的&lt;strong&gt;动作&lt;/strong&gt;因为你可以搜索任何内容。你知道多少终端命令，外部命令操作符(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;)就给你了多少种过滤工具。使用Vim这种能够组合的工具，所有你知道的东西都可以被串起来完成更复杂的操作。你知道的越多，你就越强大。

这种具有结合性的行为也正符合Unix的哲学：&lt;em&gt;一个命令做好一件事&lt;/em&gt;。&lt;strong&gt;动作&lt;/strong&gt;只需要做一件事：前往X。&lt;strong&gt;操作符&lt;/strong&gt;只需要做一件事：完成Y。通过结合一个&lt;strong&gt;操作符&lt;/strong&gt;和一个&lt;strong&gt;动作&lt;/strong&gt;，你就获得了YX：在X上完成Y。

甚至，&lt;strong&gt;动作&lt;/strong&gt;和&lt;strong&gt;操作符&lt;/strong&gt;都是可拓展的，你可以自己创造&lt;strong&gt;动作&lt;/strong&gt;和&lt;strong&gt;操作符&lt;/strong&gt;去丰富你的Vim工具箱，&lt;a href=&quot;https://github.com/kana/vim-textobj-user&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Vim-textobj-user&lt;/code&gt;&lt;/a&gt;插件允许你创建自己的文本对象，同时包含有&lt;a href=&quot;https://github.com/kana/vim-textobj-user/wiki&quot;&gt;一系列定义好的文本对象&lt;/a&gt;。

另外，如果你不知道我刚才使用的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;column&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;awk&lt;/code&gt;命令也没有关系，重要的是Vim可以和终端命令很好的结合起来。

&lt;h2 id=&quot;聪明地学习语法&quot;&gt;聪明地学习语法&lt;/h2&gt;

你刚刚学完Vim唯一的语法规则：
&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;verb + noun
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;
我学Vim中最大的”AHA moment”之一是当我刚学完大写命令(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gU&lt;/code&gt;)时，想要把一个单词变成大写，我本能的运行了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gUiW&lt;/code&gt;，它居然成功了，我光标所在的单词都大写了。我正是从那是开始理解Vim的。我希望你也会在不久之后有你自己的”AHA moment”，如果之前没有的话。

这一章的目标是向你展现Vim中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;verb+noun&lt;/code&gt;模式，因此之后你就可以像学习一门新的语言一样渐进的学习Vim而不是死记每个命令的组合。

学习这种模式并且理解其中的含义，这是聪明的学习方式。

</description>
        <pubDate>Sun, 26 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch04-vim-grammar/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch04-vim-grammar/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>语法</category>
        
        <category>操作符</category>
        
        <category>动作</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第三章：打开和搜索文件</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

本章的目的是向您介绍如何在Vim中快速搜索，能够快速搜索是提高您的Vim工作效率的重要途径。当我解决了如何快速搜索文件这个问题后，我就决定改为完全使用Vim来工作。

本章划分为两个部分：一是如何不依赖插件搜索；二是使用&lt;a href=&quot;https://github.com/junegunn/fzf.vim&quot;&gt;fzf插件&lt;/a&gt;搜索。让我们开始吧！

&lt;h2 id=&quot;打开和编辑文件&quot;&gt;打开和编辑文件&lt;/h2&gt;

要在Vim中打开一个文件，您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:edit&lt;/code&gt;。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:edit file.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file.txt&lt;/code&gt;已经存在，就会打开&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file.txt&lt;/code&gt;buffer。如果&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file.txt&lt;/code&gt;不存在，会创建一个新buffer名为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file.txt&lt;/code&gt;。

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:edit&lt;/code&gt;命令支持使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;进行自动补全。比如，如果您的文件位于&lt;a href=&quot;https://rubyonrails.org/&quot;&gt;Rails&lt;/a&gt;应用控制器的用户控制器目录&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./app/controllers/users_controllers.rb&lt;/code&gt;内，您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;对文件路径名进行快速扩展。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:edit a&amp;lt;Tab&amp;gt;c&amp;lt;Tab&amp;gt;u&amp;lt;Tab&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:edit&lt;/code&gt;可以接收通配符参数。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt;匹配当前目录下的任意文件。如果您只想查找当前目录下后缀名为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.yml&lt;/code&gt;的文件：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:edit *.yml&amp;lt;Tab&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim将列出当前目录下所有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.yml&lt;/code&gt;文件供您选择。

您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;**&lt;/code&gt;进行递归的搜索。如果您想查找当前项目文件夹下所有&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*.md&lt;/code&gt;文件，但您不知道在哪个目录，您可以这样做：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:edit **/*.md&amp;lt;Tab&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:edit&lt;/code&gt;可以用于运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netrw&lt;/code&gt;（Vim的内置文件浏览器）。使用方法是，给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:edit&lt;/code&gt;一个目录参数而不是文件名就行了：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:edit .
:edit test/unit/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;使用find命令搜索文件&quot;&gt;使用find命令搜索文件&lt;/h2&gt;

您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:find&lt;/code&gt;命令搜索文件。比如：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:find package.json
:find app/controllers/users_controller.rb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:find&lt;/code&gt;命令同样支持自动补全：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:find p&amp;lt;Tab&amp;gt;                &quot; to find package.json
:find a&amp;lt;Tab&amp;gt;c&amp;lt;Tab&amp;gt;u&amp;lt;Tab&amp;gt;    &quot; to find app/controllers/users_controller.rb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您可能注意到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:find&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:edit&lt;/code&gt;看起来很像。它们的区别是什么呢？

&lt;h2 id=&quot;find-和-path&quot;&gt;Find 和 Path&lt;/h2&gt;

两者的区别在于，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:find&lt;/code&gt;命令根据&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;选项配置的路径查找文件，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:edit&lt;/code&gt;不会。让我们了解一点关于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;选项的知识。一旦您学会如何修改您的路径，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:find&lt;/code&gt;命令能变成一个功能强大的搜索工具。先查看一下您的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;是什么：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set path?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

默认情况下，您的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;内容很可能是这样的：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;path=.,/usr/include,,
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt; 意思是在当前文件所在目录下搜索。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;,&lt;/code&gt; 意思是在当前目录下搜索。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/usr/include&lt;/code&gt; 表示在C编译器头文件目录下搜索。&lt;/li&gt;
&lt;/ul&gt;

前两个配置非常重要，第3个现在可以被忽略。您这里应该记住的是：您可以修改您自己的路径。让我们假设您的项目结构是这样的：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;app/
  assets/
  controllers/
    application_controller.rb
    comments_controller.rb
    users_controller.rb
    ...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果您想从根目录跳到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;users_controller.rb&lt;/code&gt;，您将不得不经过好几层目录（按好几次&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;）。一般说来，当您处理一个framework时，90%的时间您都在某个特定的目录下。在这种情况下，您只关心如何用最少的按键跳到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;controllers/&lt;/code&gt;目录。那么&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;设置可以减少这个途程。

您只需要将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app/controllers/&lt;/code&gt;添加到当前&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;选项。以下是操作步骤：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set path+=app/controllers/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

现在您的路径已经更新，当您输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:find u&amp;lt;Tab&amp;gt;&lt;/code&gt;时，Vim将会在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app/controllers/&lt;/code&gt;目录内搜索所有以”u”开头的文件。

如果您有一个嵌套的目录&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;controllers/&lt;/code&gt;，比如&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app/controllers/account/users_controller.rb&lt;/code&gt;，Vim就找不到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;users_controllers&lt;/code&gt;了。您必须改为添加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:set path+=app/controllers/**&lt;/code&gt;，这样自动补全才会找到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;users_controller.rb&lt;/code&gt;。这太棒了！您现在可以只需要按1次键就可找到users controller。

您可能会想将整个项目文件夹添加到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;中，这样当您按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;，Vim将在所有文件夹内搜索您要找的文件，就像这样：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:set path+=$PWD/**
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$PWD&lt;/code&gt; 表示的是当前工作目录。如果您尝试将整个项目路径加到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;中，并希望让所有文件名可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;补全，虽然对于小项目没问题，但如果您的项目中包含大量文件时，这会显著降低您的搜索速度。我建议仅仅将您最常访问的文件/目录添加到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;。

您可以将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set path+={您需要添加的目录}&lt;/code&gt;添加到您的vimrc文件中。更新&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;path&lt;/code&gt;仅花费几秒钟，但可以为您的工作节省很多时间。

&lt;h2 id=&quot;使用grep命令在文件中搜索&quot;&gt;使用Grep命令在文件中搜索&lt;/h2&gt;

如果您想在文件内部查找（搜索文件中的词句），您可以使用grep。Vim有两个方法可以完成这个工作：

&lt;ul&gt;
  &lt;li&gt;内置grep （&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:vim&lt;/code&gt;。没错，就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:vim&lt;/code&gt;，它是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:vimgrep&lt;/code&gt;的简写）。&lt;/li&gt;
  &lt;li&gt;外部grep (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep&lt;/code&gt;)。&lt;/li&gt;
&lt;/ul&gt;

让我们首先仔细看看内置grep。&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:vim&lt;/code&gt;有以下语法：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:vim /pattern/ file
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/pattern/&lt;/code&gt; 是您要搜索的内容的正则表达式。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file&lt;/code&gt; 是文件参数。您可以传入多个参数。Vim将在文件中搜索所有匹配正则表达式的内容。类似于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:find&lt;/code&gt;，您可以传入*和**通配符。&lt;/li&gt;
&lt;/ul&gt;

比如，要在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app/controllers/&lt;/code&gt;目录下所有ruby文件(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.rb&lt;/code&gt;)中，查找所有的”breakfast”字符串:

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:vim /breakfast/ app/controllers/**/*.rb
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

输入上面的命令后，您将会被导航到第一个结果。Vim的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vim&lt;/code&gt;搜索命令使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quickfix&lt;/code&gt;进行处理。要查看所有搜索结果，运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:copen&lt;/code&gt;会打开一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;quickfix&lt;/code&gt;窗口。下面有一些有用的quickfix命令，可以让您提高效率：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:copen        打开quickfix窗口
:cclose       关闭quickfix窗口
:cnext        跳到下一个错误
:cprevious    跳到前一个错误
:colder       跳到旧的错误列表
:cnewer       跳到新的错误列表
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

要了解更多关于quickfix的信息，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h quickfix&lt;/code&gt;查看帮助信息。

您可能注意到运行内置grep(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:vim&lt;/code&gt;)命令时，如果匹配结果数量较多时系统速度会变慢。这是因为Vim将每一个搜索结果读入内存。Vim加载每一个匹配的文件就像它们被编辑一样。如果Vim查到大量文件，它将消耗很多内存。

让我们谈谈外置grep。默认情况下，它使用终端命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt;。要想在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app/controllers/&lt;/code&gt;目录中搜索字符串”lunch”，您可以这样做：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:grep -R &quot;lunch&quot; app/controllers/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

注意这里不是使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/pattern/&lt;/code&gt;，它遵循的是终端grep的语法&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;pattern&quot;&lt;/code&gt;，它同样使用’quickfix’来显示所有的匹配结果。

Vim使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grepprg&lt;/code&gt;变量来决定运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep&lt;/code&gt;时，应该使用哪个外部程序。所以您并不是非得使用终端的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt;命令。稍后我将为您演示如何改变外部grep程序的默认值。

&lt;h2 id=&quot;用netrw浏览文件&quot;&gt;用Netrw浏览文件&lt;/h2&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netrw&lt;/code&gt;是Vim的内置文件浏览器，当查看项目的目录结构时很有用。要运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netrw&lt;/code&gt;，您需要在您的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.vimrc&lt;/code&gt;中做以下设置：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;set nocp
filetype plugin on
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

由于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netrw&lt;/code&gt;是一个很宽泛的话题，我将仅仅介绍它的基本用法，这应该已经足够了。您可以在启动Vim时运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netrw&lt;/code&gt;，只需要传给Vim一个目录参数（而不是文件参数）就行了。比如：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim .
vim src/client/
vim app/controllers/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

要想从Vim内部运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netrw&lt;/code&gt;，您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:edit&lt;/code&gt;命令，传给他一个目录而不是文件名：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:edit .
:edit src/client/
:edit app/controllers/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

也有其他方法，不需要传递目录参数就运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netrw&lt;/code&gt;窗口：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:Explore     从当前文件启动netrw。
:Sexplore    在顶部水平分割的窗口打开netrw。
:Vexplore    在左侧垂直分割的窗口打开netrw。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您可以使用Vim动作(motions，在后面的章节中将详细讲述)在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netrw&lt;/code&gt;中导航。如果您要创建、删除、重命名文件或文件夹，下面有一些关于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netrw&lt;/code&gt;的有用命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;%    创建新文件
d    创建新目录
R    重命名文件/目录
D    删除文件/目录
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h netrw&lt;/code&gt; 的信息非常复杂，如果您有时间可以看看。

如果您觉得 &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;netrw&lt;/code&gt; 过于单调乏味，&lt;a href=&quot;https://github.com/tpope/vim-vinegar&quot;&gt;vim-vinegar&lt;/a&gt;是netrw的一个改进插件。如果您想找一个不同的文件浏览器，&lt;a href=&quot;https://github.com/preservim/nerdtree&quot;&gt;NERDTree&lt;/a&gt; 是一个很好的选择。去看看吧。

&lt;h2 id=&quot;fzf插件&quot;&gt;Fzf插件&lt;/h2&gt;

您现在已经学会了如何使用Vim的内置工具去搜索文件，那么让我们学习一下如何用插件实现这些功能吧。

现代文本编辑器相比Vim，有一个功能设计得很好，那就是文件搜索和模糊搜索的简化。在本章的下半部分，我将向您演示如何使用&lt;a href=&quot;https://github.com/junegunn/fzf.vim&quot;&gt;fzf.vim&lt;/a&gt;插件，在Vim中轻松实现功能强大的搜索功能。

&lt;h2 id=&quot;安装&quot;&gt;安装&lt;/h2&gt;

首先，确保您下载了&lt;a href=&quot;https://github.com/junegunn/fzf&quot;&gt;fzf&lt;/a&gt;和&lt;a href=&quot;https://github.com/BurntSushi/ripgrep&quot;&gt;ripgrep&lt;/a&gt;。按照它们在github仓库上的指令一步步做。成功安装后，命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fzf&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rg&lt;/code&gt;应该就可以用了。

Ripgrep是一个类似grep（从名字上就看得出）的搜索工具。一般说来，它比grep要快，而且还有很多有用的特性。Fzf是一个多用途的命令行模糊搜索工具，您可以讲它与其他命令联合起来使用，包括ripgrep。联合起来后，它们成为一个功能强大的搜索工具。

Fzf默认并不使用ripgrep，所以我们需要设置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FZF_DEFAULT_COMMAND&lt;/code&gt;变量告诉fzf使用ripgrep命令。在我的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.zshrc&lt;/code&gt;（如果您用bash，应该是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.bashrc&lt;/code&gt;）文件内，我有以下设置：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;if type rg &amp;amp;&amp;gt; /dev/null; then
  export FZF_DEFAULT_COMMAND=&apos;rg --files&apos;
  export FZF_DEFAULT_OPTS=&apos;-m&apos;
fi
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

注意&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FZF_DEFAULT_OPTS&lt;/code&gt;变量中的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-m&lt;/code&gt;。这个设置允许我们按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Shift-Tab&amp;gt;&lt;/code&gt;后进行多重选择。如果仅想让fzf在Vim中能够工作，这个设置不是必须的，但我认为这是一个有用的设置。当您想在多个文件中执行搜索和替换，进行少量修改时，它会很方便。fzf命令可以接收很多标志，但我不会再这里讲。要想了解更多信息，可以查看&lt;a href=&quot;https://github.com/junegunn/fzf#usage&quot;&gt;fzf’s 仓库&lt;/a&gt;，或者使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;man fzf&lt;/code&gt;。要想让fzf使用ripgrep，您至少得有这个设置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;export FZF_DEFAULT_COMMAND=&apos;rg&apos;&lt;/code&gt;。

安装好了fzf和ripgrep后，让我们再安装fzf的插件。在这个例子中，我使用的是&lt;a href=&quot;https://github.com/junegunn/vim-plug&quot;&gt;vim-plug&lt;/a&gt;插件管理器，当然您可以使用其他插件管理器。

将下列配置添加到您的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.vimrc&lt;/code&gt;中。因为您需要使用&lt;a href=&quot;https://github.com/junegunn/fzf.vim&quot;&gt;fzf.vim&lt;/a&gt;插件。（同样是由fzf的作者在维护）

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Plug &apos;junegunn/fzf.vim&apos;
Plug &apos;junegunn/fzf&apos;, { &apos;do&apos;: { -&amp;gt; fzf#install() } }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

添加后，您需要打开vim，运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:PlugInstall&lt;/code&gt;。这条命令将会安装所有您在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vimrc&lt;/code&gt;文件中定义了但尚未安装的插件。 在我的例子中，将会安装&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fzf.vim&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fzf&lt;/code&gt;。

要了解更多关于此插件的信息，您可以查看&lt;a href=&quot;https://github.com/junegunn/fzf/blob/master/README-VIM.md&quot;&gt;fzf.vim 的仓库&lt;/a&gt;。

&lt;h2 id=&quot;fzf的语法&quot;&gt;Fzf的语法&lt;/h2&gt;

要想高效的使用fzf，您首先得了解一些fzf的基础语法。幸运的是，这个列表比较短：

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^&lt;/code&gt; 表示前缀精确匹配。要搜索一个以”welcome”开头的短语：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^welcom&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$&lt;/code&gt; 表示后缀精确匹配。要搜索一个以”my friends”结尾的短语：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;friends$&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;&lt;/code&gt; 表示精确匹配。要搜索短语”welcom my friends”：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&apos;welcom my friends&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&lt;/code&gt; 表示”或者”匹配。要搜索”friends”或”foes”：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;friends | foes&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt; 表示反向匹配。要搜索一个包含”welcome”但不包含”friends”的短语：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;welcome !friends&lt;/code&gt;。&lt;/li&gt;
&lt;/ul&gt;

您可以混合起来使用。比如，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;^hello | ^welcome friends$&lt;/code&gt;将搜索以”welcome”或”hello”开头，并且以”friends”结束的短语。

&lt;h2 id=&quot;查找文件&quot;&gt;查找文件&lt;/h2&gt;

要想在Vim内使用fzf.vim插件搜索文件，您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Files&lt;/code&gt;方法。在Vim中运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Files&lt;/code&gt;，您将看到fzf搜索提示符。

因为您将频繁地使用这个命令，最好建立一个键盘映射，我把它映射到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-f&lt;/code&gt;。在我的vimrc配置中，有这个设置：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nnoremap &amp;lt;silent&amp;gt; &amp;lt;C-f&amp;gt; :Files&amp;lt;CR&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;在文件中查找&quot;&gt;在文件中查找&lt;/h2&gt;

要想在文件内部搜索，您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Rg&lt;/code&gt;命令。

同样，因为您可能将频繁的使用这个命令，让我们给它一个键盘映射。我的映射在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Leader&amp;gt;f&lt;/code&gt;。

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nnoremap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;f :Rg&amp;lt;CR&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;其他搜索&quot;&gt;其他搜索&lt;/h2&gt;

Fzf.vim提供了许多其他命令。这里我不会一个个仔细讲，您可以去&lt;a href=&quot;https://github.com/junegunn/fzf.vim#commands&quot;&gt;这里&lt;/a&gt;查看更多信息。

这是我的fzf键盘映射：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;nnoremap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;b :Buffers&amp;lt;CR&amp;gt;
nnoremap &amp;lt;silent&amp;gt; &amp;lt;C-f&amp;gt; :Files&amp;lt;CR&amp;gt;
nnoremap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;f :Rg&amp;lt;CR&amp;gt;
nnoremap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;/ :BLines&amp;lt;CR&amp;gt;
nnoremap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;&apos; :Marks&amp;lt;CR&amp;gt;
nnoremap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;g :Commits&amp;lt;CR&amp;gt;
nnoremap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;H :Helptags&amp;lt;CR&amp;gt;
nnoremap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;hh :History&amp;lt;CR&amp;gt;
nnoremap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;h: :History:&amp;lt;CR&amp;gt;
nnoremap &amp;lt;silent&amp;gt; &amp;lt;Leader&amp;gt;h/ :History/&amp;lt;CR&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;将grep替换为rg&quot;&gt;将Grep替换为Rg&lt;/h2&gt;

正如前面提到的，Vim有两种方法在文件内搜索：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:vim&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep&lt;/code&gt;。您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grepprg&lt;/code&gt;这个关键字重新指定&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep&lt;/code&gt;使用的外部搜索工具。我将向您演示如何设置Vim，使得当运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep&lt;/code&gt;命令时，使用ripgrep代替终端的grep。

现在，让我们设置&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grepprg&lt;/code&gt;来使&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep&lt;/code&gt;使用ripgrep。将下列设置添加到您的vimrc：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;set grepprg=rg\ --vimgrep\ --smart-case\ --follow
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

上面的一些选项可以随意修改！要想了解更多关于这些选项的含义，请使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;man rg&lt;/code&gt;了解详情。

当您更新&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grepprg&lt;/code&gt;选项后，现在当您运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep&lt;/code&gt;，它将实际运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rg --vimgrep --smart-case --follow&lt;/code&gt;而不是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep&lt;/code&gt;。如果您想使用ripgrep搜索”donut”，您可以运行一条更简洁的命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep &quot;donut&quot;&lt;/code&gt;，而不是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep &quot;donut&quot; . -R&lt;/code&gt;

就像老的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep&lt;/code&gt;一样，新的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep&lt;/code&gt;同样使用quickfix窗口来显示结果。

&lt;h2 id=&quot;在多文件中搜索和替换&quot;&gt;在多文件中搜索和替换&lt;/h2&gt;

现代文本编辑器，比如VSCode中，在多个文件中搜索和替换一个字符串是很简单的事情。在这一节，我将向您演示如何在Vim中轻松实现这个。

第一个方法是在您的项目中替换 &lt;strong&gt;所有&lt;/strong&gt; 的匹配短句。您得使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep&lt;/code&gt;命令。如果您想将所有”pizza”替换为”donut”，下面是操作方法：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:grep &quot;pizza&quot;
:cfdo %s/pizza/donut/g | update
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

让我们来分析一下这条命令：

&lt;ol&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:grep pizza&lt;/code&gt;使用ripgrep去搜索所有”pizza”。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:cfdo&lt;/code&gt;会在您的quickfix列表中所有文件里，执行您传递给它的命令。在这个例子中，您的命令是一条替换命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;%s/pizza/donut/g&lt;/code&gt;。管道符号(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;|&lt;/code&gt;)是一个链接操作符。命令&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;update&lt;/code&gt;在每个文件被替换后，立刻保存。&lt;/li&gt;
&lt;/ol&gt;

第二个方法是在您选择文件中执行搜索和替换。用这个方法，您可以手动选择您想执行搜索和替换的文件。下面是操作方法：

&lt;ol&gt;
  &lt;li&gt;首先清空您的buffer。让您的buffer列表仅包含您所需要的文件，这一点很有必要。您可以重启Vim，也可以运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:%bd | e#&lt;/code&gt;命令。&lt;/li&gt;
  &lt;li&gt;运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:Files&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;选择好您想搜索-替换的文件。要选择多个文件，使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Shift-Tab&amp;gt;&lt;/code&gt;。&lt;/li&gt;
  &lt;li&gt;运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:bufdo %s/pizza/donut/g | update&lt;/code&gt;。区别在于，(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:cfdo&lt;/code&gt;)替换所有quickfix中的实体，而(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:bufdo&lt;/code&gt;)替换所有buffer中的实体。&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;用聪明的方法学习搜索&quot;&gt;用聪明的方法学习搜索&lt;/h2&gt;

在文本编辑时，搜索是一个很实用的技巧。学会在Vim中如何搜索，将显著提高您的文本编辑工作流程效率。

Fzf.vim插件就像一个游戏规则改变者。我无法想象使用Vim没有它的情景。当最开始使用Vim时，如果有一个好的搜索工具，我想是非常重要的。我看见很多人过渡到Vim时的艰难历程，就是因为Vim缺少了现代编辑器所拥有的一些关键功能特性，比如简单快捷且功能强大的搜索功能。我希望本章将帮助您更轻松地向Vim过渡。

</description>
        <pubDate>Sat, 25 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch03-searching-files/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch03-searching-files/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>fzf</category>
        
        <category>搜索</category>
        
        <category>ripgrep</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第二章：缓冲区、窗口和标签页</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

（译者注：在Vim中，Buffers缓冲区，Windows窗口，Tabs选项卡是专有名词。为适应不同读者的翻译习惯，确保没有歧义，本文将不对Buffers、Windows、Tabs这三个词进行翻译）。

如果您使用过现代文本编辑器，您很可能对Windows和tabs这两个概念是非常熟悉的。但Vim使用了三个关于显示方面的抽象概念：buffers, windows, 还有tabs。在本章，我将向您解释什么是buffers, windows和tabs，以及它们在Vim中如何工作。

在开始之前，确保您的vimrc文件中开启了&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set hidden&lt;/code&gt;选项。若没有配置该选项，当您想切换buffer且当前buffer没有保存时，Vim将提示您保存文件（如果您想快速切换，您不会想要这个提示）。我目前还没有讲vimrc，如果您没有vimrc配置文件，那就创建一个。它通常位于根目录下，名字叫&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.vimrc&lt;/code&gt;。我的vimrc位于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vimrc&lt;/code&gt;。要查看您自己的vimrc文件应该放置在哪，可以在Vim命令模式中输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h vimrc&lt;/code&gt;。在vimrc文件中，添加：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;set hidden
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

保存好vimrc文件，然后激活它(在vimrc文件中运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:source %&lt;/code&gt;)。

&lt;h2 id=&quot;buffers&quot;&gt;Buffers&lt;/h2&gt;

&lt;em&gt;buffer&lt;/em&gt;到底是什么？

buffer就是内存中的一块空间，您可以在这里写入或编辑文本。当您在Vim中打开一个文件时，文件的数据就与一个buffer绑定。当您在Vim中打开3个文件，您就有3个buffers。

创建两个可使用的空文件，分别名为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;（如果可能，尽量使用Vim来创建）。在终端中运行下面的命令：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim file1.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

这时您看到的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;的 &lt;em&gt;buffer&lt;/em&gt; 。每当您打开一个新文件，Vim总是会创建一个新的buffer。

退出Vim。这一次，打开两个新文件：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim file1.js file2.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim当前显示的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;的buffer，但它实际上创建了两个buffers：&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;buffer和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;buffer。运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:buffers&lt;/code&gt;命令可以查看所有的buffers（另外，您也可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:ls&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:files&lt;/code&gt;命令）。您应该会 &lt;em&gt;同时&lt;/em&gt; 看到列出来的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;。运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vim file1 file2 file3 ... filen&lt;/code&gt;创建n个buffers。每一次您打开一个新文件，Vim就为这个文件创建一个新的buffer。

要遍历所有buffers，有以下几种方法：

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:bnext&lt;/code&gt; 切换至下一个buffer（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:bprevious&lt;/code&gt;切换至前一个buffer）。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:buffer&lt;/code&gt; + 文件名。（按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;键Vim会自动补全文件名）。&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:buffer&lt;/code&gt; + &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt;, n是buffer的编号。比如，输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:buffer 2&lt;/code&gt;将使您切换到buffer #2。&lt;/li&gt;
  &lt;li&gt;按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-O&lt;/code&gt;将跳转至跳转列表中旧的位置，对应的，按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-I&lt;/code&gt;将跳转至跳转列表中新的位置。这并不是属于buffer的特有方法，但它可以用来在不同的buffers中跳转。我将在第5章详细讲述关于跳转的知识。&lt;/li&gt;
  &lt;li&gt;按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-^&lt;/code&gt;跳转至先前编辑过的buffer。&lt;/li&gt;
&lt;/ul&gt;

一旦Vim创建了一个buffer，它将保留在您的buffers列表中。若想删除它，您可以输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:bdelete&lt;/code&gt;。这条命令也可以接收一个buffer编号（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:bdelete 3&lt;/code&gt;将删除buffer #3）或一个文件名（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:bdelete&lt;/code&gt;然后按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;自动补全文件名）。

我学习buffer时最困难的事情就是理解buffer如何工作，因为我当时的思维已经习惯了使用主流文本编辑器时关于窗口的概念。要理解buffer，可以打个很好的比方，就是打牌的桌面。如果您有2个buffers，就像您有一叠牌（2张）。您只能看见顶部的牌，虽然您知道在它下面还有其他的牌。如果您看见&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;buffer，那么&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;就是顶部的牌。虽然您看不到其他的牌&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;，但它实际上就在那。如果您切换buffers到&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;，那么&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;这张牌就换到了顶部，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;就换到了底部。

如果您以前没有用过Vim，这是一个新的概念。花上几分钟理解一下。

&lt;h2 id=&quot;退出vim&quot;&gt;退出Vim&lt;/h2&gt;

顺带说一句，如果您已经打开了多个buffers，您可以使用quit -all来关闭所有的buffers：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:qall
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果您想关闭所有buffers但不保存，仅需要在后面加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;!&lt;/code&gt;（叹号）就行了：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:qall!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

若要保存所有buffers然后退出，请运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:wqall
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;windows&quot;&gt;Windows&lt;/h2&gt;

一个window就是在buffer上的一个视口。如果您使用过主流的编辑器，Windows这个概念应该很熟悉。大部分文本编辑器具有显示多个窗口的能力。在Vim中，您同样可以拥有多个窗口。

让我们从终端再次打开&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim file1.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

先前我说过，您看到的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;的buffer。但这个说法并不完整，现在这句话得更正一下，您看到的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js &lt;/code&gt;的buffer通过 &lt;strong&gt;一个窗口&lt;/strong&gt; 显示出来。窗口就是您查看的buffer所使用的视口。

先不忙急着退出Vim，在Vim中运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:split file2.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

现在您看到的是两个buffers通过 &lt;strong&gt;两个窗口&lt;/strong&gt; 显示出来。上面的窗口显示的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;的buffer。而下面的窗口显示的是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;的buffer。

如果您想在窗口之间导航，使用这些快捷键：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-W H    移动光标到左边的窗口
Ctrl-W J    移动光标到下面的窗口
Ctrl-W K    移动光标到上面的窗口
Ctrl-W L    移动光标到右边的窗口
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

现在，在Vim中运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:vsplit file3.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您现在看到的是三个窗口显示三个buffers。一个窗口显示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file3.js&lt;/code&gt;的buffer，一个窗口显示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;的buffer，还有一个窗口显示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;的buffer。

您可以使多个窗口显示同一个buffer。当光标位于左上方窗口时，输入：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:buffer file2.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

现在两个窗口显示的都是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;的buffer。如果您现在在这两个窗口中的某一个输入内容，您会看到所有显示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;buffer的窗口都在实时更新。

要关闭当前的窗口，您可以按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-W C&lt;/code&gt;或输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:quit&lt;/code&gt;。当您关闭一个窗口后，buffers仍然会在列表中。（可以运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:buffers&lt;/code&gt;来确认这一点）。

这里有一些普通模式下关于窗口的命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Ctrl-W V    打开一个新的垂直分割的窗口
Ctrl-W S    打开一个新的水平分割的窗口
Ctrl-W C    关闭一个窗口
Ctrl-W O    除了当前窗口，关闭所有其他的窗口
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

另外，下面的列表列出了一些有用的关于windows的命令行命令

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:vsplit filename    垂直分割当前窗口，并在新窗口中打开名为filename的文件。
:split filename     水平分割当前窗口，并在新窗口中打开名为filename的文件。
:new filename       创建一个新窗口并打开名为filename的文件。
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

花一点时间理解上面的知识。要了解更多信息，可以查看帮助&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h window&lt;/code&gt;。

&lt;h2 id=&quot;tabs&quot;&gt;Tabs&lt;/h2&gt;

Tabs就是windows的集合。它就像窗口的布局。在大部分的现代文本编辑器（还有现代互联网浏览器）中，一个tab意味着打开一个文件/页面，当您关闭标签，相应的文件/页面就消失了。但在Vim中，tab并不表示打开了一个文件。当您在Vim中关闭一个tab，您并不是关闭一个文件。您仅仅关闭了窗口布局。文件的数据依然存储在内存中的buffers中。

让我们运行几个命令看看Vim中tabs的功能。打开&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file1.js&lt;/code&gt;：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim file1.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

若要在新tab中打开&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;file2.js&lt;/code&gt;：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:tabnew file2.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

当然您可以按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;让Vim自动补全 &lt;em&gt;新tab&lt;/em&gt; 中将要打开的文件名（啰嗦几句，请理解作者的幽默 ）。

下面的列表列出了一些有用的关于tab导航的命令：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:tabnew file.txt    在tab中打开一个文件
:tabclose           关闭当前tab
:tabnext            切换至下一个tab
:tabprevious        切换至前一个tab
:tablast            切换至最后一个tab
:tabfirst           切换至第一个tab
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您可以输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gt&lt;/code&gt;切换到下一个标签页（对应的，可以用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gT&lt;/code&gt;切换到前一个标签页）。您也可以传递一个数字作为参数给&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gt&lt;/code&gt;，这个数字是tab的编号。若想切换到第3个tab，输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;3gt&lt;/code&gt;。

拥有多个tabs的好处是，您可以在不同的tab中使用不同的窗口布局。也许，您想让您的第1个tab包含3个垂直分割的窗口，然后让第2个tab为水平分割和垂直分割混合的窗口布局。tab是完成这件工作的完美工具!

若想让Vim启动时就包含多个tabs，您可以在终端中运行如下命令：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; file1.js file2.js file3.js
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;三维移动&quot;&gt;三维移动&lt;/h2&gt;

在windows之间移动就像在笛卡尔坐标系的二维平面上沿着X-Y轴移动。您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-W H/J/K/L&lt;/code&gt;移动到上面、右侧、下面、以及左侧的窗口。

在buffer之间移动就像在笛卡尔坐标系的Z轴上穿梭。想象您的buffer文件在Z轴上呈线性排列，您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:bnext&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bprevious&lt;/code&gt;在Z轴上一次一个buffer地遍历。您也可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:buffer 文件名/buffer编号&lt;/code&gt;在Z轴上跳转到任意坐标。

结合window和buffer的移动，您可以在 &lt;em&gt;三维空间&lt;/em&gt; 中移动。您可以使用window导航命令移动到上面、右侧、下面、或左侧的窗口（X-Y平面导航）。因为每个window都可能包含了多个buffers，您可以使用buffer移动命令向前、向后移动（Z轴导航）。

&lt;h2 id=&quot;用聪明的方法使用bufferswindows以及tabs&quot;&gt;用聪明的方法使用Buffers、Windows、以及Tabs&lt;/h2&gt;

您已经学习了什么是buffers、windows、以及tabs，也学习了它们如何在Vim中工作。现在您对它们有了更好地理解，您可以把它们用在您自己的工作流程中。

每个人都有不同的工作流程，以下示例是我的工作流程：

&lt;ul&gt;
  &lt;li&gt;首先，对于某个特定任务，我先使用buffers存储所有需要的文件。Vim就算打开很多buffer，速度一般也不会减慢。另外打开多个buffers并不会使我的屏幕变得拥挤。我始终只会看到1个buffer（假设我只有1个window），这可以让我注意力集中在1个屏幕上。当我需要使用其他文件时，可以快速切换至对应文件的buffer。&lt;/li&gt;
  &lt;li&gt;当比对文件、读文档、或追踪代码流时，我使用多窗口来一次查看多个buffers。我尽量保持屏幕上的窗口数不超过3个，因为超过3个屏幕将变得拥挤（我使用的是小型笔记本）。当相应工作完成后，我就关掉多余的窗口。窗口越少可以使注意力更集中。&lt;/li&gt;
  &lt;li&gt;我使用&lt;a href=&quot;https://github.com/tmux/tmux/wiki&quot;&gt;tmux&lt;/a&gt;windows来代替tabs。通常一次使用多个tmux窗口。比如，一个tmux窗口用来写客户端代码，一个用来写后台代码。&lt;/li&gt;
&lt;/ul&gt;

由于编辑风格不同，我的工作流程可能和您的工作流程不同，这没关系。您可以在实践中去探索适合您自己工作流程的编码风格。

</description>
        <pubDate>Fri, 24 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch02-buffers-windows-tabs/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch02-buffers-windows-tabs/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>buffer</category>
        
        <category>window</category>
        
        <category>tab</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 第一章：起步</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

在本章，您将了解从终端启动Vim的几种不同方法。我写这本教程时使用的Vim版本是8.2。如果您使用Neovim或老版本的Vim，大部分情况下方法是通用的，但注意个别命令可能无效。

&lt;h2 id=&quot;安装&quot;&gt;安装&lt;/h2&gt;

我不会给出在某台特定机器上安装Vim的详细指令。好消息是，大部分Unix-based电脑应该预装了Vim。如果没有，大部分发行版也应该有关于如何安装Vim的指令。

从Vim的官方网站或官方仓库可以获得下载链接：

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.vim.org/download.php&quot;&gt;Vim 官网&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/vim/vim&quot;&gt;Vim 官方仓库&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hub.fastgit.org/vim/vim&quot;&gt;Vim 官方仓库镜像&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;vim命令&quot;&gt;Vim命令&lt;/h2&gt;

当您安装好Vim后，在终端运行以下命令：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您应该会看到一个介绍界面。这就是您用来处理文本的工作区。不像其它大部分文本编辑器和IDE，Vim是一个模式编辑器。如果您想输入”hello”，您需要使用’i’命令切换到插入模式。按下’ihello&lt;Esc&gt;&apos;可以在工作区插入文本&quot;hello&quot;。&lt;/Esc&gt;

&lt;h2 id=&quot;退出vim&quot;&gt;退出Vim&lt;/h2&gt;

有好几种不同的方法都可以退出Vim。（译者注：在stackflow论坛上，有个著名的问题”如何退出Vim”，五年来，有超过100万开发者遇到相同的问题。^_^，这件事已经成为了开发者中的一个梗）。最常用的退出方法是在Vim中输入：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:quit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您可以使用简写&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:q&lt;/code&gt;。这是一个命令行模式的命令(command-line mode：Vim的另一种模式)。如果您在普通模式输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;，光标将移动到屏幕底部，在这里您可以输入命令。在后面的第15章，您会学到关于命令行模式更多信息。如果您处于插入模式，按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:&lt;/code&gt;将会在屏幕上直接显示”:”(冒号)。因此，您需要按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Esc&amp;gt;&lt;/code&gt;键切换回普通模式。顺带说一下，在命令行模式也可以通过按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Esc&amp;gt;&lt;/code&gt;键切换回普通模式。您将会注意到，在Vim的好几种模式下都可以通过按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Esc&amp;gt;&lt;/code&gt;键切回普通模式。

&lt;h2 id=&quot;保存文件&quot;&gt;保存文件&lt;/h2&gt;

若要保存您的修改，在Vim中输入：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:write
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

您也可以输入简写’:w’。如果这是一个新建的文件，您必须给出文件名才能保存。下面的命令使文件保存为名为’file.txt’的文件，在Vim命令行运行：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:w file.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果想保存并退出，可以将’:w’和’:q’命令联起来，在Vim命令行中输入：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:wq
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果想不保存修改而强制退出，可以在’:q’命令后加’!’（叹号）,在Vim命令行中：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:q!
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;帮助&quot;&gt;帮助&lt;/h2&gt;

在本指南全文中，我将向您提及好几种Vim的帮助页面。您可以通过输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:help {命令}&lt;/code&gt;(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h&lt;/code&gt;是简写)进入相关命令的帮助文档。可以向&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h&lt;/code&gt;命令传递主题、命令名作为参数。比如，如果想查询退出Vim的方法，在vim中输入：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:h write-quit
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

我是怎么知道应该搜索”write-quit”这个关键词的呢？实际上我也不知道，我仅仅只是输入’:h quit’，然后按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;。Vim会自动显示相关联的关键词供用户选择。如果您需要查询一些信息，只需要输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h&lt;/code&gt;后接关键词，然后按&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;Tab&amp;gt;&lt;/code&gt;。

&lt;h2 id=&quot;打开文件&quot;&gt;打开文件&lt;/h2&gt;

如果想在终端中使用Vim打开名为(‘hello1.txt’)，在终端中运行：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim hello1.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

可以一次打开多个文件，在终端中：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim hello1.txt hello2.txt hello3.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim会在不同的buffers中打开’hello1.txt’，’hello2.txt’，’hello3.txt’。在下一章您将学到关于buffers的知识。

&lt;h2 id=&quot;参数&quot;&gt;参数&lt;/h2&gt;

您可以在终端中向&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vim&lt;/code&gt;命令传递参数。

如果想查看Vim的当前版本，在终端中运行：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim &lt;span class=&quot;nt&quot;&gt;--version&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

终端中将显示您当前Vim的版本和所有支持的特性，’+’表示支持的特性，’-‘表示不支持的特性。本教程中的一些操作需要您的Vim支持特定的特性。比如，在后面的章节中提到可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:history&lt;/code&gt;查看Vim的命令行历史记录。您的Vim必须包含&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+cmdline_history&lt;/code&gt;这一特性，这条命令才能正常使用。一般情况下，如果您通过主流的安装源下载Vim的话，您安装的Vim是支持所有特性的，

您在终端里做的很多事情都可以在Vim内部实现。比如，在Vim程序中也可以查看当前Vim版本，您可以运行下面的命令，在Vim中输入：

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;:version
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

如果您想打开&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.txt&lt;/code&gt;文件后迅速执行一条命令，您可以向&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vim&lt;/code&gt;传递一个&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+{cmd}&lt;/code&gt;选项。

在Vim中，您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:s&lt;/code&gt;命令（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;substitue&lt;/code&gt;的缩写）替换文本。如果您想打开&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello.txt&lt;/code&gt;后立即将所有的”pancake”替换成”bagel”，在终端中：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim +%s/pancake/bagel/g hello.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

该命令可以被叠加，在终端中：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim +%s/pancake/bagel/g +%s/bagel/egg/g +%s/egg/donut/g hello.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

Vim会将所有”pancake” 实例替换为”bagel”，然后将所有”bagel”替换为”egg”，然后将所有”egg”替换为”donut”（在后面的章节中您将学到如何替换）。

您同样可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;c&lt;/code&gt;标志来代替&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt;语法，在终端中：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; %s/pancake/bagel/g hello.txt
vim &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; %s/pancake/bagel/g &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; %s/bagel/egg/g &lt;span class=&quot;nt&quot;&gt;-c&lt;/span&gt; %s/egg/donut/g hello.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;打开多个窗口&quot;&gt;打开多个窗口&lt;/h2&gt;

您可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;o&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;O&lt;/code&gt;选项使Vim打开后分别显示为水平或垂直分割的窗口。

若想将Vim打开为2个水平分割的窗口，在终端中运行：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim &lt;span class=&quot;nt&quot;&gt;-o2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

若想将Vim打开为5个水平分割的窗口，在终端中运行：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim &lt;span class=&quot;nt&quot;&gt;-o5&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

若想将Vim打开为5个水平分割的窗口，并使前两个窗口显示&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello1.txt&lt;/code&gt;和&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hello2.txt&lt;/code&gt;的内容，在终端中运行：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim &lt;span class=&quot;nt&quot;&gt;-o5&lt;/span&gt; hello1.txt hello2.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

若想将Vim打开为2个垂直分割的窗口、5个垂直分割的窗口、5个垂直分割窗口并显示2个文件，在终端中分别运行以下命令：

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;vim &lt;span class=&quot;nt&quot;&gt;-O2&lt;/span&gt;
vim &lt;span class=&quot;nt&quot;&gt;-O5&lt;/span&gt;
vim &lt;span class=&quot;nt&quot;&gt;-O5&lt;/span&gt; hello1.txt hello2.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;h2 id=&quot;挂起&quot;&gt;挂起&lt;/h2&gt;

如果您编辑时想将Vim挂起，您可以按下&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-z&lt;/code&gt;。同样，您也可以使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:stop&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:suspend&lt;/code&gt;命令达到相同的效果。若想从挂起状态返回，在终端中运行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fg&lt;/code&gt;命令。

&lt;h2 id=&quot;聪明的启动vim&quot;&gt;聪明的启动Vim&lt;/h2&gt;

您可以向&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vim&lt;/code&gt;命令传递不同的选项(option)和标志(flag)，就像其他终端命令一样。其中一个选项是命令行命令（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+{cmd}&lt;/code&gt;或&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-c cmd&lt;/code&gt;）。当您读完本教程学到更多命令后，看看您是否能将相应命令应用到Vim的启动中。同样，作为一个终端命令，您可以将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vim&lt;/code&gt;命令和其他终端命令联合起来。比如，您可以将&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls&lt;/code&gt;命令的输出重定向到Vim中编辑，命令是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls -l | vim -&lt;/code&gt;。

若要了解更多Vim终端命令，查看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;man vim&lt;/code&gt;。若要了解更多关于Vim编辑器的知识，继续阅读本教程，多使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:help&lt;/code&gt;命令。

</description>
        <pubDate>Thu, 23 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch01-starting-vim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch01-starting-vim/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>入门</category>
        
        <category>启动</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Learn Vim 前言：为什么学 Vim</title>
        <description>&lt;blockquote&gt;
  本文翻译自 &lt;a href=&quot;https://github.com/iggredible/Learn-Vim&quot;&gt;iggredible/Learn-Vim&lt;/a&gt;，部分内容有增改。
&lt;/blockquote&gt;

&lt;h2 id=&quot;为什么写这本教程&quot;&gt;为什么写这本教程&lt;/h2&gt;

有很多途径可以学习Vim:&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vimtutor&lt;/code&gt;就是一个很好的途径，另外还有Vim自带的帮助手册（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:help&lt;/code&gt;查看）包含了所有需要的参考信息。

但是在日常使用中，对于绝大部分用户的需求而言，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;vimtutor&lt;/code&gt;讲得太浅不能满足需要，而&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:help&lt;/code&gt;所提供的信息又有很多是用不上的。本指南尝试在两者之间搭一座桥，仅重点介绍关键功能，使您尽可能在最短的时间内掌握Vim中最有用的知识。

绝大部分情况下，您并不会使用到Vim100%的功能。您可能仅仅需要掌握其中20%就可以成为一名娴熟的Vim用户。本指南将为您展示Vim中最有用的功能。

本指南带有作者强烈的个人观点。它涵盖了作者在使用Vim的过程中经常用到的技巧。章节按照作者认为的，最适合初学者最符合逻辑的顺序排列。

本指南包含大量示例。当学习一个新技能时，示例是不可缺少的，大量的示例可以更有效的巩固所学概念。

部分读者可能好奇为什么要学习Vimscript？我刚使用Vim时，仅仅满足于知道如何使用Vim。随着时间推移，我越来越需要使用Vimscript去写一些自定义命令，以满足特殊的编辑需要。在您使用Vim的过程中，您早晚会接触Vimscript。所以为什么不早点学呢？Vimscript是一门小巧的脚本语言，仅需要本指南的四个章节，您就可以学到它的基础知识。

您当然可以继续使用Vim而不了解任何Vimscript知识，但了解它可以使您更加擅长Vim。

本指南同时为初学者和高级Vim用户撰写。它从宽泛而简单的概念开始讲，最后落在特殊的、进阶的技巧上。如果您已经是一名进阶用户，我还是鼓励您从头到尾阅读本指南，因为您将了解到一些新的东西。

&lt;h2 id=&quot;如何从其他编辑器过渡到vim&quot;&gt;如何从其他编辑器过渡到Vim&lt;/h2&gt;

学习Vim的过程虽然很困难,但很具有成就感。有两种方法去学习Vim：

&lt;ol&gt;
  &lt;li&gt;强制转换地使用Vim&lt;/li&gt;
  &lt;li&gt;循序渐进地使用Vim&lt;/li&gt;
&lt;/ol&gt;

强制转换为Vim意味着从现在开始只使用Vim，停止使用您以前使用的任何编辑器/IDE。本方法的缺点是在前一周或两周，您将面临编辑效率的严重下降。如果您是一名专职程序员，本方法可能不太行得通。这也是为什么对于大部分读者而言，我认为过渡到Vim最好是循序渐进地使用Vim。

想要循序渐进地使用Vim，应该在最开始的两个星期，每天花1个小时使用Vim作为您的编辑器，而剩下的时间您可以继续使用其他编辑器。许多现代编辑器都带有Vim插件。我刚开始时，每天花1个小时使用VSCode的Vim插件。逐渐地，我增加使用Vim插件的时间，直到最终完全使用Vim插件。记住，这些插件仅仅模拟了Vim中的一小部分功能。要想体验Vim中比如Vimscript、命令行命令（Ex模式）、以及外部集成命令的强大功能，您必须使用Vim自身。

有两个关键节点驱使我开始100%地使用Vim：一是当我理解到Vim命令的语法结构（看第4章）时。二是学会&lt;a href=&quot;https://github.com/junegunn/fzf.vim&quot;&gt;fzf.vim&lt;/a&gt;插件时（看第3章）。

第一个，当我了解了Vim命令的语法结构时。这是一个决定性时间节点，我终于理解了Vim用户以前讨论的是什么。我并不需要去学习上百个命令，而仅需要学习一小部分命令，然后就可以凭直觉将它们串起来去做很多事情。

第二个，fzf插件我经常使用，该插件具有类似IDE的模糊文件查找功能。当我学会在Vim中如何使用该功能时，我的编辑效率极大提升，从此再也回不去了。

尽管每一个程序都是不同的。但仔细一想，您会发现您所偏爱的编辑器/IDE总有1个或2个功能是您频繁使用的。也许是模糊查找，也许是跳转至定义，或是快速编译，但不管是什么，快速确认这些功能，然后学会如何在Vim中实现它们（一般情况下Vim都能办到）。您的编辑速度将会获得一个巨大的提高。

当您使用Vim可以达到您原来50%的效率时，就是时候完全使用Vim了。

&lt;h2 id=&quot;如何阅读本指南&quot;&gt;如何阅读本指南&lt;/h2&gt;

本指南注重实效性。为了更好地在Vim中工作，您需要锻炼您的肌肉记忆，而不是大脑理解。

想学会骑自行车，您并不需要首先阅读一本骑车教程。您需要做的是骑上一辆真正的自行车，然后在实践中探索。

您需要输入每一条本指南中提到的命令。不仅如此，您还需要不断地重复，然后尝试命令的不同联合方式。想了解您刚学到命令具有什么功能，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:help&lt;/code&gt;命令和搜索引擎是您最好的帮手。但您的目标并不是去掌握关于一条命令的全部信息，而是能够本能地、自然地使用该命令。

我本想尽可能地使本指南的知识点呈线性化展示，但有些概念还是不得不违反顺序。比如在第1章，我就提到了替换命令（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:s&lt;/code&gt;），这个知识点要到第12章才讲。作为补救，不管是什么概念，如果被提前讲到了，我将为它提供一个简短的说明，原谅我吧。

&lt;h2 id=&quot;更多帮助&quot;&gt;更多帮助&lt;/h2&gt;

关于Vim帮助手册有一个额外的小技巧：假设您想了解关于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-p&lt;/code&gt;在插入模式时的功能。如果您仅仅查找&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h CTRL-P&lt;/code&gt;，您将被导航到普通模式下关于&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Ctrl-P&lt;/code&gt;的介绍页面，这并不是您想要的信息。在这种情况下，改为查找&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h i_CTRL-P&lt;/code&gt;。添加的&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i_&lt;/code&gt;表示插入模式。注意它属于哪个模式。

&lt;h2 id=&quot;语法&quot;&gt;语法&lt;/h2&gt;

大部分命令或涉及的代码片段都使用代码样式（&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;like this&lt;/code&gt;）。

字符串使用双引号包括（”like this”）。

Vim命令可以使用简写。比如，&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:join&lt;/code&gt;可以简写为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:j&lt;/code&gt;。本指南全文中，我将混合使用简写和全称。对于本指南不经常使用的命令，我将使用全称。而对于经常使用的命令，我将使用简写。我为这不一致性道歉。总之，不管什么时候当您看到一个新命令，记得使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:help&lt;/code&gt;查看它的简写。

&lt;h2 id=&quot;vimrc&quot;&gt;Vimrc&lt;/h2&gt;

在本指南的很多地方，我将提到vimrc选项。如果您是初学者，可以把vimrc看做一个配置文件。

Vimrc直到第21章才讲。为了保持清晰，我将在这里简要的介绍如何配置Vimrc。

假设您需要设置number选项，即显示行号(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set number&lt;/code&gt;)。如果您还没有vimrc文件，就创建一个。它通常位于根目录，名字为&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.vimrc&lt;/code&gt;。根据您的操作系统，该位置可能不同。在macOS，我将它放在&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;~/.vimrc&lt;/code&gt;。要查看您的vimrc文件应该放在哪里，查看&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:h vimrc&lt;/code&gt;。

在vimrc文件内，添加一行&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set number&lt;/code&gt;。保存(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:w&lt;/code&gt;)，然后激活这一配置(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:source %&lt;/code&gt;)。您将会在每一行的的左侧看到行号。

另外，如果您不想永久配置一个选项，您可以在Vim命令行使用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set&lt;/code&gt;命令，输入&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;:set number&lt;/code&gt;。这种方法的缺点是设置是临时的。当您关闭Vim，该配置选项将消失。

因为我们学的是Vim而不是Vi，有一个设置您必须启用，这就是&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nocompatible&lt;/code&gt;选项。在您的vimrc文件中添加&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;set nocompatible&lt;/code&gt;。如果启用&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;compatible&lt;/code&gt;选项，许多Vim特有的功能将会被禁止。

一般而言，不管什么时候只要有一段内容涉及vimrc选项，只需要将该选项添加到vimrc中就好了，然后保存并激活相关选项。

&lt;h2 id=&quot;未来计划错误问题&quot;&gt;未来计划，错误，问题&lt;/h2&gt;

期待未来有更多的更新。如果您发现任何错误或有什么疑问，请随意提交。

我计划近期再发布一些更新章节，敬请关注。

&lt;h2 id=&quot;更多关于vim的技巧&quot;&gt;更多关于Vim的技巧&lt;/h2&gt;

要了解Vim更多的信息，请关注我的推特（需墙）。&lt;a href=&quot;https://twitter.com/learnvim&quot;&gt;@learnvim&lt;/a&gt;.

&lt;h2 id=&quot;感谢&quot;&gt;感谢&lt;/h2&gt;

感谢Bram Moleenar编写了Vim，没有Vim就不会有本指南。感谢我的妻子当我编写本指南时表现的耐心和支持。感谢所有的贡献者&lt;a href=&quot;https://github.com/iggredible/Learn-Vim/graphs/contributors&quot;&gt;contributors&lt;/a&gt;，感谢Vim社区，还有很多其他没有提及的人们。

谢谢，您们使得这件工作更加有趣。:)

</description>
        <pubDate>Wed, 22 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-vim-ch00-preface/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-vim-ch00-preface/</guid>
        
        <category>Vim</category>
        
        <category>Learn-Vim</category>
        
        <category>入门</category>
        
        
        <category>Vim</category>
        
        <category>教程</category>
        
      </item>
    
      <item>
        <title>Nova - chat.nvim 的 Android 客户端</title>
        <description>Nova 是 chat.nvim 的 Android 客户端，让你在手机上继续 Neovim 中的 AI 对话。</description>
        <pubDate>Wed, 22 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/introducing-nova/</link>
        <guid isPermaLink="true">https://wsdjeg.net/introducing-nova/</guid>
        
        <category>android</category>
        
        <category>neovim</category>
        
        <category>chat.nvim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>curl 请求体长度限制问题及解决方案</title>
        <description>排查 curl 通过 stdin 传递请求体时的长度限制问题，并给出解决方案。</description>
        <pubDate>Mon, 20 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/curl-stdin-body/</link>
        <guid isPermaLink="true">https://wsdjeg.net/curl-stdin-body/</guid>
        
        <category>neovim</category>
        
        <category>lua</category>
        
        <category>curl</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>阿里云 Coding Plan Bug：流式输出内容截断</title>
        <description>记录阿里云百炼平台 Coding Plan 流式输出内容截断的 Bug 分析过程。</description>
        <pubDate>Mon, 20 Apr 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/aliyuncs-coding-plan-content-truncated-bug/</link>
        <guid isPermaLink="true">https://wsdjeg.net/aliyuncs-coding-plan-content-truncated-bug/</guid>
        
        <category>SSE</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Neovim 配置管理最佳实践</title>
        <description>Neovim 配置管理的最佳实践，从目录结构到多机器同步的完整方案。</description>
        <pubDate>Sat, 21 Mar 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-config-best-practices/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-config-best-practices/</guid>
        
        <category>neovim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>阿里云 Coding Plan GLM-5 流式 Tool Call 异常输出</title>
        <description>分析阿里云 Coding Plan GLM-5 模型流式 Tool Call 输出的协议异常问题。</description>
        <pubDate>Sat, 21 Mar 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/glm5-streaming-toolcall-analysis/</link>
        <guid isPermaLink="true">https://wsdjeg.net/glm5-streaming-toolcall-analysis/</guid>
        
        <category>SSE</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>job.nvim raw 参数导致 JSON 解析失败</title>
        <description>排查 job.nvim 中 raw 参数导致 JSON 解析失败的问题。</description>
        <pubDate>Tue, 10 Mar 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/job-nvim-raw-json-parsing/</link>
        <guid isPermaLink="true">https://wsdjeg.net/job-nvim-raw-json-parsing/</guid>
        
        <category>neovim</category>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>为什么仍然在使用 job.nvim</title>
        <description>对比 job.nvim 与 vim.system，解释为什么仍然选择使用 job.nvim。</description>
        <pubDate>Wed, 25 Feb 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/why-use-job-instead-of-vim-system/</link>
        <guid isPermaLink="true">https://wsdjeg.net/why-use-job-instead-of-vim-system/</guid>
        
        <category>neovim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>从零开始理解如何制作自己的 AI 助手</title>
        <description>从零开始理解 AI 助手的核心原理，拨开概念迷雾，看清技术本质。</description>
        <pubDate>Wed, 25 Feb 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/ai-assistant-implementation-from-scratch/</link>
        <guid isPermaLink="true">https://wsdjeg.net/ai-assistant-implementation-from-scratch/</guid>
        
        <category>AI</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Neovim 智能对话伙伴 chat.nvim</title>
        <description>chat.nvim 插件介绍：在 Neovim 中集成 AI 对话，提升编码效率。</description>
        <pubDate>Tue, 03 Feb 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/integrate-ai-chat-into-neovim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/integrate-ai-chat-into-neovim/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim buffer 删除插件 bufdel.nvim</title>
        <description>bufdel.nvim 插件介绍：删除 buffer 时保持窗口布局不变。</description>
        <pubDate>Wed, 28 Jan 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/bufdel-without-changing-window-layout/</link>
        <guid isPermaLink="true">https://wsdjeg.net/bufdel-without-changing-window-layout/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 日历插件 calendar.nvim</title>
        <description>calendar.nvim 插件介绍：在 Neovim 中查看日历，方便笔记日期管理。</description>
        <pubDate>Sun, 04 Jan 2026 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-minimal-calendar/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-minimal-calendar/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>文件路径大小写敏感导致 Lua 模块重载</title>
        <description>文件路径大小写敏感问题导致 Lua 模块重载失败的排查过程。</description>
        <pubDate>Sun, 28 Dec 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-module-reload-path-case-sensitivity/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-module-reload-path-case-sensitivity/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 悬浮滚动条 scrollbar.nvim</title>
        <description>scrollbar.nvim 插件介绍：为 Neovim 提供悬浮滚动条功能。</description>
        <pubDate>Thu, 25 Dec 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/floating-scrollbar-for-neovim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/floating-scrollbar-for-neovim/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>如何正确地使用 ftplugin 目录</title>
        <description>正确使用 Neovim ftplugin 目录的方法，避免常见配置陷阱。</description>
        <pubDate>Tue, 23 Dec 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/how-to-use-ftplugin-directory/</link>
        <guid isPermaLink="true">https://wsdjeg.net/how-to-use-ftplugin-directory/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>本地化管理 Github secrets</title>
        <description>使用脚本本地化管理多个 GitHub 仓库的 secrets，提升效率。</description>
        <pubDate>Thu, 04 Dec 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/local-github-secrets/</link>
        <guid isPermaLink="true">https://wsdjeg.net/local-github-secrets/</guid>
        
        <category>github</category>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>发现 Git 仓库中幽灵文件</title>
        <description>在 Git 历史中搜索已删除文件的技巧，快速定位删除 commit。</description>
        <pubDate>Sat, 29 Nov 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/find-ghosts-file-in-git-repository/</link>
        <guid isPermaLink="true">https://wsdjeg.net/find-ghosts-file-in-git-repository/</guid>
        
        <category>git</category>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 中使用 luarocks</title>
        <description>在 Neovim 中使用 luarocks 包管理器管理 Lua 依赖。</description>
        <pubDate>Thu, 27 Nov 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/using-luarocks-in-neovim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/using-luarocks-in-neovim/</guid>
        
        <category>neovim</category>
        
        <category>lua</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 窗口 API 参数 noautocmd 测试</title>
        <description>测试 Neovim 窗口 API 的 noautocmd 参数行为。</description>
        <pubDate>Wed, 12 Nov 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-nvim-win-noautocmd/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-nvim-win-noautocmd/</guid>
        
        <category>neovim</category>
        
        <category>lua</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 模糊搜索插件 picker.nvim</title>
        <description>picker.nvim 插件介绍：纯 Lua 实现的 Neovim 模糊搜索插件。</description>
        <pubDate>Fri, 31 Oct 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-fuzzy-finder-picker-nvim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-fuzzy-finder-picker-nvim/</guid>
        
        <category>neovim</category>
        
        <category>lua</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Github 仓库自动 release</title>
        <description>使用 Google release-please-action 自动化 GitHub 仓库版本发布。</description>
        <pubDate>Mon, 22 Sep 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/release-please-action/</link>
        <guid isPermaLink="true">https://wsdjeg.net/release-please-action/</guid>
        
        <category>git</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>为什么有那么多人讨厌清朝？</title>
        <description>从历史屠杀事件探讨为什么很多人对清朝评价负面。</description>
        <pubDate>Wed, 03 Sep 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20250903/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20250903/</guid>
        
        <category>满清</category>
        
        <category>历史</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>Lua 可变参数</title>
        <description>Lua 可变参数的使用方法与技巧。</description>
        <pubDate>Sun, 24 Aug 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-function-argvs/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-function-argvs/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>高效处理 Markdown 中的代码块</title>
        <description>使用插件高效编辑和执行 Markdown 中的代码块。</description>
        <pubDate>Sun, 24 Aug 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/code-block-in-markdown/</link>
        <guid isPermaLink="true">https://wsdjeg.net/code-block-in-markdown/</guid>
        
        <category>markdown</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>为什么停止维护 SpaceVim</title>
        <description>停止维护 SpaceVim 项目的原因说明。</description>
        <pubDate>Sat, 03 May 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/why-spacevim-is-archived/</link>
        <guid isPermaLink="true">https://wsdjeg.net/why-spacevim-is-archived/</guid>
        
        <category>neovim</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>解决 Windows 系统下输入法问题</title>
        <description>解决 Windows 系统下 Neovim 输入法切换问题。</description>
        <pubDate>Wed, 23 Apr 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-im-select-in-windows/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-im-select-in-windows/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 历史使用文件插件 mru.nvim</title>
        <description>mru.nvim 插件介绍：Neovim 最近使用文件管理插件。</description>
        <pubDate>Sun, 13 Apr 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-most-recently-used-files/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-most-recently-used-files/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 录制按键及屏幕</title>
        <description>在 Neovim 中录制按键和屏幕动图的方法。</description>
        <pubDate>Sat, 12 Apr 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-record-key-and-screen/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-record-key-and-screen/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 自动切换至项目根目录</title>
        <description>Neovim 自动切换至项目根目录的插件介绍。</description>
        <pubDate>Sat, 22 Mar 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-project-root-manager/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-project-root-manager/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 日志插件 logger.nvim</title>
        <description>logger.nvim 插件介绍：为 Neovim 插件提供统一的日志框架。</description>
        <pubDate>Sun, 16 Mar 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-logger/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-logger/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 任务管理插件 tasks.nvim</title>
        <description>tasks.nvim 插件介绍：Neovim 任务管理插件，支持 TOML 任务定义、变量展开、Problem Matcher、自动检测和 Task Provider API。</description>
        <pubDate>Sat, 01 Mar 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/tasks-manager-for-neovim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/tasks-manager-for-neovim/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 代码执行插件 code-runner.nvim</title>
        <description>code-runner.nvim 插件介绍：在 Neovim 中快速执行代码。</description>
        <pubDate>Fri, 28 Feb 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-code-runner/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-code-runner/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 插件管理器 nvim-plug</title>
        <description>nvim-plug 插件介绍：轻量级 Neovim 插件管理器。</description>
        <pubDate>Sun, 09 Feb 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-plugin-manager-nvim-plug/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-plugin-manager-nvim-plug/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>从 VimScipt 切换至 Lua</title>
        <description>从 VimScript 切换到 Lua 开发 Neovim 配置和插件的经验分享。</description>
        <pubDate>Sat, 01 Feb 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/from-vimscript-to-lua/</link>
        <guid isPermaLink="true">https://wsdjeg.net/from-vimscript-to-lua/</guid>
        
        <category>neovim</category>
        
        <category>lua</category>
        
        <category>VimScript</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>启用 Git Commit 签名</title>
        <description>启用 Git Commit 签名验证，提升代码安全性。</description>
        <pubDate>Fri, 24 Jan 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/enable-git-commit-signing/</link>
        <guid isPermaLink="true">https://wsdjeg.net/enable-git-commit-signing/</guid>
        
        <category>git</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>从零开始制作实时搜索插件</title>
        <description>从零开始制作 Neovim 实时搜索插件的完整过程。</description>
        <pubDate>Thu, 23 Jan 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/create-a-searching-plugin-from-scratch/</link>
        <guid isPermaLink="true">https://wsdjeg.net/create-a-searching-plugin-from-scratch/</guid>
        
        <category>neovim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Neovim extmarks 功能介绍</title>
        <description>Neovim extmarks 功能介绍：精确的缓冲区文本标记。</description>
        <pubDate>Sun, 19 Jan 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-extmarks/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-extmarks/</guid>
        
        <category>neovim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Neovim winbar 设置</title>
        <description>Neovim winbar 的设置方法与使用技巧。</description>
        <pubDate>Tue, 14 Jan 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-winbar/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-winbar/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>自定义 quickfix 窗口格式</title>
        <description>自定义 Neovim quickfix 窗口的显示格式。</description>
        <pubDate>Fri, 10 Jan 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/custom-quickfix-format/</link>
        <guid isPermaLink="true">https://wsdjeg.net/custom-quickfix-format/</guid>
        
        <category>vim</category>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>《误杀3》观后感</title>
        <description>电影《误杀3》观后感。</description>
        <pubDate>Wed, 08 Jan 2025 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20250108/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20250108/</guid>
        
        <category>movie</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>Neovim quickfix 窗口内的一些快捷键</title>
        <description>Neovim quickfix 窗口内的常用快捷键配置。</description>
        <pubDate>Mon, 30 Dec 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/key-bindings-for-neovim-quickfix-window/</link>
        <guid isPermaLink="true">https://wsdjeg.net/key-bindings-for-neovim-quickfix-window/</guid>
        
        <category>neovim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Vim/Neovim 中使用正则表达式</title>
        <description>Vim/Neovim 正则表达式的使用方法与注意事项。</description>
        <pubDate>Sun, 29 Dec 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/using-regular-expressions-in-vim-and-neovim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/using-regular-expressions-in-vim-and-neovim/</guid>
        
        <category>vim</category>
        
        <category>neovim</category>
        
        <category>regexp</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 状态栏及标签栏点击事件</title>
        <description>Neovim 状态栏和标签栏的点击事件处理。</description>
        <pubDate>Sat, 28 Dec 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-tabline-statusline-click-events/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-tabline-statusline-click-events/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>代码格式化插件 format.nvim</title>
        <description>format.nvim 插件介绍：Neovim 代码格式化插件。</description>
        <pubDate>Wed, 25 Dec 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/code-formatting-plugin-format-nvim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/code-formatting-plugin-format-nvim/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>在 SpaceVim 中使用卢曼卡片盒笔记法做笔记</title>
        <description>在 SpaceVim 中使用卢曼卡片盒笔记法管理笔记。</description>
        <pubDate>Wed, 11 Dec 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/zettelkasten-method-plugin-for-spacevim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/zettelkasten-method-plugin-for-spacevim/</guid>
        
        <category>neovim</category>
        
        <category>zettelkasten</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>《好东西》观后感</title>
        <description>电影《好东西》观后感，在压抑生活中寻找温暖。</description>
        <pubDate>Mon, 25 Nov 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20241125/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20241125/</guid>
        
        <category>movie</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>《重生》：揭穿人性假面</title>
        <description>电影《重生》观后感：揭穿人性假面。</description>
        <pubDate>Sun, 08 Sep 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/movie-review-go-for-broke/</link>
        <guid isPermaLink="true">https://wsdjeg.net/movie-review-go-for-broke/</guid>
        
        <category>movie</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>删除 Telegram 账号</title>
        <description>删除 Telegram 账号的原因与过程。</description>
        <pubDate>Sat, 24 Aug 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/delete-telegram-account/</link>
        <guid isPermaLink="true">https://wsdjeg.net/delete-telegram-account/</guid>
        
        <category>telegram</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>Vim Script 的新语法</title>
        <description>Vim Script 新语法介绍，从旧版本到新特性的演变。</description>
        <pubDate>Wed, 21 Aug 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/new-syntax-for-vim-script/</link>
        <guid isPermaLink="true">https://wsdjeg.net/new-syntax-for-vim-script/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>龙迹之城散人玩家攻略</title>
        <description>龙迹之城传奇类游戏散人玩家攻略分享。</description>
        <pubDate>Wed, 21 Aug 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20240821/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20240821/</guid>
        
        <category>game</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>使用 GitHub 进行身份验证时要小心</title>
        <description>使用 GitHub 身份验证时的安全注意事项。</description>
        <pubDate>Mon, 12 Aug 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/be-careful-when-authenticating-with-github/</link>
        <guid isPermaLink="true">https://wsdjeg.net/be-careful-when-authenticating-with-github/</guid>
        
        <category>github</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>请停止复制我的个人网站</title>
        <description>发现个人博客被全站复制，呼吁尊重原创内容。</description>
        <pubDate>Sun, 11 Aug 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/stop-copy-my-static-website/</link>
        <guid isPermaLink="true">https://wsdjeg.net/stop-copy-my-static-website/</guid>
        
        <category>jekyll</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>从 GitHub Page 切换至 Cloudflare Page</title>
        <description>从 GitHub Page 迁移到 Cloudflare Page 的过程与原因。</description>
        <pubDate>Sun, 11 Aug 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/from-github-page-to-cloudflare-page/</link>
        <guid isPermaLink="true">https://wsdjeg.net/from-github-page-to-cloudflare-page/</guid>
        
        <category>jekyll</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Vim9Script 与 Lua 的速度比较</title>
        <description>Vim9Script 与 Lua 执行速度的比较测试。</description>
        <pubDate>Sat, 10 Aug 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/speed-comparison-between-vim9script-and-lua/</link>
        <guid isPermaLink="true">https://wsdjeg.net/speed-comparison-between-vim9script-and-lua/</guid>
        
        <category>neovim</category>
        
        <category>vim</category>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Neovim 和 Vim 插件管理器的实现逻辑</title>
        <description>Neovim/Vim 插件管理器的实现逻辑分析。</description>
        <pubDate>Thu, 08 Aug 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/the-implementation-logic-of-plugin-manager/</link>
        <guid isPermaLink="true">https://wsdjeg.net/the-implementation-logic-of-plugin-manager/</guid>
        
        <category>neovim</category>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>实时调整 Neovim 的颜色主题</title>
        <description>实时调整 Neovim 颜色主题的插件介绍。</description>
        <pubDate>Sat, 03 Aug 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/patch-neovim-colorscheme-on-the-fly/</link>
        <guid isPermaLink="true">https://wsdjeg.net/patch-neovim-colorscheme-on-the-fly/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>悬浮滚动条插件 scrollbar.vim</title>
        <description>scrollbar.vim 插件介绍：Vim/Neovim 悬浮滚动条。</description>
        <pubDate>Tue, 30 Jul 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/floating-scrollbar-for-vim-and-neovim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/floating-scrollbar-for-vim-and-neovim/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>关于 Neovim 插件开发的指南</title>
        <description>对 Neovim 插件开发指南的看法与讨论。</description>
        <pubDate>Mon, 29 Jul 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/about-the-nvim-plugin-dev-guide/</link>
        <guid isPermaLink="true">https://wsdjeg.net/about-the-nvim-plugin-dev-guide/</guid>
        
        <category>neovim</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>Neovim 调色板插件 cpicker.nvim</title>
        <description>cpicker.nvim 插件介绍：Neovim 内置调色板工具。</description>
        <pubDate>Sun, 14 Jul 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-plugin-cpicker/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-plugin-cpicker/</guid>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neovim 缓冲区（buffer）相关事件</title>
        <description>Neovim 缓冲区相关事件的详解与分析。</description>
        <pubDate>Sun, 07 Jul 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-buf-events/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-buf-events/</guid>
        
        <category>neovim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>(Neo)Vim 括号补全插件比较</title>
        <description>(Neo)Vim 括号自动补全插件的横向比较。</description>
        <pubDate>Thu, 27 Jun 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/comparison-of-auto-pairs-plugins/</link>
        <guid isPermaLink="true">https://wsdjeg.net/comparison-of-auto-pairs-plugins/</guid>
        
        <category>vim</category>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Lua 与 Vim Script 之间函数相互调用</title>
        <description>Lua 与 Vim Script 之间函数相互调用的方法。</description>
        <pubDate>Tue, 11 Jun 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/call-function-between-lua-and-vim-script/</link>
        <guid isPermaLink="true">https://wsdjeg.net/call-function-between-lua-and-vim-script/</guid>
        
        <category>neovim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>更新 Neovim 遇到的问题</title>
        <description>升级 Neovim nightly 版本时遇到的兼容性问题。</description>
        <pubDate>Mon, 11 Mar 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/fucking-problems-when-update-neovim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/fucking-problems-when-update-neovim/</guid>
        
        <category>neovim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>修复 git clone 问题: gnutls_handshake() failed</title>
        <description>修复 WSL 中 git clone 出现的 gnutls_handshake 错误。</description>
        <pubDate>Fri, 23 Feb 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/fix-git-clone-in-wsl/</link>
        <guid isPermaLink="true">https://wsdjeg.net/fix-git-clone-in-wsl/</guid>
        
        <category>git</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>《热辣滚烫》观后感</title>
        <description>电影《热辣滚烫》观后感：为梦想坚持与改变。</description>
        <pubDate>Thu, 15 Feb 2024 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/movie-review-YOLO/</link>
        <guid isPermaLink="true">https://wsdjeg.net/movie-review-YOLO/</guid>
        
        <category>movie</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>从 Github 迁移到 Gitlab</title>
        <description>从 GitHub 迁移到 GitLab 的原因与过程。</description>
        <pubDate>Wed, 27 Sep 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/Migrate-from-GitHub-to-GitLab/</link>
        <guid isPermaLink="true">https://wsdjeg.net/Migrate-from-GitHub-to-GitLab/</guid>
        
        <category>gitlab</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>非常棒的安卓输入法 fcitx-android</title>
        <description>fcitx5-android 输入法推荐：安卓平台的开源隐私选择。</description>
        <pubDate>Wed, 07 Jun 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/fcitx-android/</link>
        <guid isPermaLink="true">https://wsdjeg.net/fcitx-android/</guid>
        
        <category>fcitx</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Neo-tree.nvim 糟糕的体验</title>
        <description>Neo-tree.nvim 文件树插件的使用体验与问题。</description>
        <pubDate>Wed, 31 May 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/bad-experience-about-neotree/</link>
        <guid isPermaLink="true">https://wsdjeg.net/bad-experience-about-neotree/</guid>
        
        <category>neovim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>我的 Neovim 之旅</title>
        <description>从 Vim 到 Neovim 的使用历程与技术演进。</description>
        <pubDate>Wed, 24 May 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/neovim-journey/</link>
        <guid isPermaLink="true">https://wsdjeg.net/neovim-journey/</guid>
        
        <category>neovim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>写给自己·关于健康</title>
        <description>关于健康重要性的思考，珍惜生命与奋斗的平衡。</description>
        <pubDate>Mon, 22 May 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20230522/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20230522/</guid>
        
        <category>lifestyle</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>停用 v2ex 账号</title>
        <description>停用 v2ex 账号的原因与经过。</description>
        <pubDate>Mon, 15 May 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/delete-v2ex-account/</link>
        <guid isPermaLink="true">https://wsdjeg.net/delete-v2ex-account/</guid>
        
        <category>lifestyle</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>母亲节·常回家看看</title>
        <description>母亲节回家看望家人的温馨记录。</description>
        <pubDate>Sun, 14 May 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20230514/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20230514/</guid>
        
        <category>life</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>介绍我的新插件 gfr.vim</title>
        <description>gfr.vim 插件介绍：集搜索、筛选、替换于一体的 Vim 插件。</description>
        <pubDate>Sun, 07 May 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/introduce-new-vim-plugin-gfr-vim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/introduce-new-vim-plugin-gfr-vim/</guid>
        
        <category>vim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>将微信朋友圈移至静态网站</title>
        <description>将微信朋友圈内容迁移至博客相册的过程分享。</description>
        <pubDate>Thu, 04 May 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/wechat-to-static-website/</link>
        <guid isPermaLink="true">https://wsdjeg.net/wechat-to-static-website/</guid>
        
        <category>gallery</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>五一假期·苏州一日游</title>
        <description>毕业十三年后重返苏州大学的五一一日游记录。</description>
        <pubDate>Mon, 01 May 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20230501/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20230501/</guid>
        
        <category>苏大</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>《长空之王》观后感</title>
        <description>电影《长空之王》观后感。</description>
        <pubDate>Sun, 30 Apr 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/movie-review-Born-To-Fly/</link>
        <guid isPermaLink="true">https://wsdjeg.net/movie-review-Born-To-Fly/</guid>
        
        <category>movie</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>Neovim lua bindeval 解决方案</title>
        <description>Neovim Lua bindeval 问题的解决方案。</description>
        <pubDate>Mon, 03 Apr 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/workaround-for-neovim-lua-bindeval/</link>
        <guid isPermaLink="true">https://wsdjeg.net/workaround-for-neovim-lua-bindeval/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>《忠犬八公》观后感</title>
        <description>电影《忠犬八公》观后感：忠诚与陪伴的故事。</description>
        <pubDate>Fri, 31 Mar 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/movie-review-Hachi-A-Dogs-Tale/</link>
        <guid isPermaLink="true">https://wsdjeg.net/movie-review-Hachi-A-Dogs-Tale/</guid>
        
        <category>movie</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>《保你平安》观后感</title>
        <description>电影《保你平安》观后感：网络谣言与人性的思考。</description>
        <pubDate>Fri, 24 Mar 2023 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/movie-review-Post-Truth/</link>
        <guid isPermaLink="true">https://wsdjeg.net/movie-review-Post-Truth/</guid>
        
        <category>movie</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>使用 Lua 重写 SpaceVim 内置插件</title>
        <description>使用 Lua 重写 SpaceVim 内置插件的过程与速度对比。</description>
        <pubDate>Sat, 22 Oct 2022 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/rewrite-spacevim-builtin-plugin-using-lua/</link>
        <guid isPermaLink="true">https://wsdjeg.net/rewrite-spacevim-builtin-plugin-using-lua/</guid>
        
        <category>vim</category>
        
        <category>neovim</category>
        
        <category>lua</category>
        
        <category>spacevim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>使用 Vim 作为 SSH 客户端</title>
        <description>使用 Vim/Neovim 作为 SSH 客户端连接远程服务器。</description>
        <pubDate>Mon, 10 Oct 2022 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/use-vim-as-ssh-client/</link>
        <guid isPermaLink="true">https://wsdjeg.net/use-vim-as-ssh-client/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>安装并设置 Shadowsocks</title>
        <description>下载安装并配置 Shadowsocks 代理服务。</description>
        <pubDate>Fri, 03 Jun 2022 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/setup-shadowsocks/</link>
        <guid isPermaLink="true">https://wsdjeg.net/setup-shadowsocks/</guid>
        
        <category>vim</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>重新启用知乎账号</title>
        <description>注销又重新启用知乎账号的经历。</description>
        <pubDate>Fri, 20 May 2022 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/cancel-zhihu-account/</link>
        <guid isPermaLink="true">https://wsdjeg.net/cancel-zhihu-account/</guid>
        
        <category>zhihu</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>想对微博说 FUCK</title>
        <description>对微博随意封号行为的抗议与反思。</description>
        <pubDate>Tue, 17 May 2022 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/fuck-weibo-com/</link>
        <guid isPermaLink="true">https://wsdjeg.net/fuck-weibo-com/</guid>
        
        <category>vim</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>nvim-tree.lua 使用初体验</title>
        <description>nvim-tree.lua 文件树插件的初次使用体验。</description>
        <pubDate>Mon, 09 May 2022 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/first-experience-with-nvim-tree/</link>
        <guid isPermaLink="true">https://wsdjeg.net/first-experience-with-nvim-tree/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>如何使用 SpaceVim 的 Job API</title>
        <description>SpaceVim Job API 的使用方法，统一 Vim8 和 Neovim 的异步接口。</description>
        <pubDate>Tue, 03 May 2022 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/how-to-use-spacevim-job-api/</link>
        <guid isPermaLink="true">https://wsdjeg.net/how-to-use-spacevim-job-api/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>使用 Vim 作为聊天客户端</title>
        <description>使用 Vim 作为聊天客户端，在终端中直接沟通。</description>
        <pubDate>Mon, 02 May 2022 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/use-vim-as-a-chat-client/</link>
        <guid isPermaLink="true">https://wsdjeg.net/use-vim-as-a-chat-client/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>下载安装winrar，并激活去广告</title>
        <description>下载安装 WinRAR 并去除广告的方法。</description>
        <pubDate>Sat, 19 Feb 2022 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/activate-winrar/</link>
        <guid isPermaLink="true">https://wsdjeg.net/activate-winrar/</guid>
        
        <category>winrar</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>在 SpaceVim 中启自动保存</title>
        <description>在 SpaceVim 中启用自动保存功能的方法。</description>
        <pubDate>Sun, 06 Feb 2022 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/setup-autosave-in-spacevim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/setup-autosave-in-spacevim/</guid>
        
        <category>spacevim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Window 7 下安装 nodejs</title>
        <description>Windows 7 下安装指定版本 Node.js 的方法。</description>
        <pubDate>Sat, 02 Oct 2021 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/install-nodejs-in-win7/</link>
        <guid isPermaLink="true">https://wsdjeg.net/install-nodejs-in-win7/</guid>
        
        <category>nodejs</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>配置 zig 的 Vim 开发环境</title>
        <description>配置 Vim/Neovim 的 Zig 语言开发环境。</description>
        <pubDate>Sun, 03 Jan 2021 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/setup-vim-as-zig-ide/</link>
        <guid isPermaLink="true">https://wsdjeg.net/setup-vim-as-zig-ide/</guid>
        
        <category>zig</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>字符串编辑距离算法</title>
        <description>Rust 实现字符串编辑距离算法。</description>
        <pubDate>Tue, 24 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/rust-edit-distance/</link>
        <guid isPermaLink="true">https://wsdjeg.net/rust-edit-distance/</guid>
        
        <category>rust</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Rust 模块与项目文件组织</title>
        <description>Rust 模块系统与项目文件组织方式详解。</description>
        <pubDate>Sun, 22 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/rust-modules-and-files/</link>
        <guid isPermaLink="true">https://wsdjeg.net/rust-modules-and-files/</guid>
        
        <category>rust</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Rust 读取标准输入</title>
        <description>Rust 读取标准输入的方法与实践。</description>
        <pubDate>Tue, 17 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/handle-stdin-in-rust/</link>
        <guid isPermaLink="true">https://wsdjeg.net/handle-stdin-in-rust/</guid>
        
        <category>rust</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Rust 字符串算法</title>
        <description>使用 Rust 解决字符串相关的算法问题。</description>
        <pubDate>Sun, 15 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/rust-string-algorithm/</link>
        <guid isPermaLink="true">https://wsdjeg.net/rust-string-algorithm/</guid>
        
        <category>rust</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Vim 字典补全插件</title>
        <description>使用 Rust 编写 Vim 异步字典补全插件。</description>
        <pubDate>Sat, 14 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/rust-async-dict-vim-plugin/</link>
        <guid isPermaLink="true">https://wsdjeg.net/rust-async-dict-vim-plugin/</guid>
        
        <category>vim</category>
        
        <category>rust</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>rust 格式化整理</title>
        <description>Rust 格式化宏的使用方法整理。</description>
        <pubDate>Sat, 07 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/format-macro-in-rust/</link>
        <guid isPermaLink="true">https://wsdjeg.net/format-macro-in-rust/</guid>
        
        <category>rust</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>开始学习 Rust</title>
        <description>从零开始学习 Rust 编程语言的入门笔记。</description>
        <pubDate>Sun, 01 Mar 2020 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/start-to-learn-rust/</link>
        <guid isPermaLink="true">https://wsdjeg.net/start-to-learn-rust/</guid>
        
        <category>rust</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>从 Vim 切换至 SpaceVim 的细节</title>
        <description>从原生 Vim 切换至 SpaceVim 的实用指南。</description>
        <pubDate>Thu, 20 Feb 2020 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/from-vim-to-spacevim-tips/</link>
        <guid isPermaLink="true">https://wsdjeg.net/from-vim-to-spacevim-tips/</guid>
        
        <category>spacevim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>SpaceVim 下的异步任务系统</title>
        <description>SpaceVim 异步任务系统的配置与使用。</description>
        <pubDate>Fri, 14 Feb 2020 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/tasks-support-in-spacevim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/tasks-support-in-spacevim/</guid>
        
        <category>spacevim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>心型脂肪酸结合蛋白（H-FABP）检测的临床意义</title>
        <description>心型脂肪酸结合蛋白（H-FABP）在急性心肌梗死早期诊断中的临床意义。</description>
        <pubDate>Thu, 22 Aug 2019 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/significance-of-h-fabp/</link>
        <guid isPermaLink="true">https://wsdjeg.net/significance-of-h-fabp/</guid>
        
        <category>ivd</category>
        
        
        <category>工作资料</category>
        
      </item>
    
      <item>
        <title>右键使用 SpaceVim 打开文件</title>
        <description>在 Windows 右键菜单中添加用 SpaceVim 打开文件。</description>
        <pubDate>Wed, 07 Aug 2019 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/open-with-spacevim-from-right-click-menu/</link>
        <guid isPermaLink="true">https://wsdjeg.net/open-with-spacevim-from-right-click-menu/</guid>
        
        <category>spacevim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>使用 Vim 管理待办事项</title>
        <description>使用 Vim 管理源码中的待办事项（TODO）。</description>
        <pubDate>Tue, 06 Aug 2019 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/todo-manager-in-vim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/todo-manager-in-vim/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>如何学习并使用 SpaceVim</title>
        <description>如何高效学习并使用 SpaceVim 的建议。</description>
        <pubDate>Mon, 05 Aug 2019 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/the-way-to-learn-spacevim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/the-way-to-learn-spacevim/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>《波西米亚狂想曲》观后感</title>
        <description>电影《波西米亚狂想曲》观后感：保持初心。</description>
        <pubDate>Wed, 27 Mar 2019 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/movie-review-Bohemian-Rhapsody/</link>
        <guid isPermaLink="true">https://wsdjeg.net/movie-review-Bohemian-Rhapsody/</guid>
        
        <category>movie</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>降钙素原检测的临床意义</title>
        <description>降钙素原（PCT）检测在细菌感染诊断中的临床意义。</description>
        <pubDate>Tue, 01 Jan 2019 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/significance-of-pct/</link>
        <guid isPermaLink="true">https://wsdjeg.net/significance-of-pct/</guid>
        
        <category>ivd</category>
        
        
        <category>工作资料</category>
        
      </item>
    
      <item>
        <title>再见了，网易博客</title>
        <description>告别网易博客，迁移至独立博客平台。</description>
        <pubDate>Wed, 12 Dec 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/goodbye-163-blog/</link>
        <guid isPermaLink="true">https://wsdjeg.net/goodbye-163-blog/</guid>
        
        <category>blog</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>Ruby 模块和包</title>
        <description>Ruby 模块和包详解：命名空间、Mixin 机制与 require 用法。</description>
        <pubDate>Wed, 10 Oct 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/ruby-modules/</link>
        <guid isPermaLink="true">https://wsdjeg.net/ruby-modules/</guid>
        
        <category>ruby</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Ruby 代码块</title>
        <description>Ruby 代码块详解：块声明、yield 语句与 BEGIN/END 块。</description>
        <pubDate>Thu, 04 Oct 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/ruby-block/</link>
        <guid isPermaLink="true">https://wsdjeg.net/ruby-block/</guid>
        
        <category>ruby</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Ruby 函数</title>
        <description>Ruby 函数详解：方法定义、参数传递与返回值。</description>
        <pubDate>Sun, 30 Sep 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/ruby-functions/</link>
        <guid isPermaLink="true">https://wsdjeg.net/ruby-functions/</guid>
        
        <category>ruby</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Ruby 循环</title>
        <description>Ruby 循环详解：while、until、for 及循环控制语句。</description>
        <pubDate>Mon, 24 Sep 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/ruby-loops/</link>
        <guid isPermaLink="true">https://wsdjeg.net/ruby-loops/</guid>
        
        <category>ruby</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>ruby 流程控制</title>
        <description>Ruby 流程控制详解：if/else、case、unless 条件语句。</description>
        <pubDate>Thu, 20 Sep 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/ruby-decision-making/</link>
        <guid isPermaLink="true">https://wsdjeg.net/ruby-decision-making/</guid>
        
        <category>ruby</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Ruby 运算符</title>
        <description>Ruby 运算符详解：算术、比较、赋值、位运算等。</description>
        <pubDate>Wed, 12 Sep 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/ruby-miscellaneous-operator/</link>
        <guid isPermaLink="true">https://wsdjeg.net/ruby-miscellaneous-operator/</guid>
        
        <category>ruby</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>如何在 Vim 内进行高效的排序</title>
        <description>Vim 内高效排序的方法：sort 函数与 sort 命令。</description>
        <pubDate>Fri, 07 Sep 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/how-to-sort-efficiently-in-vim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/how-to-sort-efficiently-in-vim/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>(Neo)Vim 插件开发指南</title>
        <description>(Neo)Vim 插件开发完整指南。</description>
        <pubDate>Fri, 31 Aug 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/vim-plugin-dev-guide/</link>
        <guid isPermaLink="true">https://wsdjeg.net/vim-plugin-dev-guide/</guid>
        
        <category>neovim</category>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>如何配置 SpaceVim</title>
        <description>SpaceVim 配置详解：选项设置、模块管理与自定义插件。</description>
        <pubDate>Tue, 28 Aug 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/how-to-config-spacevim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/how-to-config-spacevim/</guid>
        
        <category>spacevim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Ruby 数据类型</title>
        <description>Ruby 数据类型详解：数值、字符串、范围、符号、数组与哈希。</description>
        <pubDate>Wed, 15 Aug 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/ruby-data-types/</link>
        <guid isPermaLink="true">https://wsdjeg.net/ruby-data-types/</guid>
        
        <category>ruby</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Ruby 基本语法</title>
        <description>Ruby 基本语法入门：空白、标识符、注释与 Here Document。</description>
        <pubDate>Sun, 12 Aug 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/ruby-basic-syntax/</link>
        <guid isPermaLink="true">https://wsdjeg.net/ruby-basic-syntax/</guid>
        
        <category>ruby</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>初识 Ruby 编程语言</title>
        <description>初识 Ruby 编程语言：灵活的语法与代码块特性。</description>
        <pubDate>Thu, 09 Aug 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/initial-knowledge-of-ruby/</link>
        <guid isPermaLink="true">https://wsdjeg.net/initial-knowledge-of-ruby/</guid>
        
        <category>ruby</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Vim 中文文档规范检查插件</title>
        <description>chinese_linter.vim 插件：中文文档排版规范检查工具。</description>
        <pubDate>Wed, 01 Aug 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/vim-chinese-linter/</link>
        <guid isPermaLink="true">https://wsdjeg.net/vim-chinese-linter/</guid>
        
        <category>vim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>为什么要学习 Vim？</title>
        <description>为什么要学习 Vim：从编辑器选择到效率提升的思考。</description>
        <pubDate>Sun, 25 Mar 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/why-study-vim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/why-study-vim/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Vim 下多光标编辑 iedit 模式</title>
        <description>Vim/Neovim 多光标编辑 iedit 模式介绍。</description>
        <pubDate>Tue, 23 Jan 2018 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/iedit-mode-for-neovim-and-vim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/iedit-mode-for-neovim-and-vim/</guid>
        
        <category>vim</category>
        
        <category>neovim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Vim 从入门到精通</title>
        <description>Vim 从入门到精通的中文综合指南。</description>
        <pubDate>Mon, 27 Mar 2017 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/vim-galore-zh-cn/</link>
        <guid isPermaLink="true">https://wsdjeg.net/vim-galore-zh-cn/</guid>
        
        <category>neovim</category>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>SpaceVim 第一个版本</title>
        <description>SpaceVim 首个版本发布：社区驱动的 Vim 发行版。</description>
        <pubDate>Wed, 18 Jan 2017 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/first-release-of-spacevim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/first-release-of-spacevim/</guid>
        
        <category>vim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>在 Vim 中使用聊天工具</title>
        <description>在 Vim 中集成聊天工具，在 I3-WM 桌面下实现即时通讯。</description>
        <pubDate>Fri, 30 Dec 2016 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/chatting-in-vim/</link>
        <guid isPermaLink="true">https://wsdjeg.net/chatting-in-vim/</guid>
        
        <category>vim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>为什么要写博客</title>
        <description>关于写博客的意义与坚持。</description>
        <pubDate>Wed, 24 Jun 2015 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20150624/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20150624/</guid>
        
        <category>blog</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>可爱的小天使</title>
        <description>记录女儿出生的喜悦时刻。</description>
        <pubDate>Mon, 25 May 2015 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/my-little-daughter/</link>
        <guid isPermaLink="true">https://wsdjeg.net/my-little-daughter/</guid>
        
        <category>love</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>不忘初心、方得始终</title>
        <description>不忘初心方得始终：关于梦想与坚持的感悟。</description>
        <pubDate>Sat, 12 Jul 2014 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/never-forget-the-beginning/</link>
        <guid isPermaLink="true">https://wsdjeg.net/never-forget-the-beginning/</guid>
        
        <category>love</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>强大的vim命令 :g 和 :s</title>
        <description>Vim 全局替换命令 :g 和 :s 的强大用法。</description>
        <pubDate>Mon, 01 Jul 2013 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/vim-global-substitute/</link>
        <guid isPermaLink="true">https://wsdjeg.net/vim-global-substitute/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>欢迎来到 Racket</title>
        <description>Racket 编程语言入门：Lisp 方言与 Scheme 派生。</description>
        <pubDate>Tue, 19 Mar 2013 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/welcome-to-racket/</link>
        <guid isPermaLink="true">https://wsdjeg.net/welcome-to-racket/</guid>
        
        <category>lisp</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>使用 Windows Live Writer 发布博客</title>
        <description>使用 Windows Live Writer 发布博客的体验。</description>
        <pubDate>Fri, 20 May 2011 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20110520/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20110520/</guid>
        
        <category>blog</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>大学毕业了</title>
        <description>大学毕业回忆：从苏州大学走出的青春岁月。</description>
        <pubDate>Wed, 30 Jun 2010 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/graduated-from-Soochow-University/</link>
        <guid isPermaLink="true">https://wsdjeg.net/graduated-from-Soochow-University/</guid>
        
        <category>苏大</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>模块化 vimrc 初体验</title>
        <description>模块化管理 Vim 配置文件的思路与实践。</description>
        <pubDate>Wed, 01 Apr 2009 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/modular-vimrc/</link>
        <guid isPermaLink="true">https://wsdjeg.net/modular-vimrc/</guid>
        
        <category>vim</category>
        
        
        <category>工具分享</category>
        
      </item>
    
      <item>
        <title>Lua 数据库访问</title>
        <description>Lua 数据库访问库 LuaSQL 的使用方法。</description>
        <pubDate>Fri, 20 Jun 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-database-access/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-database-access/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 面向对象</title>
        <description>Lua 面向对象编程的实现方式。</description>
        <pubDate>Sat, 14 Jun 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-object-oriented/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-object-oriented/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 垃圾回收</title>
        <description>Lua 自动内存管理与垃圾回收机制详解。</description>
        <pubDate>Thu, 12 Jun 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-garbage-collection/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-garbage-collection/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 调试(Debug)</title>
        <description>Lua debug 库的使用方法与调试技巧。</description>
        <pubDate>Tue, 10 Jun 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-debug/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-debug/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 异常处理</title>
        <description>Lua 异常处理机制与错误类型详解。</description>
        <pubDate>Fri, 30 May 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-error-handling/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-error-handling/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 文件 IO</title>
        <description>Lua 文件 IO 操作：简单模式与完全模式。</description>
        <pubDate>Sat, 24 May 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-file-io/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-file-io/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 协同程序(coroutine)</title>
        <description>Lua 协同程序（coroutine）的概念与使用。</description>
        <pubDate>Tue, 20 May 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-coroutine/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-coroutine/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 元表(Metatable)</title>
        <description>Lua 元表（Metatable）的使用方法与元方法。</description>
        <pubDate>Tue, 13 May 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-metatables/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-metatables/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 模块和包</title>
        <description>Lua 模块和包的管理机制与使用方法。</description>
        <pubDate>Sat, 10 May 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-modules-packages/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-modules-packages/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua tables（表）</title>
        <description>Lua table 数据结构详解：关联数组与多种数据类型。</description>
        <pubDate>Mon, 28 Apr 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-tables/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-tables/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 迭代器</title>
        <description>Lua 迭代器的概念与遍历集合的使用方法。</description>
        <pubDate>Sun, 20 Apr 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-iterators/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-iterators/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 数组</title>
        <description>Lua 数组的使用：一维与多维数组。</description>
        <pubDate>Mon, 14 Apr 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-arrays/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-arrays/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 字符串</title>
        <description>Lua 字符串的三种表示方式与常用操作。</description>
        <pubDate>Tue, 08 Apr 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-strings/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-strings/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 运算符</title>
        <description>Lua 运算符详解：算术、关系、逻辑及其他运算符。</description>
        <pubDate>Fri, 28 Mar 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-miscellaneous-operator/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-miscellaneous-operator/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 函数</title>
        <description>Lua 函数的定义、参数传递与多返回值。</description>
        <pubDate>Thu, 20 Mar 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-functions/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-functions/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 流程控制</title>
        <description>Lua 流程控制：条件语句与布尔值判断。</description>
        <pubDate>Thu, 13 Mar 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-decision-making/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-decision-making/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 循环</title>
        <description>Lua 循环语句：while、for、repeat-until 的使用。</description>
        <pubDate>Sun, 09 Mar 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-loops/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-loops/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 数据类型</title>
        <description>Lua 数据类型详解：8 种基本类型与动态类型特性。</description>
        <pubDate>Wed, 13 Feb 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-data-types/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-data-types/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>Lua 基本语法</title>
        <description>Lua 基本语法：交互式编程与脚本式编程入门。</description>
        <pubDate>Sat, 09 Feb 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/lua-basic-syntax/</link>
        <guid isPermaLink="true">https://wsdjeg.net/lua-basic-syntax/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>学习 Lua 脚本语言</title>
        <description>因魔兽世界插件开始学习 Lua 脚本语言的经历。</description>
        <pubDate>Sun, 03 Feb 2008 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/learn-lua-script/</link>
        <guid isPermaLink="true">https://wsdjeg.net/learn-lua-script/</guid>
        
        <category>lua</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>大学选修课《Java 编程》</title>
        <description>大学选修 Java 编程课：从生物专业到编程的转折。</description>
        <pubDate>Wed, 11 Oct 2006 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/java-elective-course/</link>
        <guid isPermaLink="true">https://wsdjeg.net/java-elective-course/</guid>
        
        <category>java</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>进入苏州大学的第一天</title>
        <description>进入苏州大学第一天的入学回忆。</description>
        <pubDate>Mon, 21 Aug 2006 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/first-day-in-Soochow-University/</link>
        <guid isPermaLink="true">https://wsdjeg.net/first-day-in-Soochow-University/</guid>
        
        <category>苏大</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>高中毕业了</title>
        <description>高中毕业回忆：游戏与成长的青春岁月。</description>
        <pubDate>Fri, 30 Jun 2006 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20060630/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20060630/</guid>
        
        <category>回忆</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>不要轻言放弃</title>
        <description>不要轻言放弃：从荒废学业到重新振作的心路历程。</description>
        <pubDate>Thu, 01 Sep 2005 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/never-give-up/</link>
        <guid isPermaLink="true">https://wsdjeg.net/never-give-up/</guid>
        
        <category>高中</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>分班选生物还是化学</title>
        <description>高中分班时在生物和化学之间的选择与思考。</description>
        <pubDate>Wed, 01 Sep 2004 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/biological-or-chemical/</link>
        <guid isPermaLink="true">https://wsdjeg.net/biological-or-chemical/</guid>
        
        <category>vim</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>Vim 环视和固化分组</title>
        <description>Vim 正则表达式中的环视和固化分组用法。</description>
        <pubDate>Sun, 01 Aug 2004 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/vim-regular-expression-look-around-and-solidified-group/</link>
        <guid isPermaLink="true">https://wsdjeg.net/vim-regular-expression-look-around-and-solidified-group/</guid>
        
        <category>vim</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>最后一封信</title>
        <description>高中分班前夕收到同学来信的意外与失落。</description>
        <pubDate>Wed, 30 Jun 2004 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/20040630/</link>
        <guid isPermaLink="true">https://wsdjeg.net/20040630/</guid>
        
        <category>高中</category>
        
        
        <category>生活随笔</category>
        
      </item>
    
      <item>
        <title>Lisp 编程语言相关知识</title>
        <description>暑假学习 Lisp 编程语言的入门笔记。</description>
        <pubDate>Tue, 01 Jul 2003 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/the-related-knowledge-of-lisp/</link>
        <guid isPermaLink="true">https://wsdjeg.net/the-related-knowledge-of-lisp/</guid>
        
        <category>lisp</category>
        
        
        <category>学习笔记</category>
        
      </item>
    
      <item>
        <title>过往的记忆</title>
        <description>零碎旧时光记忆的合集，给过去画一个句号。</description>
        <pubDate>Mon, 30 Jun 2003 00:00:00 +0000</pubDate>
        <link>https://wsdjeg.net/memories-of-old-days/</link>
        <guid isPermaLink="true">https://wsdjeg.net/memories-of-old-days/</guid>
        
        
        <category>生活随笔</category>
        
      </item>
    
  </channel>
</rss>
