NYLXS - Do'ers
Mon Apr 12 09:15:22 2021 e.s.t.





New York Linux Scene Journal
How to avoid carpal tunnel syndrome (or what has the command line done for you lately?)

By Marco Scoffier

First, this is not an introduction to basic unix command line commands. (Feeling jipped? --sorry) There are many such introductions online (Unix is a Four letter word which is quite good), but quite frankly, knowing how to copy a file from one directory to another using cp rather than drag-n-drop isn't ever going to get you to prefer one method over the other.

In this article, I have tried to compile (no this isn't' about C either) a collection of shortcuts which should accelerate your daily usage of the command line. Why? because unless something is a pleasure to use, and better than other alternatives... you're never going to use it!

Unless you have a little bag of shortcuts and tricks, you will never be happy using the command line and you will never understand how someone could prefer it or even love it. (If you are already convinced of the power of the command line skip down here . If you are skeptical, read on).

Now, I was a long time mac user and administrator before converting to unix. You might think that this would make me drag-n-drop happy, but as anyone who has used macs in any professional capacity knows, you memorize every key-combo that exists to reduce your mouse-mileage and accelerate your work. (Speaking of mouse-mileage on the Gnome desktop there is an Applet under Amusements-> Odometer? It does just that...)

Now knowing the key combos on Mac power applications can take you so far, but you have not seen anything like the command line. The unix shells (the programs which run the command line) represent probably the longest running programming effort yet, built into these shells are years of short cuts developed by the world's best programmers. (Now I did say shells, yes there are different ones and they do treat some things differently. In this article I'm basically talking about bash. It is one of the more full featured shells, and usually the default for Linux. if you're running linux, and you haven't tinkered too much with your system, there's a huge chance you're running bash.)

Now if the command line is so powerful and so great, why aren't more people using it? Well, there are many answers to this question, many very long answers running the gamut from questioning the media empires and how we get our information, to our education system, but to make this a bit shorter, I want to focus on one issue I find less discussed than the rest: the difference between computer people and other people.

The command line was written by computer scientists for computer scientists right? Often these "techies" are made out to be another breed all together, but are they really a different type of person than the general population? I think not, but bear with me.

In general, it is safe to say that computer scientists are a bottom->up bunch. By bottom->up I mean these are the types of people who get excited by the idea of speaking machine-code (or klingon), and to the outsider, don't seem to get too far from obscure ruinations about bits, and clock cycles, and big-O's. Top->down people like writers or even top->float_around_in_the_nanoshpere people like artists, get exasperated seeing these guys who are hopelessly confusing, and can't get past tiny little joys that only they can understand anyway.

I think it is safe to say that top->down people get exasperated when they are faced with a bottom->up person's interface. And bottom->up people can't understand why a top->down person who can't understand the first step of some process such as allocating memory, wants to know the big picture, such as why does my mac keep crashing. What we have here is a profound failure to communicate.

What neither side realizes is that they are actually much closer than they imagine. Why do top downers use the mouse? Because they are lazy. And the command line seems difficult to learn. Why do the bottom uppers use the command line? Because they are lazy. They know about the mouse, but know that the command line is the lazy man's dream come true. Perl people will try to convince you that they use the language with the most laziness built in, but laziness has a long history in computer science, there is a story about how Richard Stallman started the whole Free Software movement because HP refused to give him access to the source code for a printer driver update, which meant that he would be forced to walk down the hall to check if his printouts had come out of the printer, rather than query the printer form his desk... but that is another story, and you want to know how you can get lazy with your command line now. So here goes.

Laziness No.1: I'm sick of typing the same stuff all the time. And typos... I hate typos....
Solution No.1 : [TAB] (also known as tab completion)

Ahhh tab completion.... the single greatest input accelerator invented in human computer interaction. You're going to get so in the habit of using tab completion, you'll want to tab-complete everything (some of you already know what I'm talkn' about right?) Example: how does it work?Let me start with something which may already be familiar to you. Many browsers will try to complete a URL for you when you start to type in an new address. For instance you type in www.goo... the browser will fill in www.google.com because you visited the site yesterday. The browser will not however compare what you are typing to every web-site that exists. The command line does. It completes what you are typing with every command in your path, or every file in your system which could possibly match what you have already typed. Examples:

You want to open the web-page you have been writing but don't remember exactly what you named it. You type: vi [TAB]this will list all the files which vi could open, and any subdirectories of the directory you are in. You remember you named the directory unix_notes, so you continue typing : u+[TAB] will print out vi unix_notes/ if there are no files or directories starting with u in your working directory. If there are other files which match [TAB][TAB] will print the list of possible completions. So if I have the directories unix and unicode I just have to type unix_[tab] to get unix_notes/ and then k[TAB] to get key_combos.html as that is the only file I have starting with a k in my unix_notes directory. You just have to type out as many characters as will uniquely identify the file. (I've been known to rename files and directories so that I can get to them using only one character + [TAB] combos. For example: vi /v[TAB]w[TAB]h[TAB]i[TAB]p[TAB]s[TAB]b[TAB] Will bring open the file /var/www/html/info/programming/shell/bash.html for editing in vi. Trust me the tab-key makes this much faster. You'll be [TAB][TAB][TAB]ing in no timeNow

Expert tip:If you have started tab-completing everything you type you might be happy to know that there is a mode for tab-completion in emacs, but even easier in that while writing code and in vim you just type CTRL-P! this will compete with any word already in the file you are typing and even search through include files if you are writing code!

Laziness No.2: I just typed that, or I hate typing the same stuff over and over
Solution No.2 : [ESC]^. (escape+period) or [ALT]^. (alt+period)

Depending on your setup the [ESC] key could either be one marked esc in the upper-left most corner or mapped to the alt key. (On many modern setups both will work). [ESC]+. reprints the last argument of the last command you typed. This is really handy.

Example: how does it work? You are moving many files from one directory to your web server directory. mv *.html /var/www/html/programming/shell/ mv *.gif /var/www/html/programming/shell/ mv *.jpg /var/www/html/programming/shell/

NOTE: if you don't know what the star does you really should learn about globbing. Globbing is a huge accelerator, and is also quite well documented (one good site is here. I chose in this article to focus on some handy lesser know key combos.

Using the one character + [TAB] we just talked about this would be quite fast, but you still have to type /v[TAB]/w[TAB]/h[TAB]/p[TAB]/s[TAB]/ over and over to get /var/www/html/programming/shell/. When human beings do things over and over, they make mistakes. Machines however are wonderfull at doing things over and over so get that machine to work for you. Using the same example as above you type the first line: mv *.html /var/www/html/programming/shell/Using [TAB]'s of course. Then for the next line you type: mv *.gif [ESC] .That is either the escape-key (upper right) or the alt key (next to the space bar on your key board and a period. This will get the last argument from the line you just typed hence in this case /var/www/html/programming/shell/. Fast hunh...

Laziness No.3: I just typed that, or I hate typing the same stuff over and over (recurring laziness)
Solution No.3: History the [up-arrow] and [CTRL]^R

I'm going to go over this first part quickly, because most of you may know about the [up-arrow] already. To get the last command you typed you hit the [up-arrow], and to go back to previous commands you hit the up arrow again and again. Sometimes if you are trying to get back that complicated command you typed yesterday or last week, this could mean a lot of [up-arrow]'s. Wouldn't it be great if you could search your history? Enter [CTRL]^r. The little hat means hold down the [CTRL] key and hit "r" otherwise you will just type an r. Now when you have done this your prompt (the part of the command line before what you type) should change to this: (reverse-i-search)`': Now type the first couple of letters of the text you want to match. For example the other day I was trying to use a command line program called transcode which takes many arguments and I don't remember what exact list of arguments I used to get it to work. So on a blank command line I type :

[CTRL]^r I get the new prompt : (reverse-i-search)`': I type t : and get the line (reverse-i-search)`t': startx because startx is the most recent command I used which has a "t" in it. I then type "r" and in my case I get : (reverse-i-search)`tr': transcode -i capture001.avi -x dv,avi -I 1 -C 1 -z -k -o ak-divxmp3.avi -y divx4 because this is the last command in my history which has a "tr" in it. An [Enter] executes this line as is. A [right-arrow] snaps me out of reverse-search mode and allows me to edit the line (say I need to touch up the command somehow). An [esc] quits reverse search mode and gets me back to my blank command line. If this was not exactly the version of the transcode command I wanted I could just continue hitting [CTRL]^r and this would keep searching back through my history for commands matching "tr". Try it out you'll get the hang of it.

NOTE: The unix jockeys would probably tell you that you could also type history | less. Search the listing using "/(search term)" get the number of the command in your history, quit less using "q", and type !(the number you have just found) to execute the command you have just found. But this seems to me more confusing, and more steps-- hence less laziness and that is a bad thing TM

Laziness No.4: this command line is sooooo long, and I have to change one letter, or one word way at the begining
Solution No.4: [CTRL]^a, [CTRL]^e, [CTRL]^b, [CTRL]^f, [CTRL]^h, [CTRL]^d, [ESC]^d, [CTRL]^w

Now in the last example we looked at some really long command lines. This can happen, and editing lots of long lines will get you tired and prone to error, unless you know how to get around them. Now if your first impulse was to use the mouse, you know what a mess that can make, even using the nifty cut and paste stuff with the middle mouse button under X. Being forced to [right-] and [left-arrow] all the time is surely one of the biggest time killers on the command line. When you finally learn to use one of the Unix text editors, either vi or emacs, you quickly realize how fast editing becomes, because you can zip around your text going to the ends of lines, to particular words without lifting your hands from the keyboard. Very quickly mousing loses its appeal. The bash shell gives you a lot of emacs combinations built right in. All you vi guys and gals didn't even notice that the [CTRL]^r from the last section is the standard reverse search in emacs. And you newer users who have been afraid of touching one of these editors have already learned an important emacs command. Now on to some more:

Command Moves cursor to... mnemonic
[CTRL]^e the end of the line "e" is for end
[CTRL]^a the beginning of the line "a" is the furthest left key on it's row
[CTRL]^b, [CTRL]^f back a character, forward a character (but wait isn't that just what the arrow keys do?Yes, but if one day you don't have arrow keys or one is broken you'll be happy to remember this, and see the next entry...) "b" is back, "f" is forward
[ALT]^b, [ALT]^f back a word, forward a word look ma same mnemonic!
[DEL], [CTRL]^h delete last character (Most people probably know where the [DEL] key is. It's the most used key on most key boards. What you may not know is that there were once keyboards without delete keys, and that some unices would map it to something else. Now you can understand techie-talk [CTRL]^h[CTRL]^h[CTRL]^h) none I can think of...
[CTRL]^d delete forward This is probably the combination you would expect to be mapped to the same as [DEL] but it's not. It actually makes it easier to remember "d" as in delete (forward)
[ALT]^d delete forward word "d" as in delete (forward) remember how above [CTRL]^b mapped to a letter, whereas [ALT]^b mapped to a word. You can use the same memory space to remember this one)
[CTRL]^w delete word "w" as in word
For the rest you are on your own. This one doesn't make a nice mnemonic like the others, but it works and is very handy
[CTRL]^u clear from cursor to the beginning of the line

I'm not going to go through examples using these. Somehow you have to internalize them, and you can get really fast on the command line. I would suggest to pick a few and use them all the time until they are second nature. It's really not that hard. I mean memorized [CTRL]^x, [CTRL]^c, and [CTRL]^v to copy and paste on many windowing systems right? This is the same thing. Good luck. let me know if I have been confusing.

Editors note: Macro will be teaching the upcoming Unix 1 class starting March 18, 2002 .Which will cove "commands".