﻿{"id":215,"date":"2014-02-01T18:06:37","date_gmt":"2014-02-01T17:06:37","guid":{"rendered":"http:\/\/blog.pjsen.eu\/?p=215"},"modified":"2014-02-06T21:58:52","modified_gmt":"2014-02-06T20:58:52","slug":"one-thing-cmd-exe-is-better-at-than-nix-shells","status":"publish","type":"post","link":"https:\/\/blog.pjsen.eu\/?p=215","title":{"rendered":"One thing cmd.exe is better at than *nix shells (with default configuration)"},"content":{"rendered":"<p>I know the statement might be considered controversial. I even encourage you to try to prove me wrong, because I wish I knew better solution.<\/p>\n<p>In my every day work I tend to use command prompt a lot. I have both <code>cmd.exe<\/code> and <code>bash<\/code> (from Git for Windows) opened all the time. My typical environment comprises numerous directories, I mean more than one hundred. Many of them share parts of their names. The names are long, dozens of characters. I have to move between them over and over again. The problem is that it is not feasible to type longish directory name many times manually.<\/p>\n<p>Now, let&#8217;s suppose we have the following (shortened, of course) subdirectories structure of a directory which we are in at the moment:<\/p>\n<pre>..\r\naa\r\nbbaa\r\nccaadd\r\neeaaff<\/pre>\n<p>When I would like to change the directory to <code>bbaa<\/code> in cmd.exe I type <code>cd *aa* &lt;Tab&gt; &lt;Tab&gt;<\/code>, I get the second result of auto completion, I press <code>&lt;Enter&gt;<\/code> and I have moved to <code>bbaa<\/code>. If I press <code>&lt;tab&gt;<\/code> three times, I get <code>ccaadd<\/code>. And when I press <code>&lt;tab&gt;<\/code> four times, I get <code>eeaaff<\/code>. This feature is brilliant. The auto completion works with wildcards and matches <strong>not<\/strong> only beginnings of a name. Last, but not least, it <strong>allows to cycle through suggestions while in-line editing a command<\/strong>.<\/p>\n<p>The most important part here is: <strong>not only beginning of a name (which is, as far as I know, the behavior of a typical Unix shell) AND also ability to have the suggestions inserted in place, not only displayed them below the command prompt<\/strong>.<\/p>\n<p>A Unix shell also does match wildcards, but only displays matched names. It does not offer (or I am not aware of it) a way to instantaneously pass matched name to a command. It only lists relevant suggestions and a user have to then manually re-edit the command so that it has desired argument. <code>cmd.exe<\/code> is better in that it allows a user to cycle through suggestions while in-line editing command argument. Which is great when it comes to long names of which only some parts can be conveniently memorized by a human.<\/p>\n<p>I propose the following function which could be appended to <code>.bashrc<\/code>.<\/p>\n<pre class=\"brush:shell\">function cdg() { ls -d *\/ | grep -i \"$1\" | awk \"{printf(\\\"%d : %s\\n\\\", NR, \\$0)}\"; read choice; if [ \"$choice\" == \"0\" ]; then : ; else cd \"`ls -d *\/ | grep -i \\\"$1\\\" | awk \\\"NR==$choice\\\"`\"; fi; }<\/pre>\n<p>It is a simplistic function that searches through <code>ls<\/code> results with <code>grep<\/code>, parses them with <code>awk<\/code> and finally picks one of them and calls <code>cd<\/code>.<\/p>\n<p>Now we can type <code>cdg aa<\/code> and we get all possible choices:<\/p>\n<pre>1 : aa\/\r\n2 : bbaa\/\r\n3 : ccaadd\/\r\n4 : eeaaff\/<\/pre>\n<p>We simply type the number and we are done being moved to the desired directory. Without the need to manually re-enter the <code>cd<\/code> command with proper argument. Obviously, in <code>cmd.exe<\/code> we get this nice auto completion for every command typed in the interpreter, and my solution only solves changing directory use case in <code>bash<\/code>.<\/p>\n<p>2014.02.02 UPDATE 1: After some deeper research, it turned out that the behavior of <code>cmd.exe<\/code> can be achieved in <code>bash<\/code> as well. The following line should be included into <code>.bashrc<\/code>:<\/p>\n<pre class=\"brush:shell\">bind '\"\\t\":menu-complete'<\/pre>\n<p>However, my solution still serves the purpose as it uses <code>grep -i<\/code> which makes it case insensitive and thus renders it useful.<\/p>\n<p>2014.02.06 UPDATE 2: I have experienced the second reason my solution is still relevant. It is much faster than pressing <code>&lt;tab&gt;<\/code> and waiting for the shell to suggest names. This can be observed in an environment with number of directories greater than a few, where MinGW tooling tends to be slow in general. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>I know the statement might be considered controversial. I even encourage you to try to prove me wrong, because I wish I knew better solution. In my every day work I tend to use command prompt a lot. I have both cmd.exe and bash (from Git for Windows) opened all the time. My typical environment<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,7,6],"tags":[],"class_list":["post-215","post","type-post","status-publish","format-standard","hentry","category-general-programming","category-quick-tip","category-solutions"],"_links":{"self":[{"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/posts\/215","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=215"}],"version-history":[{"count":0,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=\/wp\/v2\/posts\/215\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=215"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=215"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.pjsen.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=215"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}