Skip to content

Commit 2eddf80

Browse files
added 26th Vim tip
1 parent c431d34 commit 2eddf80

File tree

9 files changed

+63
-7
lines changed

9 files changed

+63
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
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 26: executing shell commands](https://learnbyexample.github.io/tips/vim-tip-26/)
7172
* [CLI tip 27: reverse text line wise with tac](https://learnbyexample.github.io/tips/cli-tip-27/)
7273
* [Python tip 27: enumerate() function](https://learnbyexample.github.io/tips/python-tip-27/)
7374
* [Vim tip 25: substitute flags](https://learnbyexample.github.io/tips/vim-tip-25/)

atom.xml

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,58 @@
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-12T00:00:00+00:00</updated>
8+
<updated>2023-05-16T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
10+
<entry xml:lang="en">
11+
<title>Vim tip 26: executing shell commands</title>
12+
<published>2023-05-16T00:00:00+00:00</published>
13+
<updated>2023-05-16T00:00:00+00:00</updated>
14+
<link rel="alternate" href="https://learnbyexample.github.io/tips/vim-tip-26/" type="text/html"/>
15+
<id>https://learnbyexample.github.io/tips/vim-tip-26/</id>
16+
<content type="html">&lt;p&gt;You can execute external commands from within Vim. Here are some examples:&lt;&#x2F;p&gt;
17+
&lt;ul&gt;
18+
&lt;li&gt;&lt;kbd&gt;:!ls&lt;&#x2F;kbd&gt; execute the given shell command and display output
19+
&lt;ul&gt;
20+
&lt;li&gt;the results are displayed as part of an expanded Command-line area, doesn&#x27;t change contents of the file&lt;&#x2F;li&gt;
21+
&lt;&#x2F;ul&gt;
22+
&lt;&#x2F;li&gt;
23+
&lt;li&gt;&lt;kbd&gt;:.!date&lt;&#x2F;kbd&gt; replace the current line with the output of the given command
24+
&lt;ul&gt;
25+
&lt;li&gt;pressing &lt;kbd&gt;!!&lt;&#x2F;kbd&gt; in Normal mode will also result in &lt;kbd&gt;:.!&lt;&#x2F;kbd&gt;&lt;&#x2F;li&gt;
26+
&lt;li&gt;&lt;code&gt;!&lt;&#x2F;code&gt; waits for motion similar to &lt;code&gt;d&lt;&#x2F;code&gt; and &lt;code&gt;y&lt;&#x2F;code&gt; commands, &lt;kbd&gt;!G&lt;&#x2F;kbd&gt; will give &lt;kbd&gt;:.,$!&lt;&#x2F;kbd&gt;&lt;&#x2F;li&gt;
27+
&lt;&#x2F;ul&gt;
28+
&lt;&#x2F;li&gt;
29+
&lt;li&gt;&lt;kbd&gt;:%!sort&lt;&#x2F;kbd&gt; sort all the lines
30+
&lt;ul&gt;
31+
&lt;li&gt;recall that &lt;code&gt;%&lt;&#x2F;code&gt; is a shortcut for the range &lt;code&gt;1,$&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
32+
&lt;li&gt;note that this executes an external command, not the built-in &lt;code&gt;:sort&lt;&#x2F;code&gt; command&lt;&#x2F;li&gt;
33+
&lt;&#x2F;ul&gt;
34+
&lt;&#x2F;li&gt;
35+
&lt;li&gt;&lt;kbd&gt;:3,8!sort&lt;&#x2F;kbd&gt; sort only lines &lt;code&gt;3&lt;&#x2F;code&gt; to &lt;code&gt;8&lt;&#x2F;code&gt;&lt;&#x2F;li&gt;
36+
&lt;li&gt;&lt;kbd&gt;:r!date&lt;&#x2F;kbd&gt; insert output of the given command below the current line&lt;&#x2F;li&gt;
37+
&lt;li&gt;&lt;kbd&gt;:r report.log&lt;&#x2F;kbd&gt; insert contents of the given file below the current line
38+
&lt;ul&gt;
39+
&lt;li&gt;Note that &lt;code&gt;!&lt;&#x2F;code&gt; is not used here since there is no shell command&lt;&#x2F;li&gt;
40+
&lt;&#x2F;ul&gt;
41+
&lt;&#x2F;li&gt;
42+
&lt;li&gt;&lt;kbd&gt;:.!grep &#x27;^Help &#x27; %&lt;&#x2F;kbd&gt; replace the current line with all the lines starting with &lt;code&gt;Help&lt;&#x2F;code&gt; in the current file
43+
&lt;ul&gt;
44+
&lt;li&gt;&lt;code&gt;%&lt;&#x2F;code&gt; here refers to current file contents&lt;&#x2F;li&gt;
45+
&lt;&#x2F;ul&gt;
46+
&lt;&#x2F;li&gt;
47+
&lt;li&gt;&lt;kbd&gt;:sh&lt;&#x2F;kbd&gt; open a shell session within Vim
48+
&lt;ul&gt;
49+
&lt;li&gt;use &lt;code&gt;exit&lt;&#x2F;code&gt; command to quit the session&lt;&#x2F;li&gt;
50+
&lt;&#x2F;ul&gt;
51+
&lt;&#x2F;li&gt;
52+
&lt;&#x2F;ul&gt;
53+
&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;various.txt.html#%3A%21cmd&quot;&gt;:h :!&lt;&#x2F;a&gt;, &lt;a href=&quot;https:&#x2F;&#x2F;vimhelp.org&#x2F;various.txt.html#%3Ash&quot;&gt;:h :sh&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;vimhelp.org&#x2F;insert.txt.html#%3Ar&quot;&gt;:h :r&lt;&#x2F;a&gt; for more details.&lt;&#x2F;p&gt;
54+
&lt;p&gt;&lt;strong&gt;Video demo&lt;&#x2F;strong&gt;:&lt;&#x2F;p&gt;
55+
&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;STSZt2c1rSA&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;
56+
&lt;br&gt;
57+
&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;
58+
</content>
59+
</entry>
1060
<entry xml:lang="en">
1161
<title>CLI text processing with GNU grep and ripgrep book announcement</title>
1262
<published>2023-05-11T00:00:00+00:00</published>

sitemap.xml

Lines changed: 5 additions & 1 deletion
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-09</lastmod>
431+
<lastmod>2023-05-16</lastmod>
432432
</url>
433433
<url>
434434
<loc>https://learnbyexample.github.io/tips/cli-tip-1/</loc>
@@ -718,6 +718,10 @@
718718
<loc>https://learnbyexample.github.io/tips/vim-tip-25/</loc>
719719
<lastmod>2023-04-25</lastmod>
720720
</url>
721+
<url>
722+
<loc>https://learnbyexample.github.io/tips/vim-tip-26/</loc>
723+
<lastmod>2023-05-16</lastmod>
724+
</url>
721725
<url>
722726
<loc>https://learnbyexample.github.io/tips/vim-tip-3/</loc>
723727
<lastmod>2022-01-18</lastmod>

tags/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tags/tip/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tags/vim/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tips/index.html

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

tips/vim-tip-25/index.html

Lines changed: 1 addition & 1 deletion
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 25: substitute flags</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-25/>Vim tip 25: substitute flags</a></h1><div class=post__meta><span class=post__time>2023-04-25</span></div></header><div class=post-content><p>Here are some of the flags you can use with the substitute command:<ul><li><code>g</code> replace all occurrences within a matching line <ul><li>by default, only the first matching portion will be replaced</ul><li><code>c</code> ask for confirmation before each replacement<li><code>i</code> ignore case for <em>searchpattern</em><li><code>I</code> don't ignore case for <em>searchpattern</em></ul><p>These flags are applicable for the substitute command but not <code>/</code> or <code>?</code> searches. Flags can also be combined, for example:<ul><li><code>s/cat/Dog/gi</code> replace every occurrence of <code>cat</code> with <code>Dog</code> <ul><li>Case is ignored, so <code>Cat</code>, <code>cAt</code>, <code>CAT</code>, etc are all valid matches<li>Note that <code>i</code> doesn't affect the case of the replacement string</ul></ul><p><img alt=info src=/images/info.svg> See <a href=https://vimhelp.org/change.txt.html#%3As_flags>:h s_flags</a> for a complete list of flags and more details about them.<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/gdcfU8wTMrM 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-24/>Vim tip 24: movement commands within the current file →</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 25: substitute flags</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-25/>Vim tip 25: substitute flags</a></h1><div class=post__meta><span class=post__time>2023-04-25</span></div></header><div class=post-content><p>Here are some of the flags you can use with the substitute command:<ul><li><code>g</code> replace all occurrences within a matching line <ul><li>by default, only the first matching portion will be replaced</ul><li><code>c</code> ask for confirmation before each replacement<li><code>i</code> ignore case for <em>searchpattern</em><li><code>I</code> don't ignore case for <em>searchpattern</em></ul><p>These flags are applicable for the substitute command but not <code>/</code> or <code>?</code> searches. Flags can also be combined, for example:<ul><li><code>s/cat/Dog/gi</code> replace every occurrence of <code>cat</code> with <code>Dog</code> <ul><li>Case is ignored, so <code>Cat</code>, <code>cAt</code>, <code>CAT</code>, etc are all valid matches<li>Note that <code>i</code> doesn't affect the case of the replacement string</ul></ul><p><img alt=info src=/images/info.svg> See <a href=https://vimhelp.org/change.txt.html#%3As_flags>:h s_flags</a> for a complete list of flags and more details about them.<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/gdcfU8wTMrM 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-26/>← Vim tip 26: executing shell commands</a><br><p><a class=next href=https://learnbyexample.github.io/tips/vim-tip-24/>Vim tip 24: movement commands within the current file →</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)