|
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-05-12T00:00:00+00:00</updated> |
| 8 | + <updated>2023-05-16T00:00:00+00:00</updated> |
9 | 9 | <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"><p>You can execute external commands from within Vim. Here are some examples:</p> |
| 17 | +<ul> |
| 18 | +<li><kbd>:!ls</kbd> execute the given shell command and display output |
| 19 | +<ul> |
| 20 | +<li>the results are displayed as part of an expanded Command-line area, doesn't change contents of the file</li> |
| 21 | +</ul> |
| 22 | +</li> |
| 23 | +<li><kbd>:.!date</kbd> replace the current line with the output of the given command |
| 24 | +<ul> |
| 25 | +<li>pressing <kbd>!!</kbd> in Normal mode will also result in <kbd>:.!</kbd></li> |
| 26 | +<li><code>!</code> waits for motion similar to <code>d</code> and <code>y</code> commands, <kbd>!G</kbd> will give <kbd>:.,$!</kbd></li> |
| 27 | +</ul> |
| 28 | +</li> |
| 29 | +<li><kbd>:%!sort</kbd> sort all the lines |
| 30 | +<ul> |
| 31 | +<li>recall that <code>%</code> is a shortcut for the range <code>1,$</code></li> |
| 32 | +<li>note that this executes an external command, not the built-in <code>:sort</code> command</li> |
| 33 | +</ul> |
| 34 | +</li> |
| 35 | +<li><kbd>:3,8!sort</kbd> sort only lines <code>3</code> to <code>8</code></li> |
| 36 | +<li><kbd>:r!date</kbd> insert output of the given command below the current line</li> |
| 37 | +<li><kbd>:r report.log</kbd> insert contents of the given file below the current line |
| 38 | +<ul> |
| 39 | +<li>Note that <code>!</code> is not used here since there is no shell command</li> |
| 40 | +</ul> |
| 41 | +</li> |
| 42 | +<li><kbd>:.!grep '^Help ' %</kbd> replace the current line with all the lines starting with <code>Help</code> in the current file |
| 43 | +<ul> |
| 44 | +<li><code>%</code> here refers to current file contents</li> |
| 45 | +</ul> |
| 46 | +</li> |
| 47 | +<li><kbd>:sh</kbd> open a shell session within Vim |
| 48 | +<ul> |
| 49 | +<li>use <code>exit</code> command to quit the session</li> |
| 50 | +</ul> |
| 51 | +</li> |
| 52 | +</ul> |
| 53 | +<p><img src="/images/info.svg" alt="info" /> 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> |
| 54 | +<p><strong>Video demo</strong>:</p> |
| 55 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/STSZt2c1rSA" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 56 | +<br> |
| 57 | +<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> |
| 58 | +</content> |
| 59 | + </entry> |
10 | 60 | <entry xml:lang="en">
|
11 | 61 | <title>CLI text processing with GNU grep and ripgrep book announcement</title>
|
12 | 62 | <published>2023-05-11T00:00:00+00:00</published>
|
|
0 commit comments