Skip to content

Commit 3daf8d4

Browse files
added 27th vim tip
1 parent ef87ff9 commit 3daf8d4

File tree

9 files changed

+57
-7
lines changed

9 files changed

+57
-7
lines changed

Diff for: README.md

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

6969
## Tips
7070

71+
* [Vim tip 27: regexp anchors](https://learnbyexample.github.io/tips/vim-tip-27/)
7172
* [CLI tip 28: substitute specific occurrence with GNU sed](https://learnbyexample.github.io/tips/cli-tip-28/)
7273
* [Python tip 28: string concatenation and repetition](https://learnbyexample.github.io/tips/python-tip-28/)
7374
* [Vim tip 26: executing shell commands](https://learnbyexample.github.io/tips/vim-tip-26/)

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-05-30T00:00:00+00:00</updated>
8+
<updated>2023-06-05T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
10+
<entry xml:lang="en">
11+
<title>Vim tip 27: regexp anchors</title>
12+
<published>2023-06-05T00:00:00+00:00</published>
13+
<updated>2023-06-05T00:00:00+00:00</updated>
14+
<link rel="alternate" href="https://learnbyexample.github.io/tips/vim-tip-27/" type="text/html"/>
15+
<id>https://learnbyexample.github.io/tips/vim-tip-27/</id>
16+
<content type="html">&lt;p&gt;By default, regexp matches anywhere in the text. You can use line and word anchors to specify additional restrictions regarding the position of matches. These restrictions are made possible by assigning special meaning to certain characters (&lt;strong&gt;metacharacters&lt;&#x2F;strong&gt;) and escape sequences.&lt;&#x2F;p&gt;
17+
&lt;ul&gt;
18+
&lt;li&gt;&lt;code&gt;^&lt;&#x2F;code&gt; restricts the match to the start-of-line
19+
&lt;ul&gt;
20+
&lt;li&gt;&lt;code&gt;^This&lt;&#x2F;code&gt; matches &lt;code&gt;This is a sample&lt;&#x2F;code&gt; but not &lt;code&gt;Do This&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
21+
&lt;&#x2F;ul&gt;
22+
&lt;&#x2F;li&gt;
23+
&lt;li&gt;&lt;code&gt;$&lt;&#x2F;code&gt; restricts the match to the end-of-line
24+
&lt;ul&gt;
25+
&lt;li&gt;&lt;code&gt;)$&lt;&#x2F;code&gt; matches &lt;code&gt;apple (5)&lt;&#x2F;code&gt; but not &lt;code&gt;def greeting():&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
26+
&lt;&#x2F;ul&gt;
27+
&lt;&#x2F;li&gt;
28+
&lt;li&gt;&lt;code&gt;^$&lt;&#x2F;code&gt; match empty line&lt;&#x2F;li&gt;
29+
&lt;li&gt;&lt;code&gt;\&amp;lt;pattern&lt;&#x2F;code&gt; restricts the match to the start of a word
30+
&lt;ul&gt;
31+
&lt;li&gt;word characters include alphabets, digits and underscore&lt;&#x2F;li&gt;
32+
&lt;li&gt;&lt;code&gt;\&amp;lt;his&lt;&#x2F;code&gt; matches &lt;code&gt;his&lt;&#x2F;code&gt; or &lt;code&gt;to-his&lt;&#x2F;code&gt; or &lt;code&gt;history&lt;&#x2F;code&gt; but not &lt;code&gt;this&lt;&#x2F;code&gt; or &lt;code&gt;_hist&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
33+
&lt;&#x2F;ul&gt;
34+
&lt;&#x2F;li&gt;
35+
&lt;li&gt;&lt;code&gt;pattern\&amp;gt;&lt;&#x2F;code&gt; restricts the match to the end of a word
36+
&lt;ul&gt;
37+
&lt;li&gt;&lt;code&gt;his\&amp;gt;&lt;&#x2F;code&gt; matches &lt;code&gt;his&lt;&#x2F;code&gt; or &lt;code&gt;to-his&lt;&#x2F;code&gt; or &lt;code&gt;this&lt;&#x2F;code&gt; but not &lt;code&gt;history&lt;&#x2F;code&gt; or &lt;code&gt;_hist&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
38+
&lt;&#x2F;ul&gt;
39+
&lt;&#x2F;li&gt;
40+
&lt;li&gt;&lt;code&gt;\&amp;lt;pattern\&amp;gt;&lt;&#x2F;code&gt; restricts the match between start of a word and end of a word
41+
&lt;ul&gt;
42+
&lt;li&gt;&lt;code&gt;\&amp;lt;his\&amp;gt;&lt;&#x2F;code&gt; matches &lt;code&gt;his&lt;&#x2F;code&gt; or &lt;code&gt;to-his&lt;&#x2F;code&gt; but not &lt;code&gt;this&lt;&#x2F;code&gt; or &lt;code&gt;history&lt;&#x2F;code&gt; or &lt;code&gt;_hist&lt;&#x2F;code&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; End-of-line can be &lt;code&gt;\r&lt;&#x2F;code&gt; (carriage return), &lt;code&gt;\n&lt;&#x2F;code&gt; (newline) or &lt;code&gt;\r\n&lt;&#x2F;code&gt; depending on your system and &lt;code&gt;fileformat&lt;&#x2F;code&gt; setting.&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;pattern.txt.html#pattern-atoms&quot;&gt;:h pattern-atoms&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;AJNWtpKA2zM&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>CLI tip 28: substitute specific occurrence with GNU sed</title>
1256
<published>2023-05-30T00:00:00+00:00</published>

Diff for: sitemap.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@
428428
</url>
429429
<url>
430430
<loc>https://learnbyexample.github.io/tips/</loc>
431-
<lastmod>2023-05-30</lastmod>
431+
<lastmod>2023-06-05</lastmod>
432432
</url>
433433
<url>
434434
<loc>https://learnbyexample.github.io/tips/cli-tip-1/</loc>
@@ -730,6 +730,10 @@
730730
<loc>https://learnbyexample.github.io/tips/vim-tip-26/</loc>
731731
<lastmod>2023-05-16</lastmod>
732732
</url>
733+
<url>
734+
<loc>https://learnbyexample.github.io/tips/vim-tip-27/</loc>
735+
<lastmod>2023-06-05</lastmod>
736+
</url>
733737
<url>
734738
<loc>https://learnbyexample.github.io/tips/vim-tip-3/</loc>
735739
<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-26/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 26: executing shell commands</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-26/>Vim tip 26: executing shell commands</a></h1><div class=post__meta><span class=post__time>2023-05-16</span></div></header><div class=post-content><p>You can execute external commands from within Vim. Here are some examples:<ul><li><kbd>:!ls</kbd> execute the given shell command and display output <ul><li>the results are displayed as part of an expanded Command-line area, doesn't change contents of the file</ul><li><kbd>:.!date</kbd> replace the current line with the output of the given command <ul><li>pressing <kbd>!!</kbd> in Normal mode will also result in <kbd>:.!</kbd><li><code>!</code> waits for motion similar to <code>d</code> and <code>y</code> commands, <kbd>!G</kbd> will give <kbd>:.,$!</kbd></ul><li><kbd>:%!sort</kbd> sort all the lines <ul><li>recall that <code>%</code> is a shortcut for the range <code>1,$</code><li>note that this executes an external command, not the built-in <code>:sort</code> command</ul><li><kbd>:3,8!sort</kbd> sort only lines <code>3</code> to <code>8</code><li><kbd>:r!date</kbd> insert output of the given command below the current line<li><kbd>:r report.log</kbd> insert contents of the given file below the current line <ul><li>Note that <code>!</code> is not used here since there is no shell command</ul><li><kbd>:.!grep '^Help ' %</kbd> replace the current line with all the lines starting with <code>Help</code> in the current file <ul><li><code>%</code> here refers to current file contents</ul><li><kbd>:sh</kbd> open a shell session within Vim <ul><li>use <code>exit</code> command to quit the session</ul></ul><p><img alt=info src=/images/info.svg> See <a href=https://vimhelp.org/various.txt.html#%3A%21cmd>:h :!</a>, <a href=https://vimhelp.org/various.txt.html#%3Ash>:h :sh</a> and <a href=https://vimhelp.org/insert.txt.html#%3Ar>:h :r</a> for more details.<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/STSZt2c1rSA 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-25/>Vim tip 25: substitute flags →</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 26: executing shell commands</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-26/>Vim tip 26: executing shell commands</a></h1><div class=post__meta><span class=post__time>2023-05-16</span></div></header><div class=post-content><p>You can execute external commands from within Vim. Here are some examples:<ul><li><kbd>:!ls</kbd> execute the given shell command and display output <ul><li>the results are displayed as part of an expanded Command-line area, doesn't change contents of the file</ul><li><kbd>:.!date</kbd> replace the current line with the output of the given command <ul><li>pressing <kbd>!!</kbd> in Normal mode will also result in <kbd>:.!</kbd><li><code>!</code> waits for motion similar to <code>d</code> and <code>y</code> commands, <kbd>!G</kbd> will give <kbd>:.,$!</kbd></ul><li><kbd>:%!sort</kbd> sort all the lines <ul><li>recall that <code>%</code> is a shortcut for the range <code>1,$</code><li>note that this executes an external command, not the built-in <code>:sort</code> command</ul><li><kbd>:3,8!sort</kbd> sort only lines <code>3</code> to <code>8</code><li><kbd>:r!date</kbd> insert output of the given command below the current line<li><kbd>:r report.log</kbd> insert contents of the given file below the current line <ul><li>Note that <code>!</code> is not used here since there is no shell command</ul><li><kbd>:.!grep '^Help ' %</kbd> replace the current line with all the lines starting with <code>Help</code> in the current file <ul><li><code>%</code> here refers to current file contents</ul><li><kbd>:sh</kbd> open a shell session within Vim <ul><li>use <code>exit</code> command to quit the session</ul></ul><p><img alt=info src=/images/info.svg> See <a href=https://vimhelp.org/various.txt.html#%3A%21cmd>:h :!</a>, <a href=https://vimhelp.org/various.txt.html#%3Ash>:h :sh</a> and <a href=https://vimhelp.org/insert.txt.html#%3Ar>:h :r</a> for more details.<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/STSZt2c1rSA 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-27/>← Vim tip 27: regexp anchors</a><br><p><a class=next href=https://learnbyexample.github.io/tips/vim-tip-25/>Vim tip 25: substitute flags →</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)