Efficient Editing In The Command Line: Part 1

If you are like me, spending a lot of time in the command line, you might be looking for ways to edit the command lines more efficiently. Many interactive UNIX programs implement line editing by using GNU’s readline library. This means that all these programs, which include the different shells, Python and many others, offer the same set of keyboard shortcuts. Mastering these shortcuts is worthwhile, since you’ll be able to use them over and over again in many places.

Moving around quickly

The most basic part is to move to the place you want to edit quickly. Here are the most useful shortcuts:

  • Ctrl-a – Move to the start of the line.
  • Ctrl-e – Move to the end of the line.
  • Alt-f – Move forward a word.
  • Ctrl-f – Move forward a char.
  • Alt-b – Move backward a word.
  • Ctrl-b – Move backward a char.

Another very common shortcut is Ctrl-L which clears the screen and reprints the current line at the top. This is useful if you are organizing your movies library and somebody enters the room.

Cut, Paste and Undo

Here is the list of most useful shortcuts for deleting text:

  • Ctrl-w – Deletes from cursor position to the previous whitespace.
  • Ctrl-k – Deletes from cursor position to the end of the line.
  • Ctrl-d – Deletes from current character.
  • Ctrl-u – Deletes from whole line.
  • Alt-d – Deletes from cursor position to the end of the current word.
  • Alt-DEL – Deletes from cursor position to the start of the current word.

The deleted text can be pasted (yanked) using Ctrl-y. You can use Ctrl-_ or Ctrl-X-U to undo the last editing command.

Summary

This covers the essentials. On the next parts of this series we’ll explore the more powerful abilities of readline. Feel free to leave here questions, suggestions, or perhaps your own tips and tricks.

This entry was posted in linux, python, tutorial, unix. Bookmark the permalink.

8 Responses to Efficient Editing In The Command Line: Part 1

  1. Kiki says:

    But I’m a screen user, oh noes! ctrl-a won’t work. 🙁

  2. Shlomi Fish says:

    To Kiki: see this post to Linux-IL about how to change the screen command character. And you can always type Ctrl+A; A to get an actual Ctrl+A or alternatively use the “Home” key.

  3. Shlomi Fish says:

    I’ve collected my own tips for using readline in bash from the bash man page myself. Most of them are somewhat more advanced than the ones presented here.

  4. Amir says:

    Thanks.
    I needed it but didn’t have the time to look (yet).

  5. Ira says:

    I changed my screen escape character to C-_ because I thought it’s about the only combination that has no other special meaning, and now I see the long fingers of the FOSS world took hold of it before me… arrgh! 🙂

  6. Silje says:

    Thanks for the concepts you share tgrouhh this website. In addition, several young women which become pregnant don’t even aim to get health insurance because they are concerned they would not qualify. Although many states at this point require that insurers offer coverage despite the pre-existing conditions. Prices on these kinds of guaranteed programs are usually greater, but when with the high cost of health care bills it may be any safer route to take to protect the financial potential.

  7. Tip top stuff. I’ll expect more now.

  8. Hehe ja wäre geil Wollte nur zeigen, dass es hier auch was gescheites gibt.Würde dir hier gefallen! Jeder Zweite hört sich wie ein Eisenmann an (TÜV gibts hier fast nicht ) Hier fahren lauter so getunte Japan-Karren rum, 2F2F Style Und jede Billigkiste hat Chromfelgen

Leave a Reply

Your email address will not be published. Required fields are marked *