2010-11-28

Taking More Control: Even More BASH Tips

This is an extension of my last BASH post, Taking Control: More BASH Tips. In that post we discussed some of the control commands that are available for clearing the screen, searching the history and moving the cursor around. This time we are going to talk about modifying the text in the current command.

CTRL+_ or CTRL+-
Let's start out with the thing that we all need sometimes, undo. Control and underscore or dash will undo the last control command done in bash. It WILL NOT undo the last command executed on the system. For example if you cut all of the text from the cursor to the beginning of the line but really ment to cut the last word you can use undo.

#I want to change badger to fox.
j2consulting:~ user1$ The quick brown badger jumps over the lazy dog.
#I hit CTRL+u instead of CTRL+w
j2consulting:~ user1$ jumps over the lazy dog.
#CTRL+- saves me from my mistake
j2consulting:~ user1$ The quick brown badger jumps over the lazy dog.


CTRL + u, CTRL + w, CTRL + k
As I showed in the previous example, you can remove text from the current command. Using these control commands you can delete text based on the current cursor position. Control and u will delete all of the text from the point of the cursor to the beginning of the command. Control and k will delete all of the test from the point of the cursor to the end of the command. Finally, control and w will delete the previous word in the command.

#I want to change badger to fox.
j2consulting:~ user1$ The quick brown badger jumps over the lazy dog.
#I move the cursor to the space between badger and jumps and then hit CTRL+w
j2consulting:~ user1$ The quick brown jumps over the lazy dog.
#Now I can add fox.
j2consulting:~ user1$ The quick brown fox jumps over the lazy dog.
#I don't like the end so I move the cursor to the space between fox and jumps and hit CTRL+k
j2consulting:~ user1$ The quick brown fox
#And now I change the ending.
j2consulting:~ user1$ The quick brown fox hides while the dog chases him.
#But now it's just wrong and I want to delete everything from the end to the beginning so I hit CTRL+u
j2consulting:~ user1$


CTRL + y
So you have just got done cutting all kinds of text, what is the next logical thing? Paste. Any of the text you cut using the previous 3 commands can be pasted into the line. Let's switch the sentence around to put the dog first.


j2consulting:~ user1$ The quick brown fox jumps over the lazy dog.
#Putting my cursor on the . and hitting CTRL+w I cut the previous word.
j2consulting:~ user1$ The quick brown fox jumps over the lazy .
#Now I put my cursor between fox and jumps and hit CTRL+y
j2consulting:~ user1$ The quick brown foxdog jumps over the lazy .
#Now I put my cursor on the d in dog and hit CTRL+w
j2consulting:~ user1$ The quick brown dog jumps over the lazy .
#Finally I put my cursor on the . again and hit CTRL+y to paste the fox
j2consulting:~ user1$ The quick brown dog jumps over the lazy fox.


CTRL + t
It is a fairly common mistake to transpose two characters while typing. Control and t can help us quickly fix this by swapping the previous two characters.

j2consulting:~ user1$ Teh
#Oops, I ment to spell The, hit CTRL+t
j2consulting:~ user1$ The
#This can also work if you want to move the cursor to the mistake.
j2consulting:~ user1$ Teh quick brown fox jumps over the lazy dog.
#By placing the cursor between the h and q and hitting CTRL+t we can fix the mistake.
j2consulting:~ user1$ The quick brown fox jumps over the lazy dog.


By using the tricks in this and previous BASH posts you can significantly reduce the time it takes you to get things done. If you know of any other tricks I'd love to hear about them in the comments.

No comments:

Post a Comment