|
5 | 5 | <link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
|
6 | 6 | <link href="https://learnbyexample.github.io"/>
|
7 | 7 | <generator uri="https://www.getzola.org/">Zola</generator>
|
8 |
| - <updated>2023-03-11T00:00:00+00:00</updated> |
| 8 | + <updated>2023-03-14T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>Vim tip 23: editing lines filtered by a pattern</title> |
| 12 | + <published>2023-03-14T00:00:00+00:00</published> |
| 13 | + <updated>2023-03-14T00:00:00+00:00</updated> |
| 14 | + <link rel="alternate" href="https://learnbyexample.github.io/tips/vim-tip-23/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/vim-tip-23/</id> |
| 16 | + <content type="html"><p>The syntax for <code>g</code> command (short for <code>global</code>) is shown below:</p> |
| 17 | +<pre style="background-color:#f5f5f5;color:#1f1f1f;"><code><span>:[range]g[lobal]/{pattern}/[cmd] |
| 18 | +</span></code></pre> |
| 19 | +<p>This command is used to edit lines that are first filtered based on a <code>searchpattern</code>.</p> |
| 20 | +<ul> |
| 21 | +<li><kbd>:g/call/d</kbd> delete all lines containing <code>call</code> |
| 22 | +<ul> |
| 23 | +<li>similar to the <code>d</code> Normal mode command, the deleted contents will be saved to the default <code>&quot;</code> register</li> |
| 24 | +<li><kbd>:g/call/d a</kbd> in addition to the default register, the deleted content will also be stored in the <code>&quot;a</code> register</li> |
| 25 | +<li><kbd>:g/call/d _</kbd> deleted content won't be saved anywhere, since it uses the black hole register</li> |
| 26 | +</ul> |
| 27 | +</li> |
| 28 | +<li><kbd>:g/^#/t0</kbd> copy all lines starting with <code>#</code> to the start of the file</li> |
| 29 | +<li><kbd>:1,5 g/call/d</kbd> delete all lines containing <code>call</code> only for the first five lines</li> |
| 30 | +<li><kbd>:g/cat/ s/animal/mammal/g</kbd> replace <code>animal</code> with <code>mammal</code> only for the lines containing <code>cat</code></li> |
| 31 | +<li><kbd>:.,.+20 g/^#/ normal &gt;&gt;</kbd> indent the current line and the next <code>20</code> lines only if the line starts with <code>#</code> |
| 32 | +<ul> |
| 33 | +<li>Note the use of <code>normal</code> when you need to use Normal mode commands on the filtered lines</li> |
| 34 | +<li>Use <code>normal!</code> if you don't want user defined mappings to be considered</li> |
| 35 | +</ul> |
| 36 | +</li> |
| 37 | +</ul> |
| 38 | +<p>You can use <code>g!</code> or <code>v</code> to act on lines <em>not</em> satisfying the filtering condition.</p> |
| 39 | +<ul> |
| 40 | +<li><kbd>:v/jump/d</kbd> delete all lines <em>not</em> containing <code>jump</code> |
| 41 | +<ul> |
| 42 | +<li>same as <kbd>:g!/jump/d</kbd></li> |
| 43 | +</ul> |
| 44 | +</li> |
| 45 | +</ul> |
| 46 | +<p><img src="/images/info.svg" alt="info" /> In addition to the <code>/</code> delimiter, you can also use any single byte character other than alphabets, <code>\</code>, <code>&quot;</code> or <code>|</code>.</p> |
| 47 | +<p><img src="/images/info.svg" alt="info" /> See <a href="https://vimhelp.org/repeat.txt.html#%3Aglobal">:h :g</a> for more details.</p> |
| 48 | +<p><strong>Video demo</strong>:</p> |
| 49 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/uQKaAOKgr2o" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 50 | +<br> |
| 51 | +<p><img src="/images/info.svg" alt="info" /> See also my <a href="https://github.com/learnbyexample/vim_reference">Vim Reference Guide</a> and <a href="https://learnbyexample.github.io/curated_resources/vim.html">curated list of resources for Vim</a>.</p> |
| 52 | +</content> |
| 53 | + </entry> |
10 | 54 | <entry xml:lang="en">
|
11 | 55 | <title>Python Regular Expressions Playground</title>
|
12 | 56 | <published>2023-03-11T00:00:00+00:00</published>
|
|
0 commit comments