The following text was based on the capabilities of GNU bash version 3.2.15(1). If you're using a different version of bash, your mileage may vary. Please, consult the bash man page if your bash shell does not work as described here.
Remember, dog is not your friend... man is.
Most Linux distributions come with bash and is the default login shell for user accounts on the box. You can check which shell you are using with the following command in your shell:
echo $SHELL
If this simple command returns "/bin/bash", "/usr/bin/bash", or "/usr/local/bin/bash", then you are using bash and can use the below tricks in your shell.
1. Searching through your command line history
Say you're working in the shell and previously ran a command but get tired of scrolling back through hundreds of commands in the history to find it. Here comes readline support to the rescue.
To find the command you're looking for, press '[CTRL]-r' then type a portion of the command and the readline support will seach backward through the history for the command.
Once you find the command, you can press '[ENTER]' to run it or press '[ESC]' to place the displayed command on the command line for editing before running it. If you change your mind and don't want to run the command you're searching for, press '[CTRL]-c' to abort the search.
If the command displayed is not the one your looking for, press '[CTRL]-r' again and bash will seach backward through the history for the next match. Likewise, if you passed the command you wanted to run, press '[CTRL]-s' to seach forward through the history for the command.
If the portion of the command you typed is too broad to find the command you are looking for, you can continue to type out the command to narrow the search.
You can read more about this and more in the "Commands for Manipulating the History" section of the bash man page (type 'man bash').
2. Moving around on the command line
It can get quite annoying to move the cursor around on the command line at times with the arrow keys---especially if the command is 3 or more lines long.
To move to the beginning of the line, press '[Home]' or '[CTRL]-a'.
To move to the end of the line, press '[End]' or '[CTRL]-e'.
To move to the next word, press '[ALT]-f'. You can provide a count of words by pressing '[ALT]-#' before this key combination where '#' is the number for words.
To move to the previous word, press '[ALT]-b'. Again, you can provide a count of words by pressing '[ALT]-#' before this key combination where '#' is the number for words.
3. Manipulating your typed command
a) Say you typed a command in the shell then realize you transposed two characters. Instead of moving your cursor to the transposed characters deleting them and typing them in the correct order, do the following.
Move your cursor to the second character and press '[CTRL-t]'. The readline support will swap the two characters for you.
The same thing can be done with words. If your transposed two directory names (i.e. /log/var/messages instead of /var/log/message), options, or commands in a command list; move your cursor into the second word and press '[ALT]-t'. The readline support will swap the two words for you and is smart enough to do the right thing for the most part. If you want to swap long and short options, readline will not do the right thing.
b) Now, say you typed in a command but did not realize the caps lock was on. No problem.
Go to the beginning of the command then press '[ALT]-l' for all the words in the command to change them to lowercase. Or, you can provide a count of words by pressing '[ALT]-#' before this key combination where '#' is the number for words.
You can change all letters in a word to uppercase by pressing '[ALT]-u' or capitalize a word by pressing '[ALT]-c'. You can provide a count of words to change with '[ALT]-#' too.
Keep in mind, however, that '[ALT]-l', '[ALT]-u' and '[ALT]-c' operate from the current cursor position forward, which means you can change a portion of a word. You can also change words from the current cursor position backward by pressing '[ALT]--' (That's [ALT]-{Minus}) and optionally follow it by the number of words to change before pressing the above key combinations.
You can read more about this in the "Commands for Changing Text" section of the bash man page (type 'man bash').
c) OK, you ran a command previously and pulled it up from the command line history but want to change options or path(s) or want to run a different command on the same path. There are several ways to do this efficiently.
To delete everything from the current cursor position to the end of the command, press '[CTRL]-k'.
To delete everything from the current cursor position to the beginning of the command, press '[CTRL]-x' followed by '[Backspace]'.
To delete everything from the current cursor position to the end of the word, press '[ALT]-d'. You can provide a count of words by pressing '[ALT]-#' before this key combination where '#' is the number for words.
To delete everything from the current cursor position to the beginning of the word, press '[ALT]-[Backspace]'. Again, you can provide a count of words by pressing '[ALT]-#' before this key combination where '#' is the number for words.
To delete everything from the current cursor position backward but stop at white space, press '[CTRL]-w'. And yes, you can provide a count of words by pressing '[ALT]-#' before this key combination where '#' is the number for words.
You can read more about this and more in the "Killing and Yanking" section of the bash man page (type 'man bash').
d) All that text on the screen bothering you or getting in the way of reading the current command you are typing? No problem, press '[CTRL]-l' and the screen will be cleared with your current command moved to the top of the screen.
No comments:
Post a Comment