{"id":642,"date":"2012-07-10T16:47:51","date_gmt":"2012-07-10T08:47:51","guid":{"rendered":"http:\/\/rmohan.com\/?p=642"},"modified":"2012-07-10T16:47:51","modified_gmt":"2012-07-10T08:47:51","slug":"bash-shortcuts-and-tips","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=642","title":{"rendered":"Bash Shortcuts and Tips"},"content":{"rendered":"<h3><strong>Bash Shortcuts and Tips<\/strong><\/h3>\n<p><strong>Repeating an argument<br \/>\n<\/strong>You can repeat the last argument of the previous command in multiple ways. Have a look at this example:<\/p>\n<p>$ mkdir \/path\/to\/dir<br \/>\n$ cd !$<\/p>\n<p>The second command might look a little strange, but it will just cd to \/path\/to\/dir.<\/p>\n<p><strong>Some keyboard shortcuts for editing<\/strong><\/p>\n<p>There are some pretty useful keyboard shortcuts for editing in bash. They might appear familiar to Emacs users:<\/p>\n<p>\u2022 Ctrl + a =&gt; Return to the start of the command you&#8217;re typing<br \/>\n\u2022 Ctrl + e =&gt; Go to the end of the command you&#8217;re typing<br \/>\n\u2022 Ctrl + u =&gt; Cut everything before the cursor to a special clipboard<br \/>\n\u2022 Ctrl + k =&gt; Cut everything after the cursor to a special clipboard<br \/>\n\u2022 Ctrl + y =&gt; Paste from the special clipboard that Ctrl + u and Ctrl + k save their data to<br \/>\n\u2022 Ctrl + t =&gt; Swap the two characters before the cursor (you can actually use this to transport a character from the left to the right, try it!)<br \/>\n\u2022 Ctrl + w =&gt; Delete the word \/ argument left of the cursor<br \/>\n\u2022 Ctrl + l =&gt; Clear the screen<\/p>\n<p><strong>Redirecting both Standard Output and Standard Error:<br \/>\n<\/strong><br \/>\n# ls -ltR 2&gt;&amp;1 &gt; \/tmp\/temp.txt<\/p>\n<p><em><strong>Specify this in .bashrc<\/strong><br \/>\n<\/em><br \/>\nMake Bash append rather than overwrite the history on disk:<\/p>\n<p># shopt -s histappend<\/p>\n<p>Whenever displaying the prompt, write the previous line to disk:<\/p>\n<p># export PROMPT_COMMAND=\u2019history -a\u2019<\/p>\n<p>To erase duplicate entries in History.<\/p>\n<p># export HISTCONTROL=erasedups<br \/>\n(or)<br \/>\n# export HISTCONTROL=ignoreboth<\/p>\n<p>To see the history with timestamps<\/p>\n<p># export HISTTIMEFORMAT=&#8221;%d\/%m\/%Y-%H:%M:%S &#8221;<\/p>\n<p>To set the Size of the historyHISTSIZE: The number of commands to remember in the command history. The default value is 500.<\/p>\n<p># export HISTSIZE=500<\/p>\n<p><strong>Searching the Past<\/strong><br \/>\n<strong><\/strong><strong><\/strong><\/p>\n<ul>\n<li><strong><\/strong>Ctrl+R searches previous lines<\/li>\n<\/ul>\n<p>This will put bash in history mode, allowing you to type a part of the command you&#8217;re looking for. In the meanwhile, it will show the most recent occasion where the string you&#8217;re typing was used. If it is showing you a too recent command, you can go further back in history by pressing Ctrl + r again and again. Once you found the command you were looking for, press enter to run it.<\/p>\n<ul>\n<li>!tcp will execute the previous command which starts with &#8220;tcp&#8221;<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Bash Shortcuts and Tips <\/p>\n<p>Repeating an argument You can repeat the last argument of the previous command in multiple ways. Have a look at this example:<\/p>\n<p>$ mkdir \/path\/to\/dir $ cd !$<\/p>\n<p>The second command might look a little strange, but it will just cd to \/path\/to\/dir.<\/p>\n<p>Some keyboard shortcuts for editing<\/p>\n<p>There are some [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/642"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=642"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/642\/revisions"}],"predecessor-version":[{"id":643,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/642\/revisions\/643"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}