Bash One-Liners Explained, Part V: Navigating around - good coders code, great reuse |
Bash One-Liners Explained, Part V: Navigating around Posted: 27 Nov 2012 09:13 AM PST This is the fifth part of the Bash One-Liners Explained article series. In this part I'll teach you how to quickly navigate around the command line in bash using emacs-style keyboard shortcuts. That's right, emacs-style keyboard shortcuts. It might surprise you but by default bash uses emacs-style keyboard shortcuts for navigating around the command line. Keep reading to learn more! See the first part of the series for introduction. After I'm done with the series I'll release an ebook (similar to my ebooks on awk, sed, and perl), and also bash1line.txt (similar to my perl1line.txt). Also see my other articles about working fast in bash from 2007 and 2008:
Parts of this post are based on my earlier post Working Productively in Bash's Emacs Command Line Editing Mode (comes with a cheat sheet). Check it out, too! Let's start. Part V: Navigating around in emacs mode0. Introduction to input editing modes Bash uses the readline library for input editing. The readline library supports emacs style key bindings, vi style key bindings as well as custom key bindings. By default readline will use the emacs style key bindings but you can easily switch to vi editing mode or customize them. You can switch between the emacs and vi editing modes through The key bindings can be customized through the I'll cover the emacs editing mode in this article. In the next two articles I'll cover the vi editing mode and customizing readline. 1. Move to the beginning of the line Ctrl+a moves the cursor to the beginning of the line. Here's an illustration. Let's say you've typed Pressing Ctrl+a moves the cursor to the beginning of the line: 2. Move to the end of the line Ctrl+e moves the cursor to the end of the line. Here's an illustration. Let's say you've typed Pressing Ctrl+e moves the cursor to the end of the line: 3. Move one word backward Esc+b or Alt+b moves the cursor one word backward. You'll often see Meta+b but there is no such key on the keyboards anymore. Therefore either Esc+b or Alt+b will work, depending on how your terminal is configured. Here's an illustration. Let's say you've typed Pressing Esc+b or Alt+b moves the cursor one word backward: 4. Move one word forward Esc+f or Alt+f moves the cursor one word forward. Here's an illustration. Let's say you've typed Pressing Esc+b or Alt+b moves the cursor one word forward: 5. Delete the last word Ctrl+w deletes the last word. Deleting a word is also known as killing a word. Each killed word gets stored in the kill ring buffer. If you accidentally killed a word press Here's an illustration. Let's say you've typed Pressing Ctrl+w deletes 6. Paste the deleted word(s) back Ctrl+y pastes whatever is in the kill buffer back to the terminal. Here's an illustration. Let's say you had typed Pressing Ctrl+y brings 7. Move one character backward Ctrl+b moves the cursor one char backward. Here's an illustration. Let's say you had typed Pressing Ctrl+b moves the cursor one character backward: 8. Move one character forward Ctrl+f moves the cursor one char forward. Here's an illustration. Let's say you moved one character backward (as in the previous example): Pressing Ctrl+f moves the cursor one character forward: 9. Delete the whole line Ctrl+u kills the whole line and puts it in the kill buffer. Same as with killed words, you can paste the whole line back by pressing Here's an illustration. Let's say you've typed Pressing Ctrl+u deletes the whole line: 10. Search the history backward This is probably one of the most used keyboard shortcuts in bash. Pressing Here's an illustration. Let's say you had executed a complicated command such as this one: joinlines () { sed ':a; N; s/\n/'"$1"'/; ba'; } And now you wish to modify it but you don't want to keep going through the history to find it. Press 11. Search the history forward If you press The right way to go is to change the terminal behavior for $ stty stop 'undef' This will undefine the key binding for the stop-flow signal and you'll be able to use bash's
Here's an illustration. Let's say you typed Pressing 12. Exchange two adjacent characters quickly Ctrl+t transposes two characters (exchanges them) and moves the cursor one character forward. Here's an illustration. Let's say you've mistyped Pressing 13. Exchange two adjacent words quickly Esc+t or Alt+t transposes two words (exchanges them) and moves the cursor one word forward. Here's an illustration. Let's say you've typed Pressing 14. Uppercase the rest of the word Esc+u or Alt+u uppercases the rest of the word. Here's an illustration. Let's say you've typed Pressing 15. Lowercase the rest of the word Esc+t or Alt+t uppercases the rest of the word. Here's an illustration. Let's say you've typed Pressing 16. Capitalize a word Esc+c or Alt+c properly capitalizes a word. Here's an illustration. Let's say you've typed Pressing 17. Insert a raw character (such as TAB or Ctrl+c) Ctrl+v inserts the next character typed verbatim. For example, Ctrl+v followed by <TAB> would insert a literal tab in the command line, or Ctrl+v followed by Ctrl+m would insert a Windows newline (aka carriage return CR). Here's an illustration. Let's say you've typed Pressing 18. Comment the current line (append # at the beginning quickly) Esc+# or Alt+# quickly comments the line. Here's an illustration. Let's say you typed Pressing 19. Open the current command in a text editor quickly Pressing CTRL+x followed by CTRL+e opens the current command in your favorite text editor. Exiting the editor will execute the command. 20. Delete a character to the left Ctrl+h deletes the character to the left of the cursor. Here's an illustration. Let's say you've typed Pressing 21. Delete a character to the right Ctrl+d deletes the character to the right of the cursor. Here's an illustration. Let's say you've typed Pressing 22. Incremental undo Pressing Ctrl+x followed by Ctrl+u undoes a change. Here's an illustration. Let's say you typed Pressing 23. Insert the last argument from the previous command Esc+. or Alt+. inserts the last argument from the previous command at the current cursor position. Here's an illustration. Let's say you had run And now you want to extract the archive. So all you've to do is type 24. Undo all changes to the line Esc+r or Alt+r undoes all changes to the line. It's useful when you're going through the command history with Here's an illustration. Let's say you searched for And let's say you wanted to make some changes to the command but messed up: Pressing 25. Clear the screen Ctrl+l clears the screen. Alternatively you can type 26. Change input mode to vi $ set -o vi This command changes the key bindings to vi's. If vi's your favorite editor, you'll love this. I'll cover the vi mode in more details in the next part of the article. Enjoy!Enjoy the article and let me know in the comments what you think about it! |
You are subscribed to email updates from good coders code, great reuse To stop receiving these emails, you may unsubscribe now. | Email delivery powered by Google |
Google Inc., 20 West Kinzie, Chicago IL USA 60610 |
No comments:
Post a Comment
Keep a civil tongue.