|
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-02-07T00:00:00+00:00</updated> |
| 8 | + <updated>2023-02-10T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id>
|
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>CLI tip 23: recursive filename matching with globstar</title> |
| 12 | + <published>2023-02-10T00:00:00+00:00</published> |
| 13 | + <updated>2023-02-10T00:00:00+00:00</updated> |
| 14 | + <link rel="alternate" href="https://learnbyexample.github.io/tips/cli-tip-23/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/cli-tip-23/</id> |
| 16 | + <content type="html"><p>Enable the <code>globstar</code> option to recursively match filenames within a specified path. You can use <code>shopt -s globstar</code> and <code>shopt -u globstar</code> to set and unset this option respectively.</p> |
| 17 | +<p>First, create some sample files:</p> |
| 18 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ mkdir test_globstar </span><span style="color:#72ab00;">&amp;&amp;</span><span> cd </span><span style="color:#5597d6;">$_ |
| 19 | +</span><span>$ mkdir </span><span style="color:#72ab00;">-</span><span style="color:#b39f04;">p</span><span> todos projects/{tictactoe,calculator} |
| 20 | +</span><span>$ touch ip.txt .hidden.txt report.log hello.py |
| 21 | +</span><span>$ touch todos/{books,outing}.txt |
| 22 | +</span><span>$ touch projects</span><span style="color:#72ab00;">/</span><span>tictactoe</span><span style="color:#72ab00;">/</span><span>game.py projects</span><span style="color:#72ab00;">/</span><span>calculator/{calc.sh,notes.txt} |
| 23 | +</span></code></pre> |
| 24 | +<p>Here are some examples:</p> |
| 25 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ shopt </span><span style="color:#72ab00;">-</span><span>s globstar |
| 26 | +</span><span> |
| 27 | +</span><span>$ ls </span><span style="color:#72ab00;">-</span><span style="color:#b3933a;">1 </span><span style="color:#72ab00;">**</span><span>/</span><span style="color:#72ab00;">*</span><span>.txt |
| 28 | +</span><span>ip.txt |
| 29 | +</span><span>projects</span><span style="color:#72ab00;">/</span><span>calculator</span><span style="color:#72ab00;">/</span><span>notes.txt |
| 30 | +</span><span>todos</span><span style="color:#72ab00;">/</span><span>books.txt |
| 31 | +</span><span>todos</span><span style="color:#72ab00;">/</span><span>outing.txt |
| 32 | +</span><span> |
| 33 | +</span><span>$ ls </span><span style="color:#72ab00;">-</span><span style="color:#b3933a;">1 </span><span style="color:#72ab00;">**</span><span style="color:#c49a39;">/*/</span><span style="color:#72ab00;">*</span><span>.txt |
| 34 | +</span><span>projects</span><span style="color:#72ab00;">/</span><span>calculator</span><span style="color:#72ab00;">/</span><span>notes.txt |
| 35 | +</span><span>todos</span><span style="color:#72ab00;">/</span><span>books.txt |
| 36 | +</span><span>todos</span><span style="color:#72ab00;">/</span><span>outing.txt |
| 37 | +</span><span> |
| 38 | +</span><span style="color:#7f8989;"># assumes extglob is enabled |
| 39 | +</span><span>$ ls </span><span style="color:#72ab00;">-</span><span style="color:#b3933a;">1 </span><span style="color:#72ab00;">**</span><span>/</span><span style="color:#72ab00;">*</span><span>.@(py</span><span style="color:#72ab00;">|</span><span>sh) |
| 40 | +</span><span>hello.py |
| 41 | +</span><span>projects</span><span style="color:#72ab00;">/</span><span>calculator</span><span style="color:#72ab00;">/</span><span>calc.sh |
| 42 | +</span><span>projects</span><span style="color:#72ab00;">/</span><span>tictactoe</span><span style="color:#72ab00;">/</span><span>game.py |
| 43 | +</span><span> |
| 44 | +</span><span>$ ls </span><span style="color:#72ab00;">-</span><span>1d </span><span style="color:#72ab00;">**/ |
| 45 | +</span><span>projects</span><span style="color:#72ab00;">/ |
| 46 | +</span><span>projects</span><span style="color:#72ab00;">/</span><span>calculator</span><span style="color:#72ab00;">/ |
| 47 | +</span><span>projects</span><span style="color:#72ab00;">/</span><span>tictactoe</span><span style="color:#72ab00;">/ |
| 48 | +</span><span>todos</span><span style="color:#72ab00;">/ |
| 49 | +</span></code></pre> |
| 50 | +<blockquote> |
| 51 | +<p><img src="/images/info.svg" alt="info" /> If you need to match hidden files as well, enable the <code>dotglob</code> option:</p> |
| 52 | +<pre data-lang="ruby" style="background-color:#f5f5f5;color:#1f1f1f;" class="language-ruby "><code class="language-ruby" data-lang="ruby"><span>$ shopt </span><span style="color:#72ab00;">-</span><span>s dotglob |
| 53 | +</span><span>$ ls </span><span style="color:#72ab00;">-</span><span style="color:#b3933a;">1 </span><span style="color:#72ab00;">**</span><span>/</span><span style="color:#72ab00;">*</span><span>.txt |
| 54 | +</span><span>.hidden.txt |
| 55 | +</span><span>ip.txt |
| 56 | +</span><span>projects</span><span style="color:#72ab00;">/</span><span>calculator</span><span style="color:#72ab00;">/</span><span>notes.txt |
| 57 | +</span><span>todos</span><span style="color:#72ab00;">/</span><span>books.txt |
| 58 | +</span><span>todos</span><span style="color:#72ab00;">/</span><span>outing.txt |
| 59 | +</span></code></pre> |
| 60 | +</blockquote> |
| 61 | +<p><strong>Video demo</strong>:</p> |
| 62 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/wo5Szdi5VLA" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 63 | +<br> |
| 64 | +<p><img src="/images/info.svg" alt="info" /> See also my <a href="https://github.com/learnbyexample/cli-computing">Computing from the Command Line</a> ebook.</p> |
| 65 | +</content> |
| 66 | + </entry> |
| 67 | + <entry xml:lang="en"> |
| 68 | + <title>Vim tip 22: word and WORD motions</title> |
| 69 | + <published>2023-02-10T00:00:00+00:00</published> |
| 70 | + <updated>2023-02-10T00:00:00+00:00</updated> |
| 71 | + <link rel="alternate" href="https://learnbyexample.github.io/tips/vim-tip-22/" type="text/html"/> |
| 72 | + <id>https://learnbyexample.github.io/tips/vim-tip-22/</id> |
| 73 | + <content type="html"><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>.</p> |
| 74 | +<blockquote> |
| 75 | +<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>&lt;EOL&gt;</code>). This can be changed with the <code>iskeyword</code> option. An empty line is also considered to be a word.</p> |
| 76 | +</blockquote> |
| 77 | +<blockquote> |
| 78 | +<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.</p> |
| 79 | +</blockquote> |
| 80 | +<p>word based motions:</p> |
| 81 | +<ul> |
| 82 | +<li><kbd>w</kbd> move to the start of the next word</li> |
| 83 | +<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> |
| 84 | +<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> |
| 85 | +<li><kbd>ge</kbd> move to the end of the previous word</li> |
| 86 | +<li><kbd>3w</kbd> move 3 words forward |
| 87 | +<ul> |
| 88 | +<li>Similarly, a number can be prefixed for all the other commands discussed here</li> |
| 89 | +</ul> |
| 90 | +</li> |
| 91 | +</ul> |
| 92 | +<p>WORD based motions:</p> |
| 93 | +<ul> |
| 94 | +<li><kbd>W</kbd> move to the start of the next WORD |
| 95 | +<ul> |
| 96 | +<li><code>192.1.168.43;hello</code> is considered as a single WORD, but has multiple words</li> |
| 97 | +</ul> |
| 98 | +</li> |
| 99 | +<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> |
| 100 | +<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> |
| 101 | +<li><kbd>gE</kbd> move to the end of the previous WORD</li> |
| 102 | +</ul> |
| 103 | +<p><img src="/images/info.svg" alt="info" /> 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> |
| 104 | +<p><strong>Video demo</strong>:</p> |
| 105 | +<p align="center"><iframe width="560" height="315" loading="lazy" src="https://www.youtube.com/embed/-1MKL82cbw8" title="YouTube video player" frameborder="0" allow="accelerometer; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></p> |
| 106 | +<br> |
| 107 | +<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> |
| 108 | +</content> |
| 109 | + </entry> |
10 | 110 | <entry xml:lang="en">
|
11 | 111 | <title>Python tip 23: map, filter and reduce</title>
|
12 | 112 | <published>2023-02-07T00:00:00+00:00</published>
|
|
0 commit comments