1
0
wiki/Technology/GettingStarted/命令行工具/Vim/3.Vim-高级功能.html

742 lines
502 KiB
HTML
Raw Permalink Normal View History

2024-10-10 16:56:32 +08:00
<!DOCTYPE html>
<html lang="zh"><head><title>Vim 高级功能</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Noto Serif Simplified Chinese:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="Vim 高级功能"/><meta property="og:description" content="探索 Vim 的高级功能如正则表达式、多行编辑、宏录制、VimScript 脚本语言及自动化工作流。."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="探索 Vim 的高级功能如正则表达式、多行编辑、宏录制、VimScript 脚本语言及自动化工作流。."/><meta name="generator" content="Quartz"/><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/GettingStarted/命令行工具/Vim/3.Vim-高级功能"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,0.672-1.5,1.5s0.672,1.5,1.5,1.5h3c0.828,0,1.5-0.672,1.5-1.5S34.328,16,33.5,16z M28.561,26.439C28.289,26.168,27.914,26,27.5,26c-0.828,0-1.5,0.672-1.5,1.5c0,0.414,0.168,0.789,0.439,1.06l2,2 C28.711,30.832,29.086,31,29.5,31c0.828,0,1.5-0.672,1.5-1.5c0-0.414-0.168-0.789-0.439-1.061L28.561,26.439z M17.5,29 c-0.829,0-1.5,0.672-1.5,1.5v3c0,0.828,0.671,1.5,1.5,1.5s1.5-0.672,1.5-1.5v-3C19,29.672,18.329,29,17.5,29z M17.5,7 C11.71,7,7,11.71,7,17.5S11.71,28,17.5,28S28,23.29,28,17.5S23.29,7,17.5,7z M17.5,25c-4.136,0-7.5-3.364-
<h3 id="高级文本操作">高级文本操作<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#高级文本操作" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="正则表达式">正则表达式<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#正则表达式" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<div class="table-container"><table><thead><tr><th>功能</th><th>命令或表达式</th><th>说明</th></tr></thead><tbody><tr><td>向前搜索</td><td><code>/pattern</code></td><td>向前搜索匹配的模式</td></tr><tr><td>向后搜索</td><td><code>?pattern</code></td><td>向后搜索匹配的模式</td></tr><tr><td>全文替换</td><td><code>:%s/pattern/replacement/g</code></td><td>全文替换所有匹配的模式</td></tr><tr><td>当前行替换</td><td><code>:s/pattern/replacement/g</code></td><td>替换当前行所有匹配的模式</td></tr><tr><td>删除行首空白</td><td><code>:%s/^\s\+//g</code></td><td>删除每行开头的空白字符</td></tr><tr><td>删除行尾空白</td><td><code>:%s/\s\+$//g</code></td><td>删除每行结尾的空白字符</td></tr><tr><td>匹配任意字符</td><td><code>.</code></td><td>匹配任意字符</td></tr><tr><td>匹配数字字符</td><td><code>\d</code></td><td>匹配数字字符</td></tr><tr><td>匹配非数字字符</td><td><code>\D</code></td><td>匹配非数字字符</td></tr><tr><td>匹配字母或数字字符</td><td><code>\w</code></td><td>匹配字母或数字字符</td></tr><tr><td>匹配非字母或数字字符</td><td><code>\W</code></td><td>匹配非字母或数字字符</td></tr><tr><td>匹配行首</td><td><code>^</code></td><td>匹配行首</td></tr><tr><td>匹配行尾</td><td><code>$</code></td><td>匹配行尾</td></tr></tbody></table></div>
<h4 id="多行编辑">多行编辑<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#多行编辑" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<div class="table-container"><table><thead><tr><th>功能</th><th>命令</th><th>说明</th></tr></thead><tbody><tr><td>行可视模式</td><td><code>V</code></td><td>进入行可视模式</td></tr><tr><td>字符可视模式</td><td><code>v</code></td><td>进入字符可视模式</td></tr><tr><td>块可视模式</td><td><code>Ctrl+v</code></td><td>进入块可视模式</td></tr><tr><td>在每行开头添加</td><td><code>:%s/^/#/</code></td><td>使用正则表达式在每行开头添加 <code>#</code></td></tr><tr><td>在每行结尾添加</td><td><code>:%s/$/;/</code></td><td>使用正则表达式在每行结尾添加 <code>;</code></td></tr></tbody></table></div>
<h4 id="宏录制与播放">宏录制与播放<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#宏录制与播放" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<div class="table-container"><table><thead><tr><th>功能</th><th>命令</th><th>说明</th></tr></thead><tbody><tr><td>录制宏</td><td><code>qx ... q</code></td><td>开始录制宏,<code>x</code> 是寄存器名,执行一系列命令后停止录制</td></tr><tr><td>播放宏</td><td><code>@x</code></td><td>播放寄存器 <code>x</code> 中的宏</td></tr><tr><td>重复播放宏</td><td><code>@@</code></td><td>重复播放上一个宏</td></tr><tr><td>多次播放宏</td><td><code>10@x</code></td><td>播放寄存器 <code>x</code> 中的宏 10 次</td></tr></tbody></table></div>
<h3 id="vimscript">VimScript<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#vimscript" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="基本语法">基本语法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#基本语法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>VimScript 是 Vim 的脚本语言,用于扩展和定制 Vim 的功能。以下是一些基本语法:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="vim" data-theme="github-light github-dark"><code data-language="vim" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">&quot; 这是一个注释</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">&quot; 变量</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">let</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> myvar </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 42</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> &quot; 整数变量</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">let</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> mystr </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Hello&quot;</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> &quot; 字符串变量</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">&quot; 条件语句</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> myvar</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> > </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">0</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Positive&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">elseif</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> myvar </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">==</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Zero&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">else</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> echo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Negative&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">endif</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">&quot; 循环语句</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">let</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 0</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">while</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> i</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> &lt; </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> echo</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> i</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> let</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> i </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">+=</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">endwhile</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">for</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> item </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">in</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">3</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">4</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">5</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> echo</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> item</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">endfor</span></span></code></pre></figure>
<h4 id="函数和变量">函数和变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#函数和变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="vim" data-theme="github-light github-dark"><code data-language="vim" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">&quot; 定义函数</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">function!</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MyFunction</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">arg</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> echo</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> a:arg</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">endfunction</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">&quot; 调用函数</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">call</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MyFunction</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;Hello&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">&quot; 局部变量</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">function!</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> MyFunction</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">()</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> let</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> l:localVar </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;I am local&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> echo</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> l:localVar</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">endfunction</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">&quot; 全局变量</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">let</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> g:globalVar </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;I am global&quot;</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">echo</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> g:globalVar</span></span></code></pre></figure>
<h4 id="编写和调试简单的-vimscript-脚本">编写和调试简单的 VimScript 脚本<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#编写和调试简单的-vimscript-脚本" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>编写脚本</strong></p>
<p>将 VimScript 代码写入 <code>.vim</code> 文件,例如 <code>myplugin.vim</code></p>
<pre><code>&quot; myplugin.vim
function! SayHello()
echo &quot;Hello, Vim!&quot;
endfunction
command! SayHello call SayHello()
</code></pre>
<p><strong>加载脚本</strong></p>
<p>将脚本放入 Vim 的 <code>plugin</code> 目录,启动 Vim 自动加载:</p>
<pre><code>mkdir -p ~/.vim/plugin
cp myplugin.vim ~/.vim/plugin/
</code></pre>
<p><strong>调试脚本</strong></p>
<p>使用 <code>echo</code> 命令输出调试信息:</p>
<pre><code>echo &quot;Debug info: &quot; . someVar
</code></pre>
<p>使用 <code>:messages</code> 查看所有输出信息。</p>
<h3 id="自动化">自动化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#自动化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="使用自动命令">使用自动命令<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用自动命令" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>自动命令autocommand允许在特定事件发生时自动执行命令例如文件保存、打开等。</p>
<pre><code>augroup MyAutoCmd
autocmd!
autocmd BufWritePost *.txt :echo &quot;Text file saved!&quot;
augroup END
</code></pre>
<div class="table-container"><table><thead><tr><th>事件</th><th>说明</th><th>权重</th></tr></thead><tbody><tr><td><code>BufReadPost</code></td><td>文件读取后</td><td>5</td></tr><tr><td><code>BufWritePost</code></td><td>文件写入后</td><td>5</td></tr><tr><td><code>BufNewFile</code></td><td>新建文件</td><td>4</td></tr><tr><td><code>FileType</code></td><td>文件类型检测</td><td>5</td></tr><tr><td><code>BufEnter</code></td><td>缓冲区进入</td><td>4</td></tr><tr><td><code>BufLeave</code></td><td>缓冲区离开</td><td>4</td></tr><tr><td><code>VimEnter</code></td><td>Vim 启动后</td><td>5</td></tr><tr><td><code>VimLeave</code></td><td>Vim 退出前</td><td>5</td></tr><tr><td><code>WinEnter</code></td><td>窗口进入</td><td>3</td></tr><tr><td><code>WinLeave</code></td><td>窗口离开</td><td>3</td></tr></tbody></table></div>
<h4 id="配置文件模板">配置文件模板<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置文件模板" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>~/.vim/templates/</code> 目录中创建模板文件,例如 <code>python_template.py</code></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="python" data-theme="github-light github-dark"><code data-language="python" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;">#!/usr/bin/env python3</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># -*- coding: utf-8 -*-</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;&quot;&quot;</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Created on &lt;++></span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">@author: &lt;++></span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;&quot;&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">def</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">():</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> pass</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">if</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> __name__</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ==</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;__main__&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> main()</span></span></code></pre></figure>
<p><strong>使用自动命令应用模板:</strong></p>
<pre><code>augroup MyTemplates
autocmd!
autocmd BufNewFile *.py 0r ~/.vim/templates/python_template.py
augroup END
</code></pre>
<h2 id="高效工作流">高效工作流<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#高效工作流" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="版本控制集成">版本控制集成<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#版本控制集成" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="使用-vim-fugitive">使用 Vim-fugitive<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-vim-fugitive" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>安装 vim-fugitive</strong></p>
<p><code>vimrc</code> 文件中添加以下内容(以 vim-plug 为例):</p>
<pre><code>call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-fugitive'
call plug#end()
</code></pre>
<p><strong>基本用法</strong></p>
<div class="table-container"><table><thead><tr><th>功能</th><th>命令</th><th>说明</th></tr></thead><tbody><tr><td>查看 Git 状态</td><td><code>:Gstatus</code></td><td>打开一个新窗口显示当前 Git 仓库的状态。可以在该窗口中导航和操作。</td></tr><tr><td>Git 添加</td><td><code>-</code></td><td><code>:Gstatus</code> 窗口中,将光标移动到文件名上,按 <code>-</code> 号将文件添加到索引。</td></tr><tr><td>Git 提交</td><td><code>:Gcommit</code></td><td>打开一个新的缓冲区输入提交信息,完成后保存并关闭缓冲区即可提交。</td></tr><tr><td>Git 推送</td><td><code>:Gpush</code></td><td>将本地提交推送到远程仓库。</td></tr><tr><td>Git 拉取</td><td><code>:Gpull</code></td><td>从远程仓库拉取最新的代码。</td></tr><tr><td>Git 日志</td><td><code>:Glog</code></td><td>显示 Git 提交日志,可以在日志中导航和查看每个提交的详细信息。</td></tr><tr><td>Git 分支</td><td><code>:Gbranch</code></td><td>显示和管理 Git 分支。</td></tr></tbody></table></div>
<h4 id="常用-git-命令">常用 Git 命令<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#常用-git-命令" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>在 Vim 中,使用 vim-fugitive 可以执行常见的 Git 命令。同时你也可以在 Vim 外部使用 Git 命令行工具:</p>
<div class="table-container"><table><thead><tr><th>功能</th><th>命令</th><th>说明</th></tr></thead><tbody><tr><td>克隆仓库</td><td><code>git clone &lt;repository_url></code></td><td>克隆一个远程仓库到本地。</td></tr><tr><td>查看状态</td><td><code>git status</code></td><td>查看当前仓库的状态。</td></tr><tr><td>添加文件</td><td><code>git add &lt;file></code></td><td>添加文件到索引。</td></tr><tr><td>提交更改</td><td><code>git commit -m &quot;Commit message&quot;</code></td><td>提交已添加的更改。</td></tr><tr><td>推送到远程仓库</td><td><code>git push</code></td><td>将本地提交推送到远程仓库。</td></tr><tr><td>拉取最新代码</td><td><code>git pull</code></td><td>从远程仓库拉取最新的代码。</td></tr><tr><td>创建新分支</td><td><code>git checkout -b &lt;new_branch></code></td><td>创建一个新的分支并切换到该分支。</td></tr><tr><td>合并分支</td><td><code>git merge &lt;branch></code></td><td>将指定分支合并到当前分支。</td></tr></tbody></table></div>
<h3 id="项目管理">项目管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#项目管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="nerdtree">NERDTree<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#nerdtree" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>vimrc</code> 文件中添加以下内容(以 vim-plug 为例):</p>
<pre><code>call plug#begin('~/.vim/plugged')
Plug 'preservim/nerdtree'
call plug#end()
</code></pre>
<p><strong>打开/关闭 NERDTree</strong></p>
<pre><code>:NERDTreeToggle
</code></pre>
<p>或者添加快捷键映射:</p>
<pre><code>nnoremap &lt;C-n> :NERDTreeToggle&lt;CR>
</code></pre>
<p><strong>在 NERDTree 中导航</strong></p>
<ul>
<li>使用 <code>j</code><code>k</code> 键上下移动</li>
<li>使用 <code>o</code> 键打开文件或目录</li>
<li>使用 <code>x</code> 键关闭目录</li>
</ul>
<p><strong>显示隐藏文件</strong></p>
<pre><code>:NERDTreeShowHidden
</code></pre>
<h4 id="配置项目相关设置">配置项目相关设置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置项目相关设置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>项目特定的 vimrc</strong></p>
<p>在项目根目录下创建一个 <code>.vimrc</code> 文件,包含项目特定的 Vim 配置。然后在主 <code>vimrc</code> 中添加以下内容:</p>
<pre><code>if filereadable(&quot;path/to/project/.vimrc&quot;)
source path/to/project/.vimrc
endif
</code></pre>
<p><strong>自动切换项目配置</strong></p>
<p>可以使用 <code>vim-rooter</code> 插件自动切换项目根目录并加载项目特定的配置。在 <code>vimrc</code> 中添加以下内容:</p>
<pre><code>call plug#begin('~/.vim/plugged')
Plug 'airblade/vim-rooter'
call plug#end()
let g:rooter_patterns = ['.git', '.hg', '.svn', 'Makefile']
</code></pre>
<h3 id="代码补全与调试">代码补全与调试<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#代码补全与调试" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="youcompleteme-代码补全">YouCompleteMe 代码补全<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#youcompleteme-代码补全" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>YouCompleteMe 是一个强大的代码补全插件,需要编译安装。具体步骤如下:</p>
<ol>
<li>
<p>安装依赖(以 Ubuntu 为例):</p>
<pre><code>sudo apt install build-essential cmake python3-dev
</code></pre>
</li>
<li>
<p>安装 YouCompleteMe</p>
<pre><code>git clone https://github.com/ycm-core/YouCompleteMe.git ~/.vim/bundle/YouCompleteMe
cd ~/.vim/bundle/YouCompleteMe
git submodule update --init --recursive
python3 install.py --all
</code></pre>
</li>
<li>
<p><code>vimrc</code> 中添加以下内容:</p>
<pre><code>call plug#begin('~/.vim/plugged')
Plug 'ycm-core/YouCompleteMe'
call plug#end()
</code></pre>
</li>
</ol>
<h4 id="deoplete-代码补全">Deoplete 代码补全<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#deoplete-代码补全" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><a href="https://github.com/Shougo/deoplete.nvim" class="external">Deoplete<svg aria-hidden="true" class="external-icon" viewBox="0 0 512 512"><path d="M320 0H288V64h32 82.7L201.4 265.4 178.7 288 224 333.3l22.6-22.6L448 109.3V192v32h64V192 32 0H480 320zM32 32H0V64 480v32H32 456h32V480 352 320H424v32 96H64V96h96 32V32H160 32z"></path></svg></a> 是另一种流行的代码补全插件,基于 Neovim。</p>
<ol>
<li>
<p>安装依赖(以 Neovim 为例):</p>
<pre><code>pip install neovim
</code></pre>
</li>
<li>
<p><code>vimrc</code> 中添加以下内容:</p>
<pre><code>call plug#begin('~/.vim/plugged')
Plug 'Shougo/deoplete.nvim', {'do': ':UpdateRemotePlugins'}
Plug 'Shougo/neosnippet.vim'
Plug 'Shougo/neosnippet-snippets'
call plug#end()
let g:deoplete#enable_at_startup = 1
</code></pre>
</li>
</ol>
<h4 id="vimspector-代码调试">Vimspector 代码调试<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#vimspector-代码调试" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>vimrc</code> 文件中添加以下内容(以 vim-plug 为例):</p>
<pre><code>call plug#begin('~/.vim/plugged')
Plug 'puremourning/vimspector'
call plug#end()
</code></pre>
<p><strong>基本配置</strong></p>
<p><code>.vimspector.json</code> 文件,定义调试配置,例如:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="json" data-theme="github-light github-dark"><code data-language="json" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;configurations&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;Launch&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;adapter&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;debugpy&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;configuration&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;request&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;launch&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;program&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;${workspaceFolder}/your_script.py&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">,</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> &quot;console&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;integratedTerminal&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<p><strong>使用</strong></p>
<ol>
<li>
<p>启动调试会话:</p>
<pre><code>:VimspectorLaunch
</code></pre>
</li>
<li>
<p>设置断点: 将光标移动到需要设置断点的行,按 <code>F9</code> 键。</p>
</li>
<li>
<p>继续执行、单步执行和停止调试: 使用 <code>F5</code><code>F10</code><code>F11</code><code>F12</code> 键进行调试控制。</p>
</li>
</ol>
<h2 id="性能优化">性能优化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#性能优化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="启动优化">启动优化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#启动优化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="延迟加载插件">延迟加载插件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#延迟加载插件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>延迟加载插件可以显著提高 Vim 的启动速度,只在需要时加载插件。</p>
<p><strong>使用 vim-plug 进行延迟加载</strong></p>
<ol>
<li>
<p><code>vimrc</code> 中配置延迟加载:</p>
<pre><code>call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-fugitive', { 'on': 'Gstatus' }
Plug 'scrooloose/nerdtree', { 'on': 'NERDTreeToggle' }
call plug#end()
</code></pre>
<p>以上配置将 vim-fugitive 插件仅在执行 <code>:Gstatus</code> 命令时加载NERDTree 插件仅在执行 <code>:NERDTreeToggle</code> 命令时加载。</p>
</li>
</ol>
<p><strong>使用 lazy.nvim 插件管理器</strong></p>
<p>lazy.nvim 是一个专门用于延迟加载插件的插件管理器。</p>
<ol>
<li>
<p>安装 lazy.nvim</p>
<pre><code>git clone https://github.com/folke/lazy.nvim.git ~/.vim/pack/lazy/start/lazy.nvim
</code></pre>
</li>
<li>
<p>配置延迟加载:</p>
<pre><code>call lazy#load_plugin('nerdtree', { 'on': 'NERDTreeToggle' })
</code></pre>
</li>
</ol>
<h4 id="精简配置文件">精简配置文件<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#精简配置文件" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>移除不必要的配置</strong></p>
<p>定期检查 <code>vimrc</code> 文件,移除不再使用的配置和插件,以减少 Vim 的启动时间和运行负担。</p>
<p><strong>分离插件配置</strong></p>
<p>将插件的配置分离到独立的文件中,以保持 <code>vimrc</code> 简洁。例如,将 NERDTree 的配置放在 <code>~/.vim/plugin/nerdtree.vim</code> 中:</p>
<pre><code>&quot; ~/.vim/plugin/nerdtree.vim
nnoremap &lt;C-n> :NERDTreeToggle&lt;CR>
</code></pre>
<p><strong>使用本地配置</strong></p>
<p>根据项目需求,在项目根目录下创建 <code>.vimrc</code><code>.vim/</code> 目录,存放项目特定的配置文件,以避免在全局配置中添加过多内容。</p>
<h3 id="系统集成">系统集成<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#系统集成" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="将-vim-与-tmux-结合使用">将 Vim 与 Tmux 结合使用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#将-vim-与-tmux-结合使用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>tmux 是一个终端复用器,允许你在一个终端窗口中运行多个会话,与 Vim 结合使用可以大大提高工作效率。</p>
<p><strong>安装 tmux</strong></p>
<p>在 Linux 或 MacOS 中,可以使用包管理器安装 tmux</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">sudo</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> apt</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tmux</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # Debian/Ubuntu</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">brew</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tmux</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # macOS</span></span></code></pre></figure>
<div class="table-container"><table><thead><tr><th>功能</th><th>命令</th><th>说明</th></tr></thead><tbody><tr><td>启动 tmux 会话</td><td><code>tmux</code></td><td>启动一个新的 tmux 会话</td></tr><tr><td>新建会话</td><td><code>tmux new -s session_name</code></td><td>创建一个名为 <code>session_name</code> 的新会话</td></tr><tr><td>附加会话</td><td><code>tmux attach -t session_name</code></td><td>附加到一个名为 <code>session_name</code> 的会话</td></tr><tr><td>列出会话</td><td><code>tmux ls</code></td><td>列出所有 tmux 会话</td></tr><tr><td>杀死会话</td><td><code>tmux kill-session -t session_name</code></td><td>杀死一个名为 <code>session_name</code> 的会话</td></tr><tr><td>分离会话</td><td><code>Ctrl-b d</code></td><td>分离当前会话</td></tr><tr><td>新建窗口</td><td><code>Ctrl-b c</code></td><td>创建一个新窗口</td></tr><tr><td>切换窗口</td><td><code>Ctrl-b n</code></td><td>切换到下一个窗口</td></tr><tr><td>切换窗口</td><td><code>Ctrl-b p</code></td><td>切换到上一个窗口</td></tr><tr><td>重命名窗口</td><td><code>Ctrl-b ,</code></td><td>重命名当前窗口</td></tr><tr><td>分割窗格</td><td><code>Ctrl-b %</code></td><td>垂直分割当前窗格</td></tr><tr><td>分割窗格</td><td><code>Ctrl-b &quot;</code></td><td>水平分割当前窗格</td></tr><tr><td>切换窗格</td><td><code>Ctrl-b o</code></td><td>切换到下一个窗格</td></tr><tr><td>关闭窗格</td><td><code>Ctrl-b x</code></td><td>关闭当前窗格</td></tr><tr><td>同步窗格</td><td><code>Ctrl-b :setw synchronize-panes on</code></td><td>在所有窗格中同步输入</td></tr><tr><td>切换会话</td><td><code>Ctrl-b s</code></td><td>列出所有会话并切换</td></tr></tbody></table></div>
<p><strong>与 Vim 集成</strong></p>
<p><code>vimrc</code> 中添加快捷键映射,方便与 tmux 交互:</p>
<pre><code>nnoremap &lt;silent> &lt;Leader>h :TmuxNavigateLeft&lt;CR>
nnoremap &lt;silent> &lt;Leader>j :TmuxNavigateDown&lt;CR>
nnoremap &lt;silent> &lt;Leader>k :TmuxNavigateUp&lt;CR>
nnoremap &lt;silent> &lt;Leader>l :TmuxNavigateRight&lt;CR>
</code></pre>
<p>需要安装 <code>christoomey/vim-tmux-navigator</code> 插件:</p>
<pre><code>call plug#begin('~/.vim/plugged')
Plug 'christoomey/vim-tmux-navigator'
call plug#end()
</code></pre>
<h4 id="在终端中使用-vim">在终端中使用 Vim<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在终端中使用-vim" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>在终端中使用 Vim 可以提高工作效率,特别是当你需要频繁切换编辑器和命令行时。</p>
<p><strong>配置终端</strong></p>
<p>选择一个支持丰富功能的终端,例如 <code>iTerm2</code>macOS<code>Alacritty</code>(跨平台)、<code>GNOME Terminal</code>Linux</p>
<p><strong>终端快捷键配置</strong></p>
<p>根据需要配置终端快捷键,方便打开 Vim。例如<code>iTerm2</code> 中,可以设置快捷键快速打开新的终端窗口或标签,并启动 Vim。</p>
<p><strong>Shell 集成</strong></p>
<p><code>~/.bashrc</code><code>~/.zshrc</code> 中添加别名和函数,提高使用 Vim 的效率:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> vi</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;vim&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> svi</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;sudo vim&quot;</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">alias</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> vrc</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;vim ~/.vimrc&quot;</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 快速打开项目目录中的 Vim</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">function</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> vproj</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">() {</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ~/projects/</span><span style="--shiki-light:#E36209;--shiki-dark:#FFAB70;">$1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> &amp;&amp; </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">vim</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
<p><strong>使用 FZF 进行文件搜索</strong></p>
<p>FZF 是一个命令行模糊查找工具,可以与 Vim 集成,快速搜索和打开文件。</p>
<ol>
<li>
<p>安装 FZF</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> clone</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --depth</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> 1</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> https://github.com/junegunn/fzf.git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ~/.fzf</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">~</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">/.fzf/install</span></span></code></pre></figure>
</li>
<li>
<p><code>vimrc</code> 中配置 FZF</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">call</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> plug#begin</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">'~/.vim/plugged'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">Plug</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'junegunn/fzf'</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">Plug</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> 'junegunn/fzf.vim'</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">call</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> plug#end</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">()</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">&quot; 使用 FZF 查找文件</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">nnoremap &lt;Leader>f :Files&lt;CR></span></span></code></pre></figure>
</li>
</ol></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:1,&quot;scale&quot;:1.1,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:-1,&quot;scale&quot;:0.9,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#高级技巧" data-for="高级技巧">高级技巧</a></li><li class="depth-1"><a href="#高级文本操作" data-for="高级文本操作">高级文本操作</a></li><li class="depth-1"><a href="#vimscript" data-for="vimscript">VimScript</a></li><li class="depth-1"><a href="#自动化" data-for="自动化">自动化</a></li><li class="depth-0"><a href="#高效工作流" data-for="高效工作流">高效工作流</a></li><li class="depth-1"><a href="#版本控制集成" data-for="版本控制集成">版本控制集成</a></li><li class="depth-1"><a href="#项目管理" data-for="项目管理">项目管理</a></li><li class="depth-1"><a href="#代码补全与调试" data-for="代码补全与调试">代码补全与调试</a></li><li class="depth-0"><a href="#性能优化" data-for="性能优化">性能优化</a></li><li class="depth-1"><a href="#启动优化" data-for="启动优化">启动优化</a></li><li class="depth-1"><a href="#系统集成" data-for="系统集成">系统集成</a></li></ul></div></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{&quot;path&quot;:&quot;Personal&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2018&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2020&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2021&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2022&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2023&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2024&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/个人成长&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/医学健康&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/历史&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/哲学宗教&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/心理&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/政治军事&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/教育学习&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/文学&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/生活百科&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/社会文化&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/科学技术&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/经济理财&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/艺术&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Book/计算机&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Journal&quot;,&quot;collapsed&qu
</script><script type="module">
let mermaidImport = undefined
document.addEventListener('nav', async () => {
if (document.querySelector("code.mermaid")) {
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
const mermaid = mermaidImport.default
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
})
await mermaid.run({
querySelector: '.mermaid'
})
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>