A Short Intro to the Power of Command Line Interface

Posted in linux with tags command-line -

Today I would like to present to you about the power of command-line interface (CLI), as a better alternative to the graphical user interface (GUI) that everyone uses every day (well, maybe not exactly everyone, we have to subtract people who are too old, too young, in comma, etc., but you know what I mean), by giving you a simple example. This is the first in a series that I tend to write about cli application, and how they will help you become more efficient in your tasks. Hopefully it will raise your interest towards it, and we’ll have some fun talking about this hard but simple way of working with computers.

Q: Wait, what is that GUI thing you are saying that everyone’s familiar with? It sounds strange, to me at least.

A: Yes, the word maybe new to a lot of people, but basically it’s everything related to using all sort of graphical elements (images, colors, buttons, menus, etc. ) in a user interface.

Q: What is a user interface then?

A: In this context, it can be understood as the way you, as a user, interact with a application: how you type the url into your browser to tell it which website you want to visit, for example.

In CLI, you interact with the computer (or, essentially, the program) by providing it some direct commands. Of course, those commands need to be precise, and need to be typed in with keyboard (or with mouse by copy/paste), rather than the kind of commands you can give to, for e.g. your Siri. In fact, while GUI is known by more people, CLI was the default way of using a computer until recently, and it is still much more powerful than GUI in most of tasks, despite the less fancy look. Anyway, to really picture how CLI works, let’s go to the example:

Mozilla Firefox, or Firefox, is a great browser that is quite common nowadays, so I bet that most of you have used it, or at least heard of it. Now let’s say you would like to open Firefox and use it to enter a website, say huymai.fi, how are you gonna do it?

Many of you would come up with an answer: you click on the Firefox icon (in your Windows’ Desktop, or your MacOS Dock), click on the address field, type the domain of the website and press Enter/Return. It’s a pretty simple task, which includes from two to three clicks (depending on where your Firefox icon is located), and some typing on keyboard. The whole process takes about 10 seconds so it’s likely that you won’t even notice how SLOW it is.

Slow? Are you kidding me?

Yes, while 10 seconds might be nothing, the whole process that I just described is relatively quite complex and slow for such a simple task. First of all, it takes time to move your hand from your mouse to your keyboard, so at least you can save half a second or so if you can use only one of these input devices for the task. Second and most important, with a mouse you have to move your hand quite a bit for the pointer to reach the screen position where you can click, and also the pointer can only click in one position at a time, so even though the mouse is pretty handy, it slows down the process. If you’ve tried using the virtual keyboard, in which you use your mouse to click on the keyboard buttons on a screen to replace your physical keyboard, you’ll understand how slow the mouse is comparing to the keyboard.

But isn’t it supposed to be that way? We have to have the mouse because the keyboard can’t do everything.

Well, not really. Or at least not in this case. For example, on Windows, you can press the Windows Key (the little button with a window logo on your keyboard) to activate the Start button (or maybe it’s called something else nowadays, I don’t use Windows anymore so pardon me), or in Mac you can press Cmd + Space to activate Spotlight. From there, you can type firefox and when the Firefox icon appears, press Enter/Return to open it (normally you just need to type some first few characters of the app name for the icon to pop up). With Firefox opened, you can press Ctrl+L on Windows or Cmd+L on Mac and you’ll notice that the cursor jumps directly to the address bar. From there you know what to do: type in your website’s address and press Enter/Return. See? Your hands never have to move away from the keyboard.

However, what if I told you there’s an even faster way?

No, I’m not kidding. You can achieve the same result with even shorter time than the aforementioned procedure, just by using Firefox’ command line interface. So if you have your Terminal Emulator opened, everything you need to do is typing directly the thing you have in mind (“Firefox, please open huymai.fi) like this:

firefox huymai.fi

And then press Enter/Return as a way of saying “That’s the end of my command, now execute it”.

Firefox will automatically open a new window (if it’s not already opened) or a new tab and bring you to the website you want. The time you save will be even greater if instead of simply open the website, you would like to open it in a private window. It will cost you at least two more mouse drags and mouse clicks to open a private window in Firefox in the normal “mouse way”, and you will also have a not-needed blank normal Firefox window that you may want to close. For cli, what you need is simply tell firefox to open the website in private mode by adding --private-window to your command:

firefox --private-window <url>

And Firefox will do exactly what you need, no more and no less (so this time you won’t have that extra normal window).

In addition, you can also make Firefox do many more stuff by using the command line. To get a full list of those commands, type firefox -h into your terminal (-h is a common shorthand to call for “help”)

Bonus: If you are lazy (like me), you can even save a few more key presses by typing “fire” and press the “TAB” key. “TAB” is the key right above your CAPLOCKS key, and it is a way of telling your terminal to autofill the command for you based on the characters you are typing, and normally if you only have one application whose name starts with these characters, most terminal will fill up the command you need. You can even set an alias for a common task, or setup the terminal so that it show you a list of apps, that you can choose interactively, in case there are more than one command starts with the string you typed, but more on those tricks in later posts

I hope the example was clear and did give you some concept of how a very simple task can be even simpler in CLI, and guess what, the difference in efficiency will get dramatically larger when it comes to more complex tasks. Future posts in this series will provide some more details to help you improve your workflow with these new tools. See you next time then!

Written by Huy Mai
Later article
About Me