-</code></pre><blockquote><p><img alt=info src=images/info.svg> You can use <code>bind -f ~/.inputrc</code> or press <code>Ctrl+x Ctrl+r</code> to apply changes from the <code>.inputrc</code> file to the current shell session.</blockquote><h3 id=further-reading><a class=header href=#further-reading>Further Reading</a></h3><ul><li><a href=https://mrzool.cc/writing/sensible-bash/>Sensible bash customizations</a><li><a href=https://blog.sanctum.geek.nz/shell-config-subfiles/>Shell config subfiles</a><li><a href=https://unix.stackexchange.com/q/30925/109046>unix.stackexchange: when to use alias, functions and scripts</a><li><a href=https://unix.stackexchange.com/q/3467/109046>unix.stackexchange: what does rc in bashrc stand for</a></ul><h2 id=readline-shortcuts><a class=header href=#readline-shortcuts>Readline shortcuts</a></h2><p>Quoting from <a href=https://www.gnu.org/software/bash/manual/bash.html#Readline-Interaction>bash manual: Readline Interaction</a>:<blockquote><p>Often during an interactive session you type in a long line of text, only to notice that the first word on the line is misspelled. The Readline library gives you a set of commands for manipulating the text as you type it in, allowing you to just fix your typo, and not forcing you to retype the majority of the line. Using these editing commands, you move the cursor to the place that needs correction, and delete or insert the text of the corrections.</blockquote><p>By default, command line editing bindings are styled after <a href=https://www.gnu.org/software/emacs/>Emacs</a> (a text editor). You can switch to Vi mode (another text editor) if you wish. This section will discuss some of the often used Emacs style key bindings.<h3 id=tab-completion><a class=header href=#tab-completion>Tab completion</a></h3><p>The tab key helps you complete commands, aliases, filenames and so on, depending on the context. If there is only one possible completion, it will be done on single tab press. Otherwise, you can press the tab key twice to get a list of possible matches (if there are any).<p>Use <code>set show-all-if-ambiguous on</code> as seen earlier in the <a href=#inputrc>.inputrc</a> section to combine the single and double tab presses into a single action.<blockquote><p><img alt=info src=./images/info.svg> See <a href=https://www.gnu.org/software/bash/manual/bash.html#Programmable-Completion>bash manual: Programmable Completion</a> for more details.</blockquote><h3 id=searching-history><a class=header href=#searching-history>Searching history</a></h3><p>You can use <code>Ctrl+r</code> to search command history. After pressing this key sequence, type characters you wish to match from history, then press the <code>Esc</code> key to return to the command prompt or press <code>Enter</code> to execute the command.<p>You can press <code>Ctrl+r</code> repeatedly to move backwards through matching entries and <code>Ctrl+s</code> to move forwards. If <code>Ctrl+s</code> is not working as expected, see <a href=https://unix.stackexchange.com/q/332791/109046>unix.stackexchange: disable ctrl-s</a>.<p>As discussed in the <a href=#inputrc>.inputrc</a> section, you can use custom key mappings to search based on starting characters of the command.<h3 id=moving-the-cursor><a class=header href=#moving-the-cursor>Moving the cursor</a></h3><p>The documentation uses <strong>Meta</strong> (<code>M-</code> prefix) and notes that this key is labeled as <code>Alt</code> on many keyboards. The documentation also mentions that you can also use the <code>Esc</code> key for such combinations.<ul><li><code>Alt+b</code> move the cursor to the start of the current or previous word<li><code>Alt+f</code> move the cursor to the end of the next word<li><code>Ctrl+a</code> or <code>Home</code> move cursor to the beginning of the command line<li><code>Ctrl+e</code> or <code>End</code> move cursor to the end of the command line</ul><blockquote><p><img alt=info src=./images/info.svg> One difference between <code>Alt</code> and <code>Esc</code> combinations is that you can keep pressing <code>b</code> or <code>f</code> while holding the <code>Alt</code> key down. The <code>Esc</code> combinations are two different key presses, whereas <code>Alt</code> has to be kept pressed down for the shortcut to take effect.</blockquote><h3 id=deleting-characters><a class=header href=#deleting-characters>Deleting characters</a></h3><ul><li><code>Alt+Backspace</code> (or <code>Esc+Backspace</code>) delete backwards up to word boundary<li><code>Ctrl+w</code> delete backwards up to whitespace boundary<li><code>Ctrl+u</code> delete from the character before the cursor till the start of the line<li><code>Ctrl+k</code> delete from the cursor location to the end of the command line</ul><h3 id=clear-screen><a class=header href=#clear-screen>Clear screen</a></h3><ul><li><code>Ctrl+l</code> preserve whatever is typed and clear the terminal screen</ul><blockquote><p><img alt=info src=./images/info.svg> Note that <code>Ctrl+l</code> doesn't try to remove the scrollback buffer altogether. Use the <code>clear</code> command for that purpose.</blockquote><h3 id=swap-words-and-characters><a class=header href=#swap-words-and-characters>Swap words and characters</a></h3><ul><li><code>Alt+t</code> (or <code>Esc+t</code>) swap the previous two words<li><code>Ctrl+t</code> swap the previous two characters <ul><li>for example, if you typed <code>sp</code> instead of <code>ps</code>, press <code>Ctrl+t</code> when the cursor is to the right of <code>sp</code></ul></ul><h3 id=insert-arguments><a class=header href=#insert-arguments>Insert arguments</a></h3><ul><li><code>Alt+.</code> (or <code>Esc+.</code>) insert the last argument from the previous command, multiple presses will traverse through second last command and so on <ul><li>for example, if <code>cat temp.txt</code> was the last command used, pressing <code>Alt+.</code> will insert <code>temp.txt</code><li>you can also use <code>!$</code> to represent the last argument from the previous command</ul></ul><h3 id=further-reading-1><a class=header href=#further-reading-1>Further Reading</a></h3><ul><li><a href=https://www.gnu.org/software/bash/manual/bash.html#Bindable-Readline-Commands>bash manual: Bindable Readline Commands</a><li><a href=https://wiki.archlinux.org/title/readline>wiki.archlinux: Simpler introduction to Readline</a><li><a href=https://cupfullofcode.com/blog/2013/07/03/efficient-command-line-navigation/index.html>Efficient command line navigation</a></ul><h2 id=copy-and-paste><a class=header href=#copy-and-paste>Copy and paste</a></h2><p>Shortcuts for copy-paste operations in the terminal are shown below. You might be able to customize these shortcuts in terminal preferences.<ul><li><code>Shift+Ctrl+c</code> copy the highlighted portion to the clipboard<li><code>Shift+Ctrl+v</code> paste clipboard contents<li><code>Shift+Insert</code> paste the last highlighted portion (not necessarily the clipboard contents)</ul><p>You can also press middle mouse button instead of the <code>Shift+Insert</code> shortcut. This is not limited to the terminal, but works in any application. Use the <code>xinput</code> command to enable/disable mouse button clicks. First, use <code>xinput</code> without any arguments and spot the number corresponding to your mouse. As an example, assuming the device number is <code>11</code>, you can use the following commands:<ul><li><code>xinput set-button-map 11 1 0 3</code> to disable middle button click<li><code>xinput set-button-map 11 1 2 3</code> to enable middle button click</ul><h2 id=exercises><a class=header href=#exercises>Exercises</a></h2><p><strong>1)</strong> Which command would you use to display the name and value of all or specific environment variables?<p><strong>2)</strong> If you add an alias for an already existing command (<code>ls</code> for example), how would you invoke the original command instead of the alias?<p><strong>3)</strong> Why doesn't the alias shown below work? What would you use instead?<pre><code class=language-bash># doesn't work as expected
0 commit comments