790 lines
451 KiB
HTML
790 lines
451 KiB
HTML
|
<!DOCTYPE html>
|
|||
|
<html><head><title>Vim 高级功能</title><meta charSet="utf-8"/><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 rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><script async src="https://umami.7wate.com/script.js" data-website-id="c061efdc-95dd-4d21-9d04-a1ffda0a85b9"></script><script>
|
|||
|
var _hmt = _hmt || [];
|
|||
|
(function() {
|
|||
|
var hm = document.createElement("script");
|
|||
|
hm.src = "https://hm.baidu.com/hm.js?94d8ccb156eb7c65abf317e6e01cdba9";
|
|||
|
var s = document.getElementsByTagName("script")[0];
|
|||
|
s.parentNode.insertBefore(hm, s);
|
|||
|
})();
|
|||
|
</script><script async src="https://www.googletagmanager.com/gtag/js?id=G-MHMEL0F832"></script><script>
|
|||
|
(function() {
|
|||
|
window.dataLayer = window.dataLayer || [];
|
|||
|
function gtag() {
|
|||
|
window.dataLayer.push(arguments);
|
|||
|
}
|
|||
|
gtag('js', new Date());
|
|||
|
gtag('config', 'G-MHMEL0F832');
|
|||
|
})();
|
|||
|
</script><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdn.jsdelivr.net/npm/katex@0.16.0/dist/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&family=Schibsted Grotesk:wght@400;700&family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&display=swap" 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="Tech/getting-started/命令行工具/Vim/3.Vim-高级功能"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h1 class="page-title "><a href="../../../..">📚 X·Eden</a></h1><div class="spacer mobile-only"></div><div class="search "><div id="search-icon"><p>Search</p><div></div><svg tabIndex="0" aria-labelledby="title desc" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title id="title">Search</title><desc id="desc">Search</desc><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></div><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="Search for something" placeholder="Search for something"/><div id="results-container"></div></div></div></div><div class="darkmode "><input class="toggle" id="darkmode-toggle" type="checkbox" tabIndex="-1"/><label id="toggle-label-light" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="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;" xmlSpace="preserve"><title>Light mode</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-7.5-7.5 c0-4.136,3.364-7.5,7.5-7.5c4.136,0,7.5,3.364,7.5,7.5C25,21.636,21.636,25,17.5,25z"></path></svg></label><label id="toggle-label-dark" for="darkmode-toggle" tabIndex="-1"><svg xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" version="1.1" id="nightIcon" x="0px" y="0px" viewBox="0 0 100 100" style="enable-background='new 0 0 100 100'" xmlSpace="preserve"><title>Dark mode</title><path d="M96.76,66.458c-0.853-0.852-2.15-1.064-3.23-0.534c-6.063,2.991-12.858,4.571-19.655,4.571 C62.022,70.495,50.88,65.88,42.5,57.5C29.043,44.043,25.658,23.536,34.076,6.47c0.532-1.08,0.318-2.379-0.534-3.23 c-0.851-0.852-2.15-1.064-3.23-0.534c-4.918,2.427-9.375,5.619-13.246,9.491c-9.447,9.447-14.65,22.00
|
|||
|
<h3 id="高级文本操作">高级文本操作<a aria-hidden="true" tabindex="-1" href="#高级文本操作" class="internal"> §</a></h3>
|
|||
|
<h4 id="正则表达式">正则表达式<a aria-hidden="true" tabindex="-1" href="#正则表达式" class="internal"> §</a></h4>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<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>
|
|||
|
<h4 id="多行编辑">多行编辑<a aria-hidden="true" tabindex="-1" href="#多行编辑" class="internal"> §</a></h4>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<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>
|
|||
|
<h4 id="宏录制与播放">宏录制与播放<a aria-hidden="true" tabindex="-1" href="#宏录制与播放" class="internal"> §</a></h4>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<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>
|
|||
|
<h3 id="vimscript">VimScript<a aria-hidden="true" tabindex="-1" href="#vimscript" class="internal"> §</a></h3>
|
|||
|
<h4 id="基本语法">基本语法<a aria-hidden="true" tabindex="-1" href="#基本语法" class="internal"> §</a></h4>
|
|||
|
<p>VimScript 是 Vim 的脚本语言,用于扩展和定制 Vim 的功能。以下是一些基本语法:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="vim" data-theme="default"><code data-language="vim" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);">" 这是一个注释</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);">" 变量</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">let myvar </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">42</span><span style="color:var(--shiki-token-comment);"> " 整数变量</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">let mystr </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"Hello"</span><span style="color:var(--shiki-token-comment);"> " 字符串变量</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);">" 条件语句</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> myvar</span><span style="color:var(--shiki-token-keyword);"> > </span><span style="color:var(--shiki-token-constant);">0</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">echo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"Positive"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">elseif</span><span style="color:var(--shiki-color-text);"> myvar </span><span style="color:var(--shiki-token-keyword);">==</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">echo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"Zero"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">else</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">echo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"Negative"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">endif</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);">" 循环语句</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">let i </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">0</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">while</span><span style="color:var(--shiki-color-text);"> i</span><span style="color:var(--shiki-token-keyword);"> < </span><span style="color:var(--shiki-token-constant);">5</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">echo</span><span style="color:var(--shiki-color-text);"> i</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> let i </span><span style="color:var(--shiki-token-keyword);">+=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">endwhile</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">for</span><span style="color:var(--shiki-color-text);"> item </span><span style="color:var(--shiki-token-keyword);">in</span><span style="color:var(--shiki-color-text);"> [</span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-color-text);">, </span><span style="color:var(--shiki-token-constant);">2</span><span style="color:var(--shiki-color-text);">, </span><span style="color:var(--shiki-token-constant);">3</span><span style="color:var(--shiki-color-text);">, </span><span style="color:var(--shiki-token-constant);">4</span><span style="color:var(--shiki-color-text);">, </span><span style="color:var(--shiki-token-constant);">5</span><span style="color:var(--shiki-color-text);">]</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">echo</span><span style="color:var(--shiki-color-text);"> item</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">endfor</span></span></code></pre></div>
|
|||
|
<h4 id="函数和变量">函数和变量<a aria-hidden="true" tabindex="-1" href="#函数和变量" class="internal"> §</a></h4>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="vim" data-theme="default"><code data-language="vim" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);">" 定义函数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">function!</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">MyFunction</span><span style="color:var(--shiki-color-text);">(arg)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">echo</span><span style="color:var(--shiki-color-text);"> a:arg</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">endfunction</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);">" 调用函数</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">call </span><span style="color:var(--shiki-token-function);">MyFunction</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-string-expression);">"Hello"</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);">" 局部变量</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">function!</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">MyFunction</span><span style="color:var(--shiki-color-text);">()</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> let l:localVar </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"I am local"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">echo</span><span style="color:var(--shiki-color-text);"> l:localVar</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">endfunction</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);">" 全局变量</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">let g:globalVar </span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"I am global"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">echo</span><span style="color:var(--shiki-color-text);"> g:globalVar</span></span></code></pre></div>
|
|||
|
<h4 id="编写和调试简单的-vimscript-脚本">编写和调试简单的 VimScript 脚本<a aria-hidden="true" tabindex="-1" href="#编写和调试简单的-vimscript-脚本" class="internal"> §</a></h4>
|
|||
|
<p><strong>编写脚本</strong>:</p>
|
|||
|
<p>将 VimScript 代码写入 <code>.vim</code> 文件,例如 <code>myplugin.vim</code>:</p>
|
|||
|
<pre><code>" myplugin.vim
|
|||
|
function! SayHello()
|
|||
|
echo "Hello, Vim!"
|
|||
|
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 "Debug info: " . someVar
|
|||
|
</code></pre>
|
|||
|
<p>使用 <code>:messages</code> 查看所有输出信息。</p>
|
|||
|
<h3 id="自动化">自动化<a aria-hidden="true" tabindex="-1" href="#自动化" class="internal"> §</a></h3>
|
|||
|
<h4 id="使用自动命令">使用自动命令<a aria-hidden="true" tabindex="-1" href="#使用自动命令" class="internal"> §</a></h4>
|
|||
|
<p>自动命令(autocommand)允许在特定事件发生时自动执行命令,例如文件保存、打开等。</p>
|
|||
|
<pre><code>augroup MyAutoCmd
|
|||
|
autocmd!
|
|||
|
autocmd BufWritePost *.txt :echo "Text file saved!"
|
|||
|
augroup END
|
|||
|
</code></pre>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<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>
|
|||
|
<h4 id="配置文件模板">配置文件模板<a aria-hidden="true" tabindex="-1" href="#配置文件模板" class="internal"> §</a></h4>
|
|||
|
<p>在 <code>~/.vim/templates/</code> 目录中创建模板文件,例如 <code>python_template.py</code>:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="python" data-theme="default"><code data-language="python" data-theme="default"><span data-line><span style="color:var(--shiki-token-comment);">#!/usr/bin/env python3</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># -*- coding: utf-8 -*-</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);">"""</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);">Created on <++></span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);">@author: <++></span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);">"""</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">def</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">main</span><span style="color:var(--shiki-color-text);">():</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">pass</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">if</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">__name__</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">==</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"__main__"</span><span style="color:var(--shiki-token-punctuation);">:</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">main</span><span style="color:var(--shiki-token-punctuation);">()</span></span></code></pre></div>
|
|||
|
<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 aria-hidden="true" tabindex="-1" href="#高效工作流" class="internal"> §</a></h2>
|
|||
|
<h3 id="版本控制集成">版本控制集成<a aria-hidden="true" tabindex="-1" href="#版本控制集成" class="internal"> §</a></h3>
|
|||
|
<h4 id="使用-vim-fugitive">使用 Vim-fugitive<a aria-hidden="true" tabindex="-1" href="#使用-vim-fugitive" class="internal"> §</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>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<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>
|
|||
|
<h4 id="常用-git-命令">常用 Git 命令<a aria-hidden="true" tabindex="-1" href="#常用-git-命令" class="internal"> §</a></h4>
|
|||
|
<p>在 Vim 中,使用 vim-fugitive 可以执行常见的 Git 命令。同时你也可以在 Vim 外部使用 Git 命令行工具:</p>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<table><thead><tr><th>功能</th><th>命令</th><th>说明</th></tr></thead><tbody><tr><td>克隆仓库</td><td><code>git clone <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 <file></code></td><td>添加文件到索引。</td></tr><tr><td>提交更改</td><td><code>git commit -m "Commit message"</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 <new_branch></code></td><td>创建一个新的分支并切换到该分支。</td></tr><tr><td>合并分支</td><td><code>git merge <branch></code></td><td>将指定分支合并到当前分支。</td></tr></tbody></table>
|
|||
|
<h3 id="项目管理">项目管理<a aria-hidden="true" tabindex="-1" href="#项目管理" class="internal"> §</a></h3>
|
|||
|
<h4 id="nerdtree">NERDTree<a aria-hidden="true" tabindex="-1" href="#nerdtree" class="internal"> §</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 <C-n> :NERDTreeToggle<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 aria-hidden="true" tabindex="-1" href="#配置项目相关设置" class="internal"> §</a></h4>
|
|||
|
<p><strong>项目特定的 vimrc</strong></p>
|
|||
|
<p>在项目根目录下创建一个 <code>.vimrc</code> 文件,包含项目特定的 Vim 配置。然后在主 <code>vimrc</code> 中添加以下内容:</p>
|
|||
|
<pre><code>if filereadable("path/to/project/.vimrc")
|
|||
|
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 aria-hidden="true" tabindex="-1" href="#代码补全与调试" class="internal"> §</a></h3>
|
|||
|
<h4 id="youcompleteme-代码补全">YouCompleteMe 代码补全<a aria-hidden="true" tabindex="-1" href="#youcompleteme-代码补全" class="internal"> §</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 aria-hidden="true" tabindex="-1" href="#deoplete-代码补全" class="internal"> §</a></h4>
|
|||
|
<p><a href="https://github.com/Shougo/deoplete.nvim" class="external">Deoplete</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 aria-hidden="true" tabindex="-1" href="#vimspector-代码调试" class="internal"> §</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>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="json" data-theme="default"><code data-language="json" data-theme="default"><span data-line><span style="color:var(--shiki-color-text);">{</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">"configurations"</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> {</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">"Launch"</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> {</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">"adapter"</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"debugpy"</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">"configuration"</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> {</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">"request"</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"launch"</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">"program"</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"${workspaceFolder}/your_script.py"</span><span style="color:var(--shiki-token-punctuation);">,</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-keyword);">"console"</span><span style="color:var(--shiki-token-punctuation);">:</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">"integratedTerminal"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> }</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> }</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> }</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">}</span></span></code></pre></div>
|
|||
|
<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 aria-hidden="true" tabindex="-1" href="#性能优化" class="internal"> §</a></h2>
|
|||
|
<h3 id="启动优化">启动优化<a aria-hidden="true" tabindex="-1" href="#启动优化" class="internal"> §</a></h3>
|
|||
|
<h4 id="延迟加载插件">延迟加载插件<a aria-hidden="true" tabindex="-1" href="#延迟加载插件" class="internal"> §</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 aria-hidden="true" tabindex="-1" href="#精简配置文件" class="internal"> §</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>" ~/.vim/plugin/nerdtree.vim
|
|||
|
nnoremap <C-n> :NERDTreeToggle<CR>
|
|||
|
</code></pre>
|
|||
|
<p><strong>使用本地配置</strong>:</p>
|
|||
|
<p>根据项目需求,在项目根目录下创建 <code>.vimrc</code> 或 <code>.vim/</code> 目录,存放项目特定的配置文件,以避免在全局配置中添加过多内容。</p>
|
|||
|
<h3 id="系统集成">系统集成<a aria-hidden="true" tabindex="-1" href="#系统集成" class="internal"> §</a></h3>
|
|||
|
<h4 id="将-vim-与-tmux-结合使用">将 Vim 与 Tmux 结合使用<a aria-hidden="true" tabindex="-1" href="#将-vim-与-tmux-结合使用" class="internal"> §</a></h4>
|
|||
|
<p>tmux 是一个终端复用器,允许你在一个终端窗口中运行多个会话,与 Vim 结合使用可以大大提高工作效率。</p>
|
|||
|
<p><strong>安装 tmux</strong>:</p>
|
|||
|
<p>在 Linux 或 MacOS 中,可以使用包管理器安装 tmux:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">sudo</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">apt</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">tmux</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># Debian/Ubuntu</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">brew</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">install</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">tmux</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-comment);"># macOS</span></span></code></pre></div>
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
<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 "</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>
|
|||
|
<p><strong>与 Vim 集成</strong>:</p>
|
|||
|
<p>在 <code>vimrc</code> 中添加快捷键映射,方便与 tmux 交互:</p>
|
|||
|
<pre><code>nnoremap <silent> <Leader>h :TmuxNavigateLeft<CR>
|
|||
|
nnoremap <silent> <Leader>j :TmuxNavigateDown<CR>
|
|||
|
nnoremap <silent> <Leader>k :TmuxNavigateUp<CR>
|
|||
|
nnoremap <silent> <Leader>l :TmuxNavigateRight<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 aria-hidden="true" tabindex="-1" href="#在终端中使用-vim" class="internal"> §</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>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> vi</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">"vim"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> svi</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">"sudo vim"</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">alias</span><span style="color:var(--shiki-color-text);"> vrc</span><span style="color:var(--shiki-token-keyword);">=</span><span style="color:var(--shiki-token-string-expression);">"vim ~/.vimrc"</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-comment);"># 快速打开项目目录中的 Vim</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">function</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">vproj</span><span style="color:var(--shiki-token-punctuation);">()</span><span style="color:var(--shiki-color-text);"> {</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">cd</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">~/projects/</span><span style="color:var(--shiki-color-text);">$1 </span><span style="color:var(--shiki-token-punctuation);">&&</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-function);">vim</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-color-text);">}</span></span></code></pre></div>
|
|||
|
<p><strong>使用 FZF 进行文件搜索</strong>:</p>
|
|||
|
<p>FZF 是一个命令行模糊查找工具,可以与 Vim 集成,快速搜索和打开文件。</p>
|
|||
|
<ol>
|
|||
|
<li>
|
|||
|
<p>安装 FZF:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">clone</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">--depth</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-constant);">1</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">https://github.com/junegunn/fzf.git</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">~/.fzf</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-keyword);">~</span><span style="color:var(--shiki-color-text);">/.fzf/install</span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
<li>
|
|||
|
<p>在 <code>vimrc</code> 中配置 FZF:</p>
|
|||
|
<div data-rehype-pretty-code-fragment><pre style="background-color:var(--shiki-color-background);" tabindex="0" data-language="shell" data-theme="default"><code data-language="shell" data-theme="default"><span data-line><span style="color:var(--shiki-token-function);">call</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">plug#begin</span><span style="color:var(--shiki-color-text);">(</span><span style="color:var(--shiki-token-function);">'~/.vim/plugged'</span><span style="color:var(--shiki-color-text);">)</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">Plug</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'junegunn/fzf'</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">Plug</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string-expression);">'junegunn/fzf.vim'</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">call</span><span style="color:var(--shiki-color-text);"> </span><span style="color:var(--shiki-token-string);">plug#end</span><span style="color:var(--shiki-color-text);">()</span></span>
|
|||
|
<span data-line> </span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">" 使用 FZF 查找文件</span></span>
|
|||
|
<span data-line><span style="color:var(--shiki-token-function);">nnoremap <Leader>f :Files<CR></span></span></code></pre></div>
|
|||
|
</li>
|
|||
|
</ol></article></div><div class="right sidebar"><div class="graph "><h3>Graph View</h3><div class="graph-outer"><div id="graph-container" data-cfg="{"drag":true,"zoom":true,"depth":1,"scale":1.1,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[]}"></div><svg version="1.1" id="global-graph-icon" xmlns="http://www.w3.org/2000/svg" xmlnsXlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xmlSpace="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></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{"drag":true,"zoom":true,"depth":-1,"scale":0.9,"repelForce":0.5,"centerForce":0.3,"linkDistance":30,"fontSize":0.6,"opacityScale":1,"showTags":true,"removeTags":[]}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class><h3>Table of Contents</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"><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="backlinks "><h3>Backlinks</h3><ul class="overflow"><li>No backlinks found</li></ul></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{"path":"Basics","collapsed":true},{"path":"Basics/english","collapsed":true},{"path":"Blog","collapsed":true},{"path":"Book","collapsed":true},{"path":"Book/个人成长","collapsed":true},{"path":"Book/医学健康","collapsed":true},{"path":"Book/历史","collapsed":true},{"path":"Book/哲学宗教","collapsed":true},{"path":"Book/心理","collapsed":true},{"path":"Book/政治军事","collapsed":true},{"path":"Book/教育学习","collapsed":true},{"path":"Book/文学","collapsed":true},{"path":"Book/生活百科","collapsed":true},{"path":"Book/社会文化","collapsed":true},{"path":"Book/科学技术","collapsed":true},{"path":"Book/经济理财","collapsed":true},{"path":"Book/艺术","collapsed":true},{"path":"Book/计算机","collapsed":true},{"path":"Company","collapsed":true},{"path":"Company/信息化","collapsed":true},{"path":"Company/信息化/规范","collapsed":true},{"path":"Culture","collapsed":true},{"path":"Government","collapsed":true},{"path":"Government/中国","collapsed":true},{"path":"Government/中国/标准","collapsed":true},{"path":"Government/中国/法律","collapsed":t
|
|||
|
function toggleCallout() {
|
|||
|
const outerBlock = this.parentElement;
|
|||
|
outerBlock.classList.toggle(`is-collapsed`);
|
|||
|
const collapsed = outerBlock.classList.contains(`is-collapsed`);
|
|||
|
const height = collapsed ? this.scrollHeight : outerBlock.scrollHeight;
|
|||
|
outerBlock.style.maxHeight = height + `px`;
|
|||
|
let current = outerBlock;
|
|||
|
let parent = outerBlock.parentElement;
|
|||
|
while (parent) {
|
|||
|
if (!parent.classList.contains(`callout`)) {
|
|||
|
return;
|
|||
|
}
|
|||
|
const collapsed2 = parent.classList.contains(`is-collapsed`);
|
|||
|
const height2 = collapsed2 ? parent.scrollHeight : parent.scrollHeight + current.scrollHeight;
|
|||
|
parent.style.maxHeight = height2 + `px`;
|
|||
|
current = parent;
|
|||
|
parent = parent.parentElement;
|
|||
|
}
|
|||
|
}
|
|||
|
function setupCallout() {
|
|||
|
const collapsible = document.getElementsByClassName(
|
|||
|
`callout is-collapsible`
|
|||
|
);
|
|||
|
for (const div of collapsible) {
|
|||
|
const title = div.firstElementChild;
|
|||
|
if (title) {
|
|||
|
title.removeEventListener(`click`, toggleCallout);
|
|||
|
title.addEventListener(`click`, toggleCallout);
|
|||
|
const collapsed = div.classList.contains(`is-collapsed`);
|
|||
|
const height = collapsed ? title.scrollHeight : div.scrollHeight;
|
|||
|
div.style.maxHeight = height + `px`;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
document.addEventListener(`nav`, setupCallout);
|
|||
|
window.addEventListener(`resize`, setupCallout);
|
|||
|
</script><script type="module">
|
|||
|
import mermaid from 'https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.esm.min.mjs';
|
|||
|
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
|
|||
|
mermaid.initialize({
|
|||
|
startOnLoad: false,
|
|||
|
securityLevel: 'loose',
|
|||
|
theme: darkMode ? 'dark' : 'default'
|
|||
|
});
|
|||
|
document.addEventListener('nav', async () => {
|
|||
|
await mermaid.run({
|
|||
|
querySelector: '.mermaid'
|
|||
|
})
|
|||
|
});
|
|||
|
</script><script src="https://cdn.jsdelivr.net/npm/katex@0.16.7/dist/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>
|