{"id":331,"date":"2011-01-11T11:10:25","date_gmt":"2011-01-11T10:10:25","guid":{"rendered":"https:\/\/thebc.ch\/blog\/?p=331"},"modified":"2013-02-01T13:01:56","modified_gmt":"2013-02-01T12:01:56","slug":"bash-magic","status":"publish","type":"post","link":"https:\/\/thebc.ch\/blog\/?p=331","title":{"rendered":"Bash Magic"},"content":{"rendered":"<pre>Ctrl + A \tGo to the beginning of the line you are currently typing on\r\nCtrl + E \tGo to the end of the line you are currently typing on\r\nCtrl + L \tClears the Screen, similar to the clear command\r\nCtrl + U \tClears the line before the cursor position. If you are at the end of the line, clears the entire line.\r\nCtrl + H \tSame as backspace\r\nCtrl + R \tLet's you search through previously used commands\r\nCtrl + C \tKill whatever you are running\r\nCtrl + D \tExit the current shell\r\nCtrl + Z \tPuts whatever you are running into a suspended background process. fg restores it.\r\nCtrl + W \tDelete the word before the cursor\r\nCtrl + K \tClear the line after the cursor\r\nCtrl + T \tSwap the last two characters before the cursor\r\nEsc + T \tSwap the last two words before the cursor\r\nAlt + F \tMove cursor forward one word on the current line\r\nAlt + B \tMove cursor backward one word on the current line\r\nTab \t        Auto-complete files and folder names<\/pre>\n<p>&nbsp;<\/p>\n<table id=\"toc\" summary=\"Contents\">\n<tbody>\n<tr>\n<td>\n<div id=\"toctitle\">\n<span id=\"Contents\"><h2>Contents<\/h2><\/span>\n<\/div>\n<ul>\n<li>1 !! (Full contents of previous command)<\/li>\n<li>2 !$ (Last arg of previous command)<\/li>\n<li>3 Sets<\/li>\n<li>4 OSX specific<\/li>\n<li>5 References<\/li>\n<\/ul>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><a id=\".21.21_.28Full_contents_of_previous_command.29\" name=\".21.21_.28Full_contents_of_previous_command.29\"><\/a><\/p>\n<span id=\"Full_contents_of_previous_command\"><h2>!! (Full contents of previous command)<\/h2><\/span>\n<p>How many times have you tried to edit a file, then realize that you need to be root to do it? Use bang-bang to quickly repeat the previous command, with other commands before or after.<\/p>\n<pre>vi \/etc\/init.d\/mongrel_cluster\r\n=&gt; Permission Denied\r\nsudo\u00c2\u00a0!!\r\n=&gt; Now opens the file as root<\/pre>\n<p><a id=\".21.24_.28Last_arg_of_previous_command.29\" name=\".21.24_.28Last_arg_of_previous_command.29\"><\/a><\/p>\n<span id=\"Last_arg_of_previous_command\"><h2>!$ (Last arg of previous command)<\/h2><\/span>\n<p>Sometimes I need to reuse the last argument with another command, like here where I forgot to quote a string.<\/p>\n<pre>wget <a title=\"http:\/\/weather.yahooapis.com\/forecastrss?p=98117\" href=\"http:\/\/weather.yahooapis.com\/forecastrss?p=98117\" rel=\"nofollow\">http:\/\/weather.yahooapis.com\/forecastrss?p=98117<\/a>\r\n=&gt; wget: No Match\r\nwget '!$'\r\n=&gt; Now it works<\/pre>\n<p><a id=\"Sets\" name=\"Sets\"><\/a><\/p>\n<span id=\"Sets\"><h2>Sets<\/h2><\/span>\n<p><strong>{a,b} (A set)<\/strong><\/p>\n<p>How often do you rename just part of a file? The {} syntax is convenient.<\/p>\n<pre>mv file.{txt,xml}\r\n=&gt; Expands to 'mv file.txt file.xml'<\/pre>\n<pre>mv file{,.orig}\r\n=&gt; Expands to 'mv file file.orig'<\/pre>\n<pre>mkdir foo{1,2,3}\r\n=&gt; Expands to 'mkdir foo1 foo2 foo3'<\/pre>\n<p><a id=\"OSX_specific\" name=\"OSX_specific\"><\/a><\/p>\n<span id=\"OSX_specific\"><h2>OSX specific<\/h2><\/span>\n<p><strong>pbcopy and pbpaste<\/strong><\/p>\n<p>In Mac OS X, you can copy things to the clipboard and read them back out. This is nice because you can reuse it in the shell or back in the OS with Apple-C or Apple-V.<\/p>\n<pre>.\/generate_random_password | pbcopy\r\npbpaste &gt; file.txt<\/pre>\n<p><a id=\"References\" name=\"References\"><\/a><\/p>\n<span id=\"References\"><h2>References<\/h2><\/span>\n<p><a title=\"http:\/\/nubyonrails.com\/articles\/useful-shell-shortcuts\" href=\"http:\/\/nubyonrails.com\/articles\/useful-shell-shortcuts\" rel=\"nofollow\">http:\/\/nubyonrails.com\/articles\/useful-shell-shortcuts<\/a><\/p>\n<p><a title=\"http:\/\/www.howtogeek.com\/howto\/ubuntu\/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc\/\" href=\"http:\/\/www.howtogeek.com\/howto\/ubuntu\/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc\/\" rel=\"nofollow\">http:\/\/www.howtogeek.com\/howto\/ubuntu\/keyboard-shortcuts-for-bash-command-shell-for-ubuntu-debian-suse-redhat-linux-etc\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ctrl + A Go to the beginning of the line you are currently typing on Ctrl + E Go to the end of the line you are currently typing on Ctrl + L Clears the Screen, similar to the clear &hellip; <a href=\"https:\/\/thebc.ch\/blog\/?p=331\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,5,19,16],"tags":[],"class_list":["post-331","post","type-post","status-publish","format-standard","hentry","category-bash","category-linux","category-mac-os-x","category-software"],"_links":{"self":[{"href":"https:\/\/thebc.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/331","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/thebc.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/thebc.ch\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/thebc.ch\/blog\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/thebc.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=331"}],"version-history":[{"count":8,"href":"https:\/\/thebc.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions"}],"predecessor-version":[{"id":2706,"href":"https:\/\/thebc.ch\/blog\/index.php?rest_route=\/wp\/v2\/posts\/331\/revisions\/2706"}],"wp:attachment":[{"href":"https:\/\/thebc.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=331"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/thebc.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=331"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/thebc.ch\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=331"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}