The previous part of this series we introduced some command line basics such as what a command-line is, some things you can do with a command line, using package managers, and a few of the basic commands. In this part, we’re going to dive deeper and look at some more advanced command-line topics; it’s time to put those Neo sunglasses on and fire up the terminal.
Nano, VIM and Emacs
If you’re going to use the terminal like a warrior, you’ll need a way to enter and save text/code. As you can imagine, there are several programs for this, and many a battle has been waged over which is best. Not wishing to start another text editor war, I’ll cover 3 of the most common.
Nano comes pre-installed on many UNIX/Linux based machines so it’s good to be familiar with it and how it works. Nano is designed to be simple to use, and still pack some great features such as syntax highlighting and undo/redo. Out of the 3 text editors here, Nano can be considered the most bare-bones, but this is also one of its advantages, it starts up instantly, and is super fast at most operations. If you only need to edit text on the command-line infrequently, you might never need to look at more powerful text editors such as Vim and Emacs. For a great howto guide and tutorial about using Nano, check out the tutorial at HowTo-Geek.
Vim is extremely powerful and my preferred text editor, but this power comes at a cost: a big learning curve. For command-line beginners and those just casually looking into the command-line I would recommend against Vim initially, instead just use Nano or something less complex and if you one day want to get into Vim then you can put aside a few weeks to learn the basics (and it then can take many years to truly master it). With that said, Vim is an awesome text editor that has many powerful extensions and customisation options. If you do want to start learning Vim, I recommend the book Practical Vim and the awesome learning resource Vimcasts.
You turn Vim into your text editor. Here’s a picture of my current setup:
Emacs is not just a text editor, it is practically an operating system in itself! With Emacs you can get your emails, do FTP, check your RSS feeds, keep notes, manage todo lists, track your time, manage schedules and more. That is why for some Emacs is completely indispensable. But all those added features sit on top of a truly great text editor. As with Vim however, there is a steep learning curve to master Emacs, but it is easier than Vim for most people and they will master it in months rather than years. If you want to start learning Emacs, I recommend reading through the EmacsWiki.
SSH is arguably one of the most powerful features available to the command-line. SSH is not exclusively a command-line feature, for example many FTP clients offer SSH transport methods, but it is extremely efficient and useful through the command-line and for me one of the things that makes the command-line invaluable. At its most basic level, SSH allows you to securely connect to a remote host machine and execute commands on that remote machine as if you were doing so locally, for example it is frequently used by developers to “remote into” servers and execute commands/run programs, it simply needs both machines to be set to allow SSH connections (by opening port 22). To connect to a remote machine you would simply type something like this:
$ ssh email@example.com
If the user exists on that remote machine you will, by default, then be asked for their password. I however recommend against using passwords and instead use SSH keys. There’s a great tutorial about how to setup and use SSH keys on the Digital Ocean Community.
Of course nothing is prefect, and SSH does have a few inconveniences. For example, on most configurations, if you do not interact with an SSH session for a period of time (usually around 10 minutes) the connection will be terminated. If you are on the go and changing wifi connections etc you will almost always have the connection terminated. Luckily, some people have come up with a great wrapper for SSH which manages and alleviates these issues, it’s called mosh. You need to install mosh on both the local and remote machine for it work but it isn’t hard and once enabled you can simply use the mosh command instead of SSH and enjoy a much more robust connection.
$ mosh firstname.lastname@example.org
Tmux is a terminal multiplexer. OK. So what the frell is a terminal multiplexer? Basically it allows you to have one or more “terminals’ at once, arranged in a tiled grid manner. Here’s a picture showing 7 terminals:
This is a bit extreme of course, not many situations need 7 terminals, but it does demonstrate how the tiling works. You can arrange tiles however you like, swap their positions and more. Another great feature about Tmux though is that it creates its own sessions, allowing you to “detach” from them and, when you are ready, reattach. This is really useful if you’re doing some work on a remote machine and have a great Tmux setup with Vim in one window, an error log in another, Grunt in another etc but suddently it’s home time and you don’t want to have to set this all up again the next time you SSH into the remote machine. If you had been using Tmux, you could simply hit the key combo ctrl + b then the letter d. You would now be detached and could exit. Then when you next SSH into the remote machine, you would simply type
$ tmux a at the command prompt and be put right back where you left off. It’s like magic! Here’s a screenshot showing Tmux in action.
Note: You will lose the Tmux session if something drastic happens on the remote machine such as a reboot.