Skip to content

Commit 93e023e

Browse files
added 23rd vim tip
1 parent 2473eeb commit 93e023e

File tree

9 files changed

+58
-7
lines changed

9 files changed

+58
-7
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ I'm addicted to reading fantasy/sci-fi books, so I have a [blog](https://learnby
6666

6767
## Tips
6868

69+
* [Vim tip 23: editing lines filtered by a pattern](https://learnbyexample.github.io/tips/vim-tip-23/)
6970
* [CLI tip 24: inserting file contents one line at a time](https://learnbyexample.github.io/tips/cli-tip-24/)
7071
* [Python tip 24: modifying list using insert and slice](https://learnbyexample.github.io/tips/python-tip-24/)
7172
* [Vim tip 22: word and WORD motions](https://learnbyexample.github.io/tips/vim-tip-22/)

Diff for: atom.xml

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

Diff for: sitemap.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@
420420
</url>
421421
<url>
422422
<loc>https://learnbyexample.github.io/tips/</loc>
423-
<lastmod>2023-03-07</lastmod>
423+
<lastmod>2023-03-14</lastmod>
424424
</url>
425425
<url>
426426
<loc>https://learnbyexample.github.io/tips/cli-tip-1/</loc>
@@ -674,6 +674,10 @@
674674
<loc>https://learnbyexample.github.io/tips/vim-tip-22/</loc>
675675
<lastmod>2023-02-10</lastmod>
676676
</url>
677+
<url>
678+
<loc>https://learnbyexample.github.io/tips/vim-tip-23/</loc>
679+
<lastmod>2023-03-14</lastmod>
680+
</url>
677681
<url>
678682
<loc>https://learnbyexample.github.io/tips/vim-tip-3/</loc>
679683
<lastmod>2022-01-18</lastmod>

Diff for: tags/index.html

+1-1
Large diffs are not rendered by default.

Diff for: tags/tip/index.html

+1-1
Large diffs are not rendered by default.

Diff for: tags/vim/index.html

+1-1
Large diffs are not rendered by default.

Diff for: tips/index.html

+1-1
Large diffs are not rendered by default.

Diff for: tips/vim-tip-22/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!doctype html><html lang=en><head><meta content="IE=edge" http-equiv=X-UA-Compatible><meta content="text/html; charset=utf-8" http-equiv=content-type><meta content="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Vim tip 22: word and WORD motions</title><link href=https://learnbyexample.github.io/atom.xml rel=alternate title=RSS type=application/atom+xml><script src=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><link href=https://learnbyexample.github.io/site.css rel=stylesheet><meta content=learnbyexample property=og:title><meta content=website property=og:type><meta content="Learn Python, Regex, Linux, Scripting, Vim, Ebooks, Self-Publishing and Interesting Tech Nuggets." property=og:description><meta content=https://learnbyexample.github.io property=og:url><meta content=https://learnbyexample.github.io/images/learnbyexample.png property=og:image><meta content=1280 property=og:image:width><meta content=640 property=og:image:height><meta content=summary_large_image property=twitter:card><meta content=@learn_byexample property=twitter:site><link href=https://learnbyexample.github.io/favicon.svg rel=icon><link rel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><div class=container><div class=mobile-navbar id=mobile-navbar><div class=mobile-header-logo><a class=logo href=/>learnbyexample</a></div><div class="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><nav class="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ul class=mobile-menu-list><li class=mobile-menu-item><a href=https://learnbyexample.github.io/books> Books </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/mini> Mini </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tips> Tips </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tags> Tags </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/about> About </a></ul></nav><header id=header><div class=logo><a href=https://learnbyexample.github.io>learnbyexample</a></div><nav class=menu><ul><li><a href=https://learnbyexample.github.io/books> Books </a><li><a href=https://learnbyexample.github.io/mini> Mini </a><li><a href=https://learnbyexample.github.io/tips> Tips </a><li><a href=https://learnbyexample.github.io/tags> Tags </a><li><a href=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><div class=content id=mobile-panel><article class=post><header class=post__header><h1 class=post__title><a href=https://learnbyexample.github.io/tips/vim-tip-22/>Vim tip 22: word and WORD motions</a></h1><div class=post__meta><span class=post__time>2023-02-10</span></div></header><div class=post-content><p>Definitions from <a href=https://vimhelp.org/motion.txt.html#word>:h word</a> and <a href=https://vimhelp.org/motion.txt.html#WORD>:h WORD</a> are quoted below to explain the difference between <strong>word</strong> and <strong>WORD</strong>.<blockquote><p><strong>word</strong> A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, <code>&LTEOL></code>). This can be changed with the <code>iskeyword</code> option. An empty line is also considered to be a word.</blockquote><blockquote><p><strong>WORD</strong> A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.</blockquote><p>word based motions:<ul><li><kbd>w</kbd> move to the start of the next word<li><kbd>b</kbd> move to the beginning of the current word if the cursor is <em>not</em> at the start of word. Otherwise, move to the beginning of the previous word<li><kbd>e</kbd> move to the end of the current word if cursor is <em>not</em> at the end of word. Otherwise, move to the end of next word<li><kbd>ge</kbd> move to the end of the previous word<li><kbd>3w</kbd> move 3 words forward <ul><li>Similarly, a number can be prefixed for all the other commands discussed here</ul></ul><p>WORD based motions:<ul><li><kbd>W</kbd> move to the start of the next WORD <ul><li><code>192.1.168.43;hello</code> is considered as a single WORD, but has multiple words</ul><li><kbd>B</kbd> move to the beginning of the current WORD if the cursor is <em>not</em> at the start of WORD. Otherwise, move to the beginning of the previous WORD<li><kbd>E</kbd> move to the end of the current WORD if cursor is <em>not</em> at the end of WORD. Otherwise, move to the end of next WORD<li><kbd>gE</kbd> move to the end of the previous WORD</ul><p><img alt=info src=/images/info.svg> All of these motions will work across lines. For example, if the cursor is on the last word of a line, pressing <kbd>w</kbd> will move to the start of the first word in the next line.<p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/-1MKL82cbw8 width=560></iframe></p><br><p><img alt=info src=/images/info.svg> 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>.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/vim/>#vim</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=next href=https://learnbyexample.github.io/tips/vim-tip-21/>Vim tip 21: working with tabs →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>
1+
<!doctype html><html lang=en><head><meta content="IE=edge" http-equiv=X-UA-Compatible><meta content="text/html; charset=utf-8" http-equiv=content-type><meta content="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Vim tip 22: word and WORD motions</title><link href=https://learnbyexample.github.io/atom.xml rel=alternate title=RSS type=application/atom+xml><script src=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><link href=https://learnbyexample.github.io/site.css rel=stylesheet><meta content=learnbyexample property=og:title><meta content=website property=og:type><meta content="Learn Python, Regex, Linux, Scripting, Vim, Ebooks, Self-Publishing and Interesting Tech Nuggets." property=og:description><meta content=https://learnbyexample.github.io property=og:url><meta content=https://learnbyexample.github.io/images/learnbyexample.png property=og:image><meta content=1280 property=og:image:width><meta content=640 property=og:image:height><meta content=summary_large_image property=twitter:card><meta content=@learn_byexample property=twitter:site><link href=https://learnbyexample.github.io/favicon.svg rel=icon><link rel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><div class=container><div class=mobile-navbar id=mobile-navbar><div class=mobile-header-logo><a class=logo href=/>learnbyexample</a></div><div class="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><nav class="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ul class=mobile-menu-list><li class=mobile-menu-item><a href=https://learnbyexample.github.io/books> Books </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/mini> Mini </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tips> Tips </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tags> Tags </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/about> About </a></ul></nav><header id=header><div class=logo><a href=https://learnbyexample.github.io>learnbyexample</a></div><nav class=menu><ul><li><a href=https://learnbyexample.github.io/books> Books </a><li><a href=https://learnbyexample.github.io/mini> Mini </a><li><a href=https://learnbyexample.github.io/tips> Tips </a><li><a href=https://learnbyexample.github.io/tags> Tags </a><li><a href=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><div class=content id=mobile-panel><article class=post><header class=post__header><h1 class=post__title><a href=https://learnbyexample.github.io/tips/vim-tip-22/>Vim tip 22: word and WORD motions</a></h1><div class=post__meta><span class=post__time>2023-02-10</span></div></header><div class=post-content><p>Definitions from <a href=https://vimhelp.org/motion.txt.html#word>:h word</a> and <a href=https://vimhelp.org/motion.txt.html#WORD>:h WORD</a> are quoted below to explain the difference between <strong>word</strong> and <strong>WORD</strong>.<blockquote><p><strong>word</strong> A word consists of a sequence of letters, digits and underscores, or a sequence of other non-blank characters, separated with white space (spaces, tabs, <code>&LTEOL></code>). This can be changed with the <code>iskeyword</code> option. An empty line is also considered to be a word.</blockquote><blockquote><p><strong>WORD</strong> A WORD consists of a sequence of non-blank characters, separated with white space. An empty line is also considered to be a WORD.</blockquote><p>word based motions:<ul><li><kbd>w</kbd> move to the start of the next word<li><kbd>b</kbd> move to the beginning of the current word if the cursor is <em>not</em> at the start of word. Otherwise, move to the beginning of the previous word<li><kbd>e</kbd> move to the end of the current word if cursor is <em>not</em> at the end of word. Otherwise, move to the end of next word<li><kbd>ge</kbd> move to the end of the previous word<li><kbd>3w</kbd> move 3 words forward <ul><li>Similarly, a number can be prefixed for all the other commands discussed here</ul></ul><p>WORD based motions:<ul><li><kbd>W</kbd> move to the start of the next WORD <ul><li><code>192.1.168.43;hello</code> is considered as a single WORD, but has multiple words</ul><li><kbd>B</kbd> move to the beginning of the current WORD if the cursor is <em>not</em> at the start of WORD. Otherwise, move to the beginning of the previous WORD<li><kbd>E</kbd> move to the end of the current WORD if cursor is <em>not</em> at the end of WORD. Otherwise, move to the end of next WORD<li><kbd>gE</kbd> move to the end of the previous WORD</ul><p><img alt=info src=/images/info.svg> All of these motions will work across lines. For example, if the cursor is on the last word of a line, pressing <kbd>w</kbd> will move to the start of the first word in the next line.<p><strong>Video demo</strong>:<p align=center><iframe allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" title="YouTube video player" allowfullscreen frameborder=0 height=315 loading=lazy src=https://www.youtube.com/embed/-1MKL82cbw8 width=560></iframe></p><br><p><img alt=info src=/images/info.svg> 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>.</div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/vim/>#vim</a><a href=https://learnbyexample.github.io/tags/tip/>#tip</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=previous href=https://learnbyexample.github.io/tips/vim-tip-23/>← Vim tip 23: editing lines filtered by a pattern</a><br><p><a class=next href=https://learnbyexample.github.io/tips/vim-tip-21/>Vim tip 21: working with tabs →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>

0 commit comments

Comments
 (0)