Friday, April 19, 2013

Using The Command Line

The Command Line (CLI) is the primary user inferface of many Unix-like operating systems. Over the years it has evolved into a powerful thing which will make your life a lot easier if you invest some time to learn it.

This post is based on one of my internal presentations which I created for my teammates at Netsafe. It assumes that you are already familiar with the CLI.

I'll try to summarize stuff that I find most useful and that I wish I had known back when I started using Linux. It will certainly not cover everything as I think that's not even possible. The journey to mastering the CLI is endless anyways. :)

I'm using BASH as my shell, so some things might differ if you use something else.

Keyboard Shortcuts

In order to use the CLI efficiently it's good to know some basic keys to move around and edit text. By default BASH uses Emacs-like keyboard shortcuts. There is also a VI-like mode but I'm not going to cover that.

Please note that most of these key bindings also work in other programs (e.g. mysql) which use the readline library to handle user input. Some of them even work in OSX GUI applications.

Movement
  • Ctrl-a Move the cursor to the start of the line
  • Ctrl-e Move the cursor to the end of the line
  • Alt-f Move forward a word
  • Alt-b Move backward a word

movement

Editing
  • Alt-Backspace Kill the previous word
  • Alt-d Kill to the end of the word
  • Ctrl-u Kill to the beginning of the line
  • Ctrl-k Kill to the end of the line
  • Ctrl-y Paste the previously killed text
  • Ctrl-_ Undo

editing

  • Alt-. Paste the last argument of the previous command (hit again to get older items)

last-arg

Personally I find Alt-. super useful. I'm using it several times a day instead of manually doing copy&paste using mouse or retyping the thing.

History
  • Ctrl-r Incrementally search the line history (hit again to get older items)
  • Ctrl-g Abort an incremental search and restore the original line
  • Ctrl-p Previous item in history
  • Ctrl-n Next item in history

history

Others
  • Ctrl-l Clear the terminal
  • Ctrl-d Exit shell

Directrory structure navigation

In CLI one uses the cd command to change the current directory. Plain cd without any argument switches the current directory to the user's home. It's the same as typing cd ~.

There are a few tricks which make jumping around easier.

  • cd - switches to the previous directory (can be used multiple times to jump back and forth)

cd

  • CDPATH - By default cd some-dir changes to some-dir located in the current directory. If you have a bunch of directories which you visit often, you can make your life easier with CDPATH. For example, if you keep all your projects in ~/Projects, you can append this path to CDPATH and use cd project-name to jump to the given project. It doesn't matter what your current directory is at the time you run cd.

cdpath

Completion

If you have the bash-completion package installed and enabled you will be able to complete almost anything using the TAB key. This includes directory names, commands, command switches, git branches, server names, environment variables and so on. If you hit TAB multiple times it will show possible completions.

completion

Thanks kotfic and pkkm (of #emacs fame) for the grammar check. :)

No comments:

Post a Comment