Search for a previous command with the prefix I just typed
Clash Royale CLAN TAG#URR8PPP
up vote
40
down vote
favorite
We can use up and down arrow to navigate in command history.
In some IDEs, such as Matlab, if we input something and then press the arrow keys, we scroll among only the history commands starting with what we have input. That's really convenient, but in a shell terminal, this doesn't work.
Is there some way to gain a similar function in a shell terminal? And any other tips for improving efficiency in terminal use?
bash shell command-history
 |Â
show 2 more comments
up vote
40
down vote
favorite
We can use up and down arrow to navigate in command history.
In some IDEs, such as Matlab, if we input something and then press the arrow keys, we scroll among only the history commands starting with what we have input. That's really convenient, but in a shell terminal, this doesn't work.
Is there some way to gain a similar function in a shell terminal? And any other tips for improving efficiency in terminal use?
bash shell command-history
1
Besides ^r / ^s history i-search,alt .
inserts the last "word" of previous commands at the cursor. This is super handy formkdir foo
,cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word),cd
. More powerful mixing/matching of commands comes from using the kill-ring. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text. Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.
â Peter Cordes
Sep 23 '15 at 21:38
converted to an answer.
â Peter Cordes
Sep 23 '15 at 21:44
You could simply use a shell that provides such a feature, likefish
;)
â yoann
Sep 23 '15 at 23:13
@yoann you're assuming bash doesn't. It does, via readline, just not accessible by default.
â muru
Sep 24 '15 at 8:35
1
@yoann check out unix.stackexchange.com/a/76591/70524. Instead ofe[5~
ande[6~
, usee[A
,e[B
.
â muru
Sep 24 '15 at 13:02
 |Â
show 2 more comments
up vote
40
down vote
favorite
up vote
40
down vote
favorite
We can use up and down arrow to navigate in command history.
In some IDEs, such as Matlab, if we input something and then press the arrow keys, we scroll among only the history commands starting with what we have input. That's really convenient, but in a shell terminal, this doesn't work.
Is there some way to gain a similar function in a shell terminal? And any other tips for improving efficiency in terminal use?
bash shell command-history
We can use up and down arrow to navigate in command history.
In some IDEs, such as Matlab, if we input something and then press the arrow keys, we scroll among only the history commands starting with what we have input. That's really convenient, but in a shell terminal, this doesn't work.
Is there some way to gain a similar function in a shell terminal? And any other tips for improving efficiency in terminal use?
bash shell command-history
bash shell command-history
edited Jul 17 at 5:15
slmâ¦
241k66501669
241k66501669
asked Sep 23 '15 at 16:43
Lee
8112819
8112819
1
Besides ^r / ^s history i-search,alt .
inserts the last "word" of previous commands at the cursor. This is super handy formkdir foo
,cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word),cd
. More powerful mixing/matching of commands comes from using the kill-ring. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text. Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.
â Peter Cordes
Sep 23 '15 at 21:38
converted to an answer.
â Peter Cordes
Sep 23 '15 at 21:44
You could simply use a shell that provides such a feature, likefish
;)
â yoann
Sep 23 '15 at 23:13
@yoann you're assuming bash doesn't. It does, via readline, just not accessible by default.
â muru
Sep 24 '15 at 8:35
1
@yoann check out unix.stackexchange.com/a/76591/70524. Instead ofe[5~
ande[6~
, usee[A
,e[B
.
â muru
Sep 24 '15 at 13:02
 |Â
show 2 more comments
1
Besides ^r / ^s history i-search,alt .
inserts the last "word" of previous commands at the cursor. This is super handy formkdir foo
,cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word),cd
. More powerful mixing/matching of commands comes from using the kill-ring. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text. Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.
â Peter Cordes
Sep 23 '15 at 21:38
converted to an answer.
â Peter Cordes
Sep 23 '15 at 21:44
You could simply use a shell that provides such a feature, likefish
;)
â yoann
Sep 23 '15 at 23:13
@yoann you're assuming bash doesn't. It does, via readline, just not accessible by default.
â muru
Sep 24 '15 at 8:35
1
@yoann check out unix.stackexchange.com/a/76591/70524. Instead ofe[5~
ande[6~
, usee[A
,e[B
.
â muru
Sep 24 '15 at 13:02
1
1
Besides ^r / ^s history i-search,
alt .
inserts the last "word" of previous commands at the cursor. This is super handy for mkdir foo
, cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word), cd
. More powerful mixing/matching of commands comes from using the kill-ring. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text. Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.â Peter Cordes
Sep 23 '15 at 21:38
Besides ^r / ^s history i-search,
alt .
inserts the last "word" of previous commands at the cursor. This is super handy for mkdir foo
, cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word), cd
. More powerful mixing/matching of commands comes from using the kill-ring. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text. Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.â Peter Cordes
Sep 23 '15 at 21:38
converted to an answer.
â Peter Cordes
Sep 23 '15 at 21:44
converted to an answer.
â Peter Cordes
Sep 23 '15 at 21:44
You could simply use a shell that provides such a feature, like
fish
;)â yoann
Sep 23 '15 at 23:13
You could simply use a shell that provides such a feature, like
fish
;)â yoann
Sep 23 '15 at 23:13
@yoann you're assuming bash doesn't. It does, via readline, just not accessible by default.
â muru
Sep 24 '15 at 8:35
@yoann you're assuming bash doesn't. It does, via readline, just not accessible by default.
â muru
Sep 24 '15 at 8:35
1
1
@yoann check out unix.stackexchange.com/a/76591/70524. Instead of
e[5~
and e[6~
, use e[A
, e[B
.â muru
Sep 24 '15 at 13:02
@yoann check out unix.stackexchange.com/a/76591/70524. Instead of
e[5~
and e[6~
, use e[A
, e[B
.â muru
Sep 24 '15 at 13:02
 |Â
show 2 more comments
9 Answers
9
active
oldest
votes
up vote
54
down vote
accepted
What you are looking for is CtrlR.
Type CtrlR and then type part of the command you want. Bash will display the first matching command. Keep typing CtrlR and bash will cycle through previous matching commands.
To search backwards in the history, type CtrlS instead. (If CtrlS doesn't work that way for you, that likely means that you need to disable XON/XOFF flow control: to do that, run stty -ixon
.)
This is documented under "Searching" in man bash
.
1
You should mention needing to turn off XON/XOFF flow control to use Control-S (e.g., viastty -ixon
).
â derobert
Sep 23 '15 at 17:37
@derobert Excellent point. Answer updated withstty -ixon
info.
â John1024
Sep 23 '15 at 17:53
You can format keyboard shortcuts with<kbd>
.
â l0b0
Sep 23 '15 at 21:20
add a comment |Â
up vote
21
down vote
Place these in your ~/.inputrc
:
"e[5~": history-search-backward
"e[6~": history-search-forward
These make Page Up and Page Down behave as you wish. (Some distributions have it already configured it for you.) I personally find these way more convenient than Ctrl+R or history
.
yeah, I tried. indeed wonderful.
â Lee
Sep 23 '15 at 18:10
This is the most convenient way in all current answers. Could u plz give a little explanation on the working mechanism?
â Lee
Sep 24 '15 at 8:43
3
~/.inputrc
or/etc/inputrc
or the file referenced by the environment variable$INPUTRC
is the line editing configuration file of all application using readline, including bash (your shell). It has certain built-in line editing capabilities, including these two: in bash's manual page search for one of these keywords to find out more.e[5~
/e[6~
are the escape sequences generated by the Page Up/Down keys in most of the terminal emulators; to see it, launch thecat
command and then press these keys (the escape byte is represented in the terminal as^[
, in inputrc ase
).
â egmont
Sep 24 '15 at 8:55
The example in your original question says that the Up / Down keys work this way in Matlab. It would be interesting to see what happened if you placed the sequence of Up and Down in inputrc and assign to this action; would it work correctly, or have nasty side effects as it conflicts with their default behavior. I don't know, I'd be happy to hear your findings if you experiment with this.
â egmont
Sep 24 '15 at 8:58
1
I personally prefer overriding up and down arrow to do this:"e[A": history-search-backward
and"e[B": history-search-forward
â A.Wan
Jun 7 at 23:32
 |Â
show 3 more comments
up vote
7
down vote
Besides ^r / ^s history i-search:
alt. inserts the last "word" of the previous command at the cursor. Repeat it to get the last word from older commands. (But note that a trailing &
counts as the last word for background commands).
This is super handy for mkdir foo
, cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word), cd
.
To get the 2nd-to-last word, use esc
-
2
alt+.
(i.e. use an emacs-style numeric argument to alt+.
. Negative counts in from the end, positive counts forward from the start.) But this is usually more trouble than it's worth; at some point it's faster to reach for the mouse and copy/paste, or up-arrow and ^w / ^y part of it (see below).
If your terminal is set up nicely/properly, ctrl-left and ctrl-right will go backward/forward by words. If not, hopefully at least alt-b and alt-f will do the same thing.
ctrl-/ is an undo. You can use auto-repeat for deleting words much more efficiently if you can undo when you overshoot by a bit.
More powerful mixing/matching of commands comes from using the kill-ring, which works just like in Emacs. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text.
Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.
Combining all of these together, you can
- start typing a command
- ctrl-r to go back to an old command and grab part of it with control-w or similar.
- esc-r or
alt+r
to restore it to how it was before you deleted part of it. - alt-
>
to go to the end of history (i.e. down-arrow all the way), to get back to the command you were in the middle of.
Other interactive-use tips:
Enable shopt -s globstar
, so you can do **/*.c
(recursive including the current dir). Sometimes handy for interactive use, but usually find -name '*.c' -exec foo +
is better.
If you write bash scripts, you'll find it handy to have shopt -s extglob
enabled in your interactive shells, too. You will sometimes find a use for stuff like *.!(c|h)
to match files that don't end with .c or .h.
Find aliases you like for ls -l
, less
, and anything else you do a lot. (cp -i
, mv -i
, and rm -I
are nice. Don't get in the habit of depending on them to do a selective rm. GNU rm's -I asks once for all the args.)
I like alias m=less
(m for "more"). I have less
set up with , and . bound to previous / next file (lesskey
). The default is a multi-keypress sequence that can't be used with autorepeat.
I like to do everything inside GNU screen. I find it easier to keep track of numbered screen-windows than a boatload of tabs in Konsole (or any other terminal emulator I've tried). If you don't already know screen
, learn tmux because it's newer and less crufty.
To get something like the functionality of opening a new shell with the same cwd as another shell, I use a custom hook for cd/pushd/popd that lets me do cds 8
to cd to whatever dir my shell in screen window 8 is using. This works even for shells outside of the screen session, as long as there's only one screen session.
Mine doesn't seem to be "properly configured". A lot of the shortcuts I see here don't work, I am using stock Terminal from Ubuntu 14.04. How can I configure it?
â JorgeeFG
Sep 24 '15 at 12:47
@JorgeeFG: I think it worked for me out of the box withgnome-terminal
in Ubuntu 14.04, before I switched to KDE. The relevant option is in your terminal settings, esc-prefix vs high-bit, and maybe some other settings. Some of them don't work perfectly for me through ssh. (e.g. ctrl left/right munge the escape-code for the arrow key, and I get5D
appearing literally)
â Peter Cordes
Sep 24 '15 at 17:38
add a comment |Â
up vote
4
down vote
I always tend to configure my machines with an large HISTSIZE value so it keeps a longer history list, as well as HISTTIMEFORMAT with the time stamp value so I can see when was the command ran.
export HISTSIZE=10000
export HISTTIMEFORMAT="%m/%d/%Y %T "
add a comment |Â
up vote
3
down vote
Add the following lines to your ~/.inputrc
"e[A": history-search-backward
"e[B": history-search-forward
This is similar to egmont's answer, but instead of using Page Up and Page Down, it uses the escape sequences for Arrow Up and Arrow Down. This way is much more convenient to use on a Mac.
add a comment |Â
up vote
2
down vote
The means I usually use is to combine the history
command with grep
IE:
history | grep <command im looking for>
That will display a numbered history of commands you have typed that contain that command, you can then use:
!<number listed in history command>
to redo that command.
IE:
history | grep history
142 history
143 history | grep lvresize
568 history | grep echo
570 history | grep echo
571 history | grep history
followed by:!571
will repeat history | grep history
1
C-R = much better + much safer
â PSkocik
Sep 23 '15 at 17:16
Perhaps, Personally I find a history piped to grep to be a faster means of finding most commands. As far as Safer, I'm not sure what you mean, what are the inherent risks in using history as apposed to searching through it with c-R?
â Gravy
Sep 23 '15 at 17:25
2
@Gravy I suspect PSkocik means the!
history substitution is less safe.
â derobert
Sep 23 '15 at 17:27
3
@Gravy The inherent risks would be accidentally getting the wrong line. No idea why your terminal has frozen, unless you used Control-S and you have xon/xoff flow control enabled (often the default). If so, Control-Q will bring it back.
â derobert
Sep 23 '15 at 17:36
1
I agree with @PSkocik, c-r is much better because it's easier to edit the history line, rather than just run it again the same way. Running the exact-same command again is a trivial case of what you can do to save time re-typing variations on complex one-liners, or tab-completing a different file in a long path. See my answer.
â Peter Cordes
Sep 23 '15 at 22:06
 |Â
show 3 more comments
up vote
1
down vote
I used daily 4 tips (and some others) in my bash terminal:
Execute last command:
!!
Execute command N from history:
!N
Print last command starting with prefix from history:
!prefix:p
Execute last command starting with prefix from history:
!prefix
add a comment |Â
up vote
1
down vote
Add the following alias to ~/.bashrc
alias h='history | awk '"'"'$1="";print'"'"' |sed "s/^ *//" |grep -v "^h " | sort | uniq | grep'
Usage: "h fire" will get me a list of commands contain fire. or "h ^fire" will get me a list of commands begin with fire.
This is useful when I want to view a series of commands at the same time.
By the way, '"'"' is ugly but I couldn't find other ways to avoid single quotes in single quotes.
fyi -h="history | awk '$1=""; print'
should work. alternatively create a function and call that.
â DarkHeart
Jun 17 '16 at 1:37
thank you DarkHeart, but I tried your code, found not worked in my environment, maybe there is something different in mine
â Hosi Golden
Jun 17 '16 at 3:09
add a comment |Â
up vote
0
down vote
If it is OK for you to use zsh
instead of bash
you cona use oh-my-zsh for that.
It contains the arrow up down navigation through your command history. It allows also, that same navigation when you have already typed text (as Mathlab).
edit: It also contains other developer-focused stuff, such as aliases and themes. That extras make the distro bigger and more complex to use.
https://github.com/robbyrussell/oh-my-zsh
Hope it helps
oh-my-zsh
contains tons of stuff. You might as well say: This exact feature is included in that distro, when all you need is a specific setting or app.
â muru
Sep 24 '15 at 4:17
I edited the post: I warned that it is bigger than a simple app.
â xyz
Sep 24 '15 at 4:44
add a comment |Â
9 Answers
9
active
oldest
votes
9 Answers
9
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
54
down vote
accepted
What you are looking for is CtrlR.
Type CtrlR and then type part of the command you want. Bash will display the first matching command. Keep typing CtrlR and bash will cycle through previous matching commands.
To search backwards in the history, type CtrlS instead. (If CtrlS doesn't work that way for you, that likely means that you need to disable XON/XOFF flow control: to do that, run stty -ixon
.)
This is documented under "Searching" in man bash
.
1
You should mention needing to turn off XON/XOFF flow control to use Control-S (e.g., viastty -ixon
).
â derobert
Sep 23 '15 at 17:37
@derobert Excellent point. Answer updated withstty -ixon
info.
â John1024
Sep 23 '15 at 17:53
You can format keyboard shortcuts with<kbd>
.
â l0b0
Sep 23 '15 at 21:20
add a comment |Â
up vote
54
down vote
accepted
What you are looking for is CtrlR.
Type CtrlR and then type part of the command you want. Bash will display the first matching command. Keep typing CtrlR and bash will cycle through previous matching commands.
To search backwards in the history, type CtrlS instead. (If CtrlS doesn't work that way for you, that likely means that you need to disable XON/XOFF flow control: to do that, run stty -ixon
.)
This is documented under "Searching" in man bash
.
1
You should mention needing to turn off XON/XOFF flow control to use Control-S (e.g., viastty -ixon
).
â derobert
Sep 23 '15 at 17:37
@derobert Excellent point. Answer updated withstty -ixon
info.
â John1024
Sep 23 '15 at 17:53
You can format keyboard shortcuts with<kbd>
.
â l0b0
Sep 23 '15 at 21:20
add a comment |Â
up vote
54
down vote
accepted
up vote
54
down vote
accepted
What you are looking for is CtrlR.
Type CtrlR and then type part of the command you want. Bash will display the first matching command. Keep typing CtrlR and bash will cycle through previous matching commands.
To search backwards in the history, type CtrlS instead. (If CtrlS doesn't work that way for you, that likely means that you need to disable XON/XOFF flow control: to do that, run stty -ixon
.)
This is documented under "Searching" in man bash
.
What you are looking for is CtrlR.
Type CtrlR and then type part of the command you want. Bash will display the first matching command. Keep typing CtrlR and bash will cycle through previous matching commands.
To search backwards in the history, type CtrlS instead. (If CtrlS doesn't work that way for you, that likely means that you need to disable XON/XOFF flow control: to do that, run stty -ixon
.)
This is documented under "Searching" in man bash
.
edited Sep 23 '15 at 21:30
muru
34.2k578148
34.2k578148
answered Sep 23 '15 at 16:57
John1024
44.9k4101117
44.9k4101117
1
You should mention needing to turn off XON/XOFF flow control to use Control-S (e.g., viastty -ixon
).
â derobert
Sep 23 '15 at 17:37
@derobert Excellent point. Answer updated withstty -ixon
info.
â John1024
Sep 23 '15 at 17:53
You can format keyboard shortcuts with<kbd>
.
â l0b0
Sep 23 '15 at 21:20
add a comment |Â
1
You should mention needing to turn off XON/XOFF flow control to use Control-S (e.g., viastty -ixon
).
â derobert
Sep 23 '15 at 17:37
@derobert Excellent point. Answer updated withstty -ixon
info.
â John1024
Sep 23 '15 at 17:53
You can format keyboard shortcuts with<kbd>
.
â l0b0
Sep 23 '15 at 21:20
1
1
You should mention needing to turn off XON/XOFF flow control to use Control-S (e.g., via
stty -ixon
).â derobert
Sep 23 '15 at 17:37
You should mention needing to turn off XON/XOFF flow control to use Control-S (e.g., via
stty -ixon
).â derobert
Sep 23 '15 at 17:37
@derobert Excellent point. Answer updated with
stty -ixon
info.â John1024
Sep 23 '15 at 17:53
@derobert Excellent point. Answer updated with
stty -ixon
info.â John1024
Sep 23 '15 at 17:53
You can format keyboard shortcuts with
<kbd>
.â l0b0
Sep 23 '15 at 21:20
You can format keyboard shortcuts with
<kbd>
.â l0b0
Sep 23 '15 at 21:20
add a comment |Â
up vote
21
down vote
Place these in your ~/.inputrc
:
"e[5~": history-search-backward
"e[6~": history-search-forward
These make Page Up and Page Down behave as you wish. (Some distributions have it already configured it for you.) I personally find these way more convenient than Ctrl+R or history
.
yeah, I tried. indeed wonderful.
â Lee
Sep 23 '15 at 18:10
This is the most convenient way in all current answers. Could u plz give a little explanation on the working mechanism?
â Lee
Sep 24 '15 at 8:43
3
~/.inputrc
or/etc/inputrc
or the file referenced by the environment variable$INPUTRC
is the line editing configuration file of all application using readline, including bash (your shell). It has certain built-in line editing capabilities, including these two: in bash's manual page search for one of these keywords to find out more.e[5~
/e[6~
are the escape sequences generated by the Page Up/Down keys in most of the terminal emulators; to see it, launch thecat
command and then press these keys (the escape byte is represented in the terminal as^[
, in inputrc ase
).
â egmont
Sep 24 '15 at 8:55
The example in your original question says that the Up / Down keys work this way in Matlab. It would be interesting to see what happened if you placed the sequence of Up and Down in inputrc and assign to this action; would it work correctly, or have nasty side effects as it conflicts with their default behavior. I don't know, I'd be happy to hear your findings if you experiment with this.
â egmont
Sep 24 '15 at 8:58
1
I personally prefer overriding up and down arrow to do this:"e[A": history-search-backward
and"e[B": history-search-forward
â A.Wan
Jun 7 at 23:32
 |Â
show 3 more comments
up vote
21
down vote
Place these in your ~/.inputrc
:
"e[5~": history-search-backward
"e[6~": history-search-forward
These make Page Up and Page Down behave as you wish. (Some distributions have it already configured it for you.) I personally find these way more convenient than Ctrl+R or history
.
yeah, I tried. indeed wonderful.
â Lee
Sep 23 '15 at 18:10
This is the most convenient way in all current answers. Could u plz give a little explanation on the working mechanism?
â Lee
Sep 24 '15 at 8:43
3
~/.inputrc
or/etc/inputrc
or the file referenced by the environment variable$INPUTRC
is the line editing configuration file of all application using readline, including bash (your shell). It has certain built-in line editing capabilities, including these two: in bash's manual page search for one of these keywords to find out more.e[5~
/e[6~
are the escape sequences generated by the Page Up/Down keys in most of the terminal emulators; to see it, launch thecat
command and then press these keys (the escape byte is represented in the terminal as^[
, in inputrc ase
).
â egmont
Sep 24 '15 at 8:55
The example in your original question says that the Up / Down keys work this way in Matlab. It would be interesting to see what happened if you placed the sequence of Up and Down in inputrc and assign to this action; would it work correctly, or have nasty side effects as it conflicts with their default behavior. I don't know, I'd be happy to hear your findings if you experiment with this.
â egmont
Sep 24 '15 at 8:58
1
I personally prefer overriding up and down arrow to do this:"e[A": history-search-backward
and"e[B": history-search-forward
â A.Wan
Jun 7 at 23:32
 |Â
show 3 more comments
up vote
21
down vote
up vote
21
down vote
Place these in your ~/.inputrc
:
"e[5~": history-search-backward
"e[6~": history-search-forward
These make Page Up and Page Down behave as you wish. (Some distributions have it already configured it for you.) I personally find these way more convenient than Ctrl+R or history
.
Place these in your ~/.inputrc
:
"e[5~": history-search-backward
"e[6~": history-search-forward
These make Page Up and Page Down behave as you wish. (Some distributions have it already configured it for you.) I personally find these way more convenient than Ctrl+R or history
.
edited Oct 27 '17 at 18:58
answered Sep 23 '15 at 17:59
egmont
2,4041712
2,4041712
yeah, I tried. indeed wonderful.
â Lee
Sep 23 '15 at 18:10
This is the most convenient way in all current answers. Could u plz give a little explanation on the working mechanism?
â Lee
Sep 24 '15 at 8:43
3
~/.inputrc
or/etc/inputrc
or the file referenced by the environment variable$INPUTRC
is the line editing configuration file of all application using readline, including bash (your shell). It has certain built-in line editing capabilities, including these two: in bash's manual page search for one of these keywords to find out more.e[5~
/e[6~
are the escape sequences generated by the Page Up/Down keys in most of the terminal emulators; to see it, launch thecat
command and then press these keys (the escape byte is represented in the terminal as^[
, in inputrc ase
).
â egmont
Sep 24 '15 at 8:55
The example in your original question says that the Up / Down keys work this way in Matlab. It would be interesting to see what happened if you placed the sequence of Up and Down in inputrc and assign to this action; would it work correctly, or have nasty side effects as it conflicts with their default behavior. I don't know, I'd be happy to hear your findings if you experiment with this.
â egmont
Sep 24 '15 at 8:58
1
I personally prefer overriding up and down arrow to do this:"e[A": history-search-backward
and"e[B": history-search-forward
â A.Wan
Jun 7 at 23:32
 |Â
show 3 more comments
yeah, I tried. indeed wonderful.
â Lee
Sep 23 '15 at 18:10
This is the most convenient way in all current answers. Could u plz give a little explanation on the working mechanism?
â Lee
Sep 24 '15 at 8:43
3
~/.inputrc
or/etc/inputrc
or the file referenced by the environment variable$INPUTRC
is the line editing configuration file of all application using readline, including bash (your shell). It has certain built-in line editing capabilities, including these two: in bash's manual page search for one of these keywords to find out more.e[5~
/e[6~
are the escape sequences generated by the Page Up/Down keys in most of the terminal emulators; to see it, launch thecat
command and then press these keys (the escape byte is represented in the terminal as^[
, in inputrc ase
).
â egmont
Sep 24 '15 at 8:55
The example in your original question says that the Up / Down keys work this way in Matlab. It would be interesting to see what happened if you placed the sequence of Up and Down in inputrc and assign to this action; would it work correctly, or have nasty side effects as it conflicts with their default behavior. I don't know, I'd be happy to hear your findings if you experiment with this.
â egmont
Sep 24 '15 at 8:58
1
I personally prefer overriding up and down arrow to do this:"e[A": history-search-backward
and"e[B": history-search-forward
â A.Wan
Jun 7 at 23:32
yeah, I tried. indeed wonderful.
â Lee
Sep 23 '15 at 18:10
yeah, I tried. indeed wonderful.
â Lee
Sep 23 '15 at 18:10
This is the most convenient way in all current answers. Could u plz give a little explanation on the working mechanism?
â Lee
Sep 24 '15 at 8:43
This is the most convenient way in all current answers. Could u plz give a little explanation on the working mechanism?
â Lee
Sep 24 '15 at 8:43
3
3
~/.inputrc
or /etc/inputrc
or the file referenced by the environment variable $INPUTRC
is the line editing configuration file of all application using readline, including bash (your shell). It has certain built-in line editing capabilities, including these two: in bash's manual page search for one of these keywords to find out more. e[5~
/e[6~
are the escape sequences generated by the Page Up/Down keys in most of the terminal emulators; to see it, launch the cat
command and then press these keys (the escape byte is represented in the terminal as ^[
, in inputrc as e
).â egmont
Sep 24 '15 at 8:55
~/.inputrc
or /etc/inputrc
or the file referenced by the environment variable $INPUTRC
is the line editing configuration file of all application using readline, including bash (your shell). It has certain built-in line editing capabilities, including these two: in bash's manual page search for one of these keywords to find out more. e[5~
/e[6~
are the escape sequences generated by the Page Up/Down keys in most of the terminal emulators; to see it, launch the cat
command and then press these keys (the escape byte is represented in the terminal as ^[
, in inputrc as e
).â egmont
Sep 24 '15 at 8:55
The example in your original question says that the Up / Down keys work this way in Matlab. It would be interesting to see what happened if you placed the sequence of Up and Down in inputrc and assign to this action; would it work correctly, or have nasty side effects as it conflicts with their default behavior. I don't know, I'd be happy to hear your findings if you experiment with this.
â egmont
Sep 24 '15 at 8:58
The example in your original question says that the Up / Down keys work this way in Matlab. It would be interesting to see what happened if you placed the sequence of Up and Down in inputrc and assign to this action; would it work correctly, or have nasty side effects as it conflicts with their default behavior. I don't know, I'd be happy to hear your findings if you experiment with this.
â egmont
Sep 24 '15 at 8:58
1
1
I personally prefer overriding up and down arrow to do this:
"e[A": history-search-backward
and "e[B": history-search-forward
â A.Wan
Jun 7 at 23:32
I personally prefer overriding up and down arrow to do this:
"e[A": history-search-backward
and "e[B": history-search-forward
â A.Wan
Jun 7 at 23:32
 |Â
show 3 more comments
up vote
7
down vote
Besides ^r / ^s history i-search:
alt. inserts the last "word" of the previous command at the cursor. Repeat it to get the last word from older commands. (But note that a trailing &
counts as the last word for background commands).
This is super handy for mkdir foo
, cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word), cd
.
To get the 2nd-to-last word, use esc
-
2
alt+.
(i.e. use an emacs-style numeric argument to alt+.
. Negative counts in from the end, positive counts forward from the start.) But this is usually more trouble than it's worth; at some point it's faster to reach for the mouse and copy/paste, or up-arrow and ^w / ^y part of it (see below).
If your terminal is set up nicely/properly, ctrl-left and ctrl-right will go backward/forward by words. If not, hopefully at least alt-b and alt-f will do the same thing.
ctrl-/ is an undo. You can use auto-repeat for deleting words much more efficiently if you can undo when you overshoot by a bit.
More powerful mixing/matching of commands comes from using the kill-ring, which works just like in Emacs. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text.
Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.
Combining all of these together, you can
- start typing a command
- ctrl-r to go back to an old command and grab part of it with control-w or similar.
- esc-r or
alt+r
to restore it to how it was before you deleted part of it. - alt-
>
to go to the end of history (i.e. down-arrow all the way), to get back to the command you were in the middle of.
Other interactive-use tips:
Enable shopt -s globstar
, so you can do **/*.c
(recursive including the current dir). Sometimes handy for interactive use, but usually find -name '*.c' -exec foo +
is better.
If you write bash scripts, you'll find it handy to have shopt -s extglob
enabled in your interactive shells, too. You will sometimes find a use for stuff like *.!(c|h)
to match files that don't end with .c or .h.
Find aliases you like for ls -l
, less
, and anything else you do a lot. (cp -i
, mv -i
, and rm -I
are nice. Don't get in the habit of depending on them to do a selective rm. GNU rm's -I asks once for all the args.)
I like alias m=less
(m for "more"). I have less
set up with , and . bound to previous / next file (lesskey
). The default is a multi-keypress sequence that can't be used with autorepeat.
I like to do everything inside GNU screen. I find it easier to keep track of numbered screen-windows than a boatload of tabs in Konsole (or any other terminal emulator I've tried). If you don't already know screen
, learn tmux because it's newer and less crufty.
To get something like the functionality of opening a new shell with the same cwd as another shell, I use a custom hook for cd/pushd/popd that lets me do cds 8
to cd to whatever dir my shell in screen window 8 is using. This works even for shells outside of the screen session, as long as there's only one screen session.
Mine doesn't seem to be "properly configured". A lot of the shortcuts I see here don't work, I am using stock Terminal from Ubuntu 14.04. How can I configure it?
â JorgeeFG
Sep 24 '15 at 12:47
@JorgeeFG: I think it worked for me out of the box withgnome-terminal
in Ubuntu 14.04, before I switched to KDE. The relevant option is in your terminal settings, esc-prefix vs high-bit, and maybe some other settings. Some of them don't work perfectly for me through ssh. (e.g. ctrl left/right munge the escape-code for the arrow key, and I get5D
appearing literally)
â Peter Cordes
Sep 24 '15 at 17:38
add a comment |Â
up vote
7
down vote
Besides ^r / ^s history i-search:
alt. inserts the last "word" of the previous command at the cursor. Repeat it to get the last word from older commands. (But note that a trailing &
counts as the last word for background commands).
This is super handy for mkdir foo
, cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word), cd
.
To get the 2nd-to-last word, use esc
-
2
alt+.
(i.e. use an emacs-style numeric argument to alt+.
. Negative counts in from the end, positive counts forward from the start.) But this is usually more trouble than it's worth; at some point it's faster to reach for the mouse and copy/paste, or up-arrow and ^w / ^y part of it (see below).
If your terminal is set up nicely/properly, ctrl-left and ctrl-right will go backward/forward by words. If not, hopefully at least alt-b and alt-f will do the same thing.
ctrl-/ is an undo. You can use auto-repeat for deleting words much more efficiently if you can undo when you overshoot by a bit.
More powerful mixing/matching of commands comes from using the kill-ring, which works just like in Emacs. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text.
Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.
Combining all of these together, you can
- start typing a command
- ctrl-r to go back to an old command and grab part of it with control-w or similar.
- esc-r or
alt+r
to restore it to how it was before you deleted part of it. - alt-
>
to go to the end of history (i.e. down-arrow all the way), to get back to the command you were in the middle of.
Other interactive-use tips:
Enable shopt -s globstar
, so you can do **/*.c
(recursive including the current dir). Sometimes handy for interactive use, but usually find -name '*.c' -exec foo +
is better.
If you write bash scripts, you'll find it handy to have shopt -s extglob
enabled in your interactive shells, too. You will sometimes find a use for stuff like *.!(c|h)
to match files that don't end with .c or .h.
Find aliases you like for ls -l
, less
, and anything else you do a lot. (cp -i
, mv -i
, and rm -I
are nice. Don't get in the habit of depending on them to do a selective rm. GNU rm's -I asks once for all the args.)
I like alias m=less
(m for "more"). I have less
set up with , and . bound to previous / next file (lesskey
). The default is a multi-keypress sequence that can't be used with autorepeat.
I like to do everything inside GNU screen. I find it easier to keep track of numbered screen-windows than a boatload of tabs in Konsole (or any other terminal emulator I've tried). If you don't already know screen
, learn tmux because it's newer and less crufty.
To get something like the functionality of opening a new shell with the same cwd as another shell, I use a custom hook for cd/pushd/popd that lets me do cds 8
to cd to whatever dir my shell in screen window 8 is using. This works even for shells outside of the screen session, as long as there's only one screen session.
Mine doesn't seem to be "properly configured". A lot of the shortcuts I see here don't work, I am using stock Terminal from Ubuntu 14.04. How can I configure it?
â JorgeeFG
Sep 24 '15 at 12:47
@JorgeeFG: I think it worked for me out of the box withgnome-terminal
in Ubuntu 14.04, before I switched to KDE. The relevant option is in your terminal settings, esc-prefix vs high-bit, and maybe some other settings. Some of them don't work perfectly for me through ssh. (e.g. ctrl left/right munge the escape-code for the arrow key, and I get5D
appearing literally)
â Peter Cordes
Sep 24 '15 at 17:38
add a comment |Â
up vote
7
down vote
up vote
7
down vote
Besides ^r / ^s history i-search:
alt. inserts the last "word" of the previous command at the cursor. Repeat it to get the last word from older commands. (But note that a trailing &
counts as the last word for background commands).
This is super handy for mkdir foo
, cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word), cd
.
To get the 2nd-to-last word, use esc
-
2
alt+.
(i.e. use an emacs-style numeric argument to alt+.
. Negative counts in from the end, positive counts forward from the start.) But this is usually more trouble than it's worth; at some point it's faster to reach for the mouse and copy/paste, or up-arrow and ^w / ^y part of it (see below).
If your terminal is set up nicely/properly, ctrl-left and ctrl-right will go backward/forward by words. If not, hopefully at least alt-b and alt-f will do the same thing.
ctrl-/ is an undo. You can use auto-repeat for deleting words much more efficiently if you can undo when you overshoot by a bit.
More powerful mixing/matching of commands comes from using the kill-ring, which works just like in Emacs. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text.
Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.
Combining all of these together, you can
- start typing a command
- ctrl-r to go back to an old command and grab part of it with control-w or similar.
- esc-r or
alt+r
to restore it to how it was before you deleted part of it. - alt-
>
to go to the end of history (i.e. down-arrow all the way), to get back to the command you were in the middle of.
Other interactive-use tips:
Enable shopt -s globstar
, so you can do **/*.c
(recursive including the current dir). Sometimes handy for interactive use, but usually find -name '*.c' -exec foo +
is better.
If you write bash scripts, you'll find it handy to have shopt -s extglob
enabled in your interactive shells, too. You will sometimes find a use for stuff like *.!(c|h)
to match files that don't end with .c or .h.
Find aliases you like for ls -l
, less
, and anything else you do a lot. (cp -i
, mv -i
, and rm -I
are nice. Don't get in the habit of depending on them to do a selective rm. GNU rm's -I asks once for all the args.)
I like alias m=less
(m for "more"). I have less
set up with , and . bound to previous / next file (lesskey
). The default is a multi-keypress sequence that can't be used with autorepeat.
I like to do everything inside GNU screen. I find it easier to keep track of numbered screen-windows than a boatload of tabs in Konsole (or any other terminal emulator I've tried). If you don't already know screen
, learn tmux because it's newer and less crufty.
To get something like the functionality of opening a new shell with the same cwd as another shell, I use a custom hook for cd/pushd/popd that lets me do cds 8
to cd to whatever dir my shell in screen window 8 is using. This works even for shells outside of the screen session, as long as there's only one screen session.
Besides ^r / ^s history i-search:
alt. inserts the last "word" of the previous command at the cursor. Repeat it to get the last word from older commands. (But note that a trailing &
counts as the last word for background commands).
This is super handy for mkdir foo
, cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word), cd
.
To get the 2nd-to-last word, use esc
-
2
alt+.
(i.e. use an emacs-style numeric argument to alt+.
. Negative counts in from the end, positive counts forward from the start.) But this is usually more trouble than it's worth; at some point it's faster to reach for the mouse and copy/paste, or up-arrow and ^w / ^y part of it (see below).
If your terminal is set up nicely/properly, ctrl-left and ctrl-right will go backward/forward by words. If not, hopefully at least alt-b and alt-f will do the same thing.
ctrl-/ is an undo. You can use auto-repeat for deleting words much more efficiently if you can undo when you overshoot by a bit.
More powerful mixing/matching of commands comes from using the kill-ring, which works just like in Emacs. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text.
Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.
Combining all of these together, you can
- start typing a command
- ctrl-r to go back to an old command and grab part of it with control-w or similar.
- esc-r or
alt+r
to restore it to how it was before you deleted part of it. - alt-
>
to go to the end of history (i.e. down-arrow all the way), to get back to the command you were in the middle of.
Other interactive-use tips:
Enable shopt -s globstar
, so you can do **/*.c
(recursive including the current dir). Sometimes handy for interactive use, but usually find -name '*.c' -exec foo +
is better.
If you write bash scripts, you'll find it handy to have shopt -s extglob
enabled in your interactive shells, too. You will sometimes find a use for stuff like *.!(c|h)
to match files that don't end with .c or .h.
Find aliases you like for ls -l
, less
, and anything else you do a lot. (cp -i
, mv -i
, and rm -I
are nice. Don't get in the habit of depending on them to do a selective rm. GNU rm's -I asks once for all the args.)
I like alias m=less
(m for "more"). I have less
set up with , and . bound to previous / next file (lesskey
). The default is a multi-keypress sequence that can't be used with autorepeat.
I like to do everything inside GNU screen. I find it easier to keep track of numbered screen-windows than a boatload of tabs in Konsole (or any other terminal emulator I've tried). If you don't already know screen
, learn tmux because it's newer and less crufty.
To get something like the functionality of opening a new shell with the same cwd as another shell, I use a custom hook for cd/pushd/popd that lets me do cds 8
to cd to whatever dir my shell in screen window 8 is using. This works even for shells outside of the screen session, as long as there's only one screen session.
edited Feb 27 at 2:56
answered Sep 23 '15 at 21:44
Peter Cordes
4,1331132
4,1331132
Mine doesn't seem to be "properly configured". A lot of the shortcuts I see here don't work, I am using stock Terminal from Ubuntu 14.04. How can I configure it?
â JorgeeFG
Sep 24 '15 at 12:47
@JorgeeFG: I think it worked for me out of the box withgnome-terminal
in Ubuntu 14.04, before I switched to KDE. The relevant option is in your terminal settings, esc-prefix vs high-bit, and maybe some other settings. Some of them don't work perfectly for me through ssh. (e.g. ctrl left/right munge the escape-code for the arrow key, and I get5D
appearing literally)
â Peter Cordes
Sep 24 '15 at 17:38
add a comment |Â
Mine doesn't seem to be "properly configured". A lot of the shortcuts I see here don't work, I am using stock Terminal from Ubuntu 14.04. How can I configure it?
â JorgeeFG
Sep 24 '15 at 12:47
@JorgeeFG: I think it worked for me out of the box withgnome-terminal
in Ubuntu 14.04, before I switched to KDE. The relevant option is in your terminal settings, esc-prefix vs high-bit, and maybe some other settings. Some of them don't work perfectly for me through ssh. (e.g. ctrl left/right munge the escape-code for the arrow key, and I get5D
appearing literally)
â Peter Cordes
Sep 24 '15 at 17:38
Mine doesn't seem to be "properly configured". A lot of the shortcuts I see here don't work, I am using stock Terminal from Ubuntu 14.04. How can I configure it?
â JorgeeFG
Sep 24 '15 at 12:47
Mine doesn't seem to be "properly configured". A lot of the shortcuts I see here don't work, I am using stock Terminal from Ubuntu 14.04. How can I configure it?
â JorgeeFG
Sep 24 '15 at 12:47
@JorgeeFG: I think it worked for me out of the box with
gnome-terminal
in Ubuntu 14.04, before I switched to KDE. The relevant option is in your terminal settings, esc-prefix vs high-bit, and maybe some other settings. Some of them don't work perfectly for me through ssh. (e.g. ctrl left/right munge the escape-code for the arrow key, and I get 5D
appearing literally)â Peter Cordes
Sep 24 '15 at 17:38
@JorgeeFG: I think it worked for me out of the box with
gnome-terminal
in Ubuntu 14.04, before I switched to KDE. The relevant option is in your terminal settings, esc-prefix vs high-bit, and maybe some other settings. Some of them don't work perfectly for me through ssh. (e.g. ctrl left/right munge the escape-code for the arrow key, and I get 5D
appearing literally)â Peter Cordes
Sep 24 '15 at 17:38
add a comment |Â
up vote
4
down vote
I always tend to configure my machines with an large HISTSIZE value so it keeps a longer history list, as well as HISTTIMEFORMAT with the time stamp value so I can see when was the command ran.
export HISTSIZE=10000
export HISTTIMEFORMAT="%m/%d/%Y %T "
add a comment |Â
up vote
4
down vote
I always tend to configure my machines with an large HISTSIZE value so it keeps a longer history list, as well as HISTTIMEFORMAT with the time stamp value so I can see when was the command ran.
export HISTSIZE=10000
export HISTTIMEFORMAT="%m/%d/%Y %T "
add a comment |Â
up vote
4
down vote
up vote
4
down vote
I always tend to configure my machines with an large HISTSIZE value so it keeps a longer history list, as well as HISTTIMEFORMAT with the time stamp value so I can see when was the command ran.
export HISTSIZE=10000
export HISTTIMEFORMAT="%m/%d/%Y %T "
I always tend to configure my machines with an large HISTSIZE value so it keeps a longer history list, as well as HISTTIMEFORMAT with the time stamp value so I can see when was the command ran.
export HISTSIZE=10000
export HISTTIMEFORMAT="%m/%d/%Y %T "
answered Sep 23 '15 at 19:51
Alpha01
1565
1565
add a comment |Â
add a comment |Â
up vote
3
down vote
Add the following lines to your ~/.inputrc
"e[A": history-search-backward
"e[B": history-search-forward
This is similar to egmont's answer, but instead of using Page Up and Page Down, it uses the escape sequences for Arrow Up and Arrow Down. This way is much more convenient to use on a Mac.
add a comment |Â
up vote
3
down vote
Add the following lines to your ~/.inputrc
"e[A": history-search-backward
"e[B": history-search-forward
This is similar to egmont's answer, but instead of using Page Up and Page Down, it uses the escape sequences for Arrow Up and Arrow Down. This way is much more convenient to use on a Mac.
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Add the following lines to your ~/.inputrc
"e[A": history-search-backward
"e[B": history-search-forward
This is similar to egmont's answer, but instead of using Page Up and Page Down, it uses the escape sequences for Arrow Up and Arrow Down. This way is much more convenient to use on a Mac.
Add the following lines to your ~/.inputrc
"e[A": history-search-backward
"e[B": history-search-forward
This is similar to egmont's answer, but instead of using Page Up and Page Down, it uses the escape sequences for Arrow Up and Arrow Down. This way is much more convenient to use on a Mac.
answered Nov 3 '17 at 18:57
John Slavick
1312
1312
add a comment |Â
add a comment |Â
up vote
2
down vote
The means I usually use is to combine the history
command with grep
IE:
history | grep <command im looking for>
That will display a numbered history of commands you have typed that contain that command, you can then use:
!<number listed in history command>
to redo that command.
IE:
history | grep history
142 history
143 history | grep lvresize
568 history | grep echo
570 history | grep echo
571 history | grep history
followed by:!571
will repeat history | grep history
1
C-R = much better + much safer
â PSkocik
Sep 23 '15 at 17:16
Perhaps, Personally I find a history piped to grep to be a faster means of finding most commands. As far as Safer, I'm not sure what you mean, what are the inherent risks in using history as apposed to searching through it with c-R?
â Gravy
Sep 23 '15 at 17:25
2
@Gravy I suspect PSkocik means the!
history substitution is less safe.
â derobert
Sep 23 '15 at 17:27
3
@Gravy The inherent risks would be accidentally getting the wrong line. No idea why your terminal has frozen, unless you used Control-S and you have xon/xoff flow control enabled (often the default). If so, Control-Q will bring it back.
â derobert
Sep 23 '15 at 17:36
1
I agree with @PSkocik, c-r is much better because it's easier to edit the history line, rather than just run it again the same way. Running the exact-same command again is a trivial case of what you can do to save time re-typing variations on complex one-liners, or tab-completing a different file in a long path. See my answer.
â Peter Cordes
Sep 23 '15 at 22:06
 |Â
show 3 more comments
up vote
2
down vote
The means I usually use is to combine the history
command with grep
IE:
history | grep <command im looking for>
That will display a numbered history of commands you have typed that contain that command, you can then use:
!<number listed in history command>
to redo that command.
IE:
history | grep history
142 history
143 history | grep lvresize
568 history | grep echo
570 history | grep echo
571 history | grep history
followed by:!571
will repeat history | grep history
1
C-R = much better + much safer
â PSkocik
Sep 23 '15 at 17:16
Perhaps, Personally I find a history piped to grep to be a faster means of finding most commands. As far as Safer, I'm not sure what you mean, what are the inherent risks in using history as apposed to searching through it with c-R?
â Gravy
Sep 23 '15 at 17:25
2
@Gravy I suspect PSkocik means the!
history substitution is less safe.
â derobert
Sep 23 '15 at 17:27
3
@Gravy The inherent risks would be accidentally getting the wrong line. No idea why your terminal has frozen, unless you used Control-S and you have xon/xoff flow control enabled (often the default). If so, Control-Q will bring it back.
â derobert
Sep 23 '15 at 17:36
1
I agree with @PSkocik, c-r is much better because it's easier to edit the history line, rather than just run it again the same way. Running the exact-same command again is a trivial case of what you can do to save time re-typing variations on complex one-liners, or tab-completing a different file in a long path. See my answer.
â Peter Cordes
Sep 23 '15 at 22:06
 |Â
show 3 more comments
up vote
2
down vote
up vote
2
down vote
The means I usually use is to combine the history
command with grep
IE:
history | grep <command im looking for>
That will display a numbered history of commands you have typed that contain that command, you can then use:
!<number listed in history command>
to redo that command.
IE:
history | grep history
142 history
143 history | grep lvresize
568 history | grep echo
570 history | grep echo
571 history | grep history
followed by:!571
will repeat history | grep history
The means I usually use is to combine the history
command with grep
IE:
history | grep <command im looking for>
That will display a numbered history of commands you have typed that contain that command, you can then use:
!<number listed in history command>
to redo that command.
IE:
history | grep history
142 history
143 history | grep lvresize
568 history | grep echo
570 history | grep echo
571 history | grep history
followed by:!571
will repeat history | grep history
answered Sep 23 '15 at 17:05
Gravy
1,343520
1,343520
1
C-R = much better + much safer
â PSkocik
Sep 23 '15 at 17:16
Perhaps, Personally I find a history piped to grep to be a faster means of finding most commands. As far as Safer, I'm not sure what you mean, what are the inherent risks in using history as apposed to searching through it with c-R?
â Gravy
Sep 23 '15 at 17:25
2
@Gravy I suspect PSkocik means the!
history substitution is less safe.
â derobert
Sep 23 '15 at 17:27
3
@Gravy The inherent risks would be accidentally getting the wrong line. No idea why your terminal has frozen, unless you used Control-S and you have xon/xoff flow control enabled (often the default). If so, Control-Q will bring it back.
â derobert
Sep 23 '15 at 17:36
1
I agree with @PSkocik, c-r is much better because it's easier to edit the history line, rather than just run it again the same way. Running the exact-same command again is a trivial case of what you can do to save time re-typing variations on complex one-liners, or tab-completing a different file in a long path. See my answer.
â Peter Cordes
Sep 23 '15 at 22:06
 |Â
show 3 more comments
1
C-R = much better + much safer
â PSkocik
Sep 23 '15 at 17:16
Perhaps, Personally I find a history piped to grep to be a faster means of finding most commands. As far as Safer, I'm not sure what you mean, what are the inherent risks in using history as apposed to searching through it with c-R?
â Gravy
Sep 23 '15 at 17:25
2
@Gravy I suspect PSkocik means the!
history substitution is less safe.
â derobert
Sep 23 '15 at 17:27
3
@Gravy The inherent risks would be accidentally getting the wrong line. No idea why your terminal has frozen, unless you used Control-S and you have xon/xoff flow control enabled (often the default). If so, Control-Q will bring it back.
â derobert
Sep 23 '15 at 17:36
1
I agree with @PSkocik, c-r is much better because it's easier to edit the history line, rather than just run it again the same way. Running the exact-same command again is a trivial case of what you can do to save time re-typing variations on complex one-liners, or tab-completing a different file in a long path. See my answer.
â Peter Cordes
Sep 23 '15 at 22:06
1
1
C-R = much better + much safer
â PSkocik
Sep 23 '15 at 17:16
C-R = much better + much safer
â PSkocik
Sep 23 '15 at 17:16
Perhaps, Personally I find a history piped to grep to be a faster means of finding most commands. As far as Safer, I'm not sure what you mean, what are the inherent risks in using history as apposed to searching through it with c-R?
â Gravy
Sep 23 '15 at 17:25
Perhaps, Personally I find a history piped to grep to be a faster means of finding most commands. As far as Safer, I'm not sure what you mean, what are the inherent risks in using history as apposed to searching through it with c-R?
â Gravy
Sep 23 '15 at 17:25
2
2
@Gravy I suspect PSkocik means the
!
history substitution is less safe.â derobert
Sep 23 '15 at 17:27
@Gravy I suspect PSkocik means the
!
history substitution is less safe.â derobert
Sep 23 '15 at 17:27
3
3
@Gravy The inherent risks would be accidentally getting the wrong line. No idea why your terminal has frozen, unless you used Control-S and you have xon/xoff flow control enabled (often the default). If so, Control-Q will bring it back.
â derobert
Sep 23 '15 at 17:36
@Gravy The inherent risks would be accidentally getting the wrong line. No idea why your terminal has frozen, unless you used Control-S and you have xon/xoff flow control enabled (often the default). If so, Control-Q will bring it back.
â derobert
Sep 23 '15 at 17:36
1
1
I agree with @PSkocik, c-r is much better because it's easier to edit the history line, rather than just run it again the same way. Running the exact-same command again is a trivial case of what you can do to save time re-typing variations on complex one-liners, or tab-completing a different file in a long path. See my answer.
â Peter Cordes
Sep 23 '15 at 22:06
I agree with @PSkocik, c-r is much better because it's easier to edit the history line, rather than just run it again the same way. Running the exact-same command again is a trivial case of what you can do to save time re-typing variations on complex one-liners, or tab-completing a different file in a long path. See my answer.
â Peter Cordes
Sep 23 '15 at 22:06
 |Â
show 3 more comments
up vote
1
down vote
I used daily 4 tips (and some others) in my bash terminal:
Execute last command:
!!
Execute command N from history:
!N
Print last command starting with prefix from history:
!prefix:p
Execute last command starting with prefix from history:
!prefix
add a comment |Â
up vote
1
down vote
I used daily 4 tips (and some others) in my bash terminal:
Execute last command:
!!
Execute command N from history:
!N
Print last command starting with prefix from history:
!prefix:p
Execute last command starting with prefix from history:
!prefix
add a comment |Â
up vote
1
down vote
up vote
1
down vote
I used daily 4 tips (and some others) in my bash terminal:
Execute last command:
!!
Execute command N from history:
!N
Print last command starting with prefix from history:
!prefix:p
Execute last command starting with prefix from history:
!prefix
I used daily 4 tips (and some others) in my bash terminal:
Execute last command:
!!
Execute command N from history:
!N
Print last command starting with prefix from history:
!prefix:p
Execute last command starting with prefix from history:
!prefix
answered Sep 3 at 15:01
Nolwennig
1555
1555
add a comment |Â
add a comment |Â
up vote
1
down vote
Add the following alias to ~/.bashrc
alias h='history | awk '"'"'$1="";print'"'"' |sed "s/^ *//" |grep -v "^h " | sort | uniq | grep'
Usage: "h fire" will get me a list of commands contain fire. or "h ^fire" will get me a list of commands begin with fire.
This is useful when I want to view a series of commands at the same time.
By the way, '"'"' is ugly but I couldn't find other ways to avoid single quotes in single quotes.
fyi -h="history | awk '$1=""; print'
should work. alternatively create a function and call that.
â DarkHeart
Jun 17 '16 at 1:37
thank you DarkHeart, but I tried your code, found not worked in my environment, maybe there is something different in mine
â Hosi Golden
Jun 17 '16 at 3:09
add a comment |Â
up vote
1
down vote
Add the following alias to ~/.bashrc
alias h='history | awk '"'"'$1="";print'"'"' |sed "s/^ *//" |grep -v "^h " | sort | uniq | grep'
Usage: "h fire" will get me a list of commands contain fire. or "h ^fire" will get me a list of commands begin with fire.
This is useful when I want to view a series of commands at the same time.
By the way, '"'"' is ugly but I couldn't find other ways to avoid single quotes in single quotes.
fyi -h="history | awk '$1=""; print'
should work. alternatively create a function and call that.
â DarkHeart
Jun 17 '16 at 1:37
thank you DarkHeart, but I tried your code, found not worked in my environment, maybe there is something different in mine
â Hosi Golden
Jun 17 '16 at 3:09
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Add the following alias to ~/.bashrc
alias h='history | awk '"'"'$1="";print'"'"' |sed "s/^ *//" |grep -v "^h " | sort | uniq | grep'
Usage: "h fire" will get me a list of commands contain fire. or "h ^fire" will get me a list of commands begin with fire.
This is useful when I want to view a series of commands at the same time.
By the way, '"'"' is ugly but I couldn't find other ways to avoid single quotes in single quotes.
Add the following alias to ~/.bashrc
alias h='history | awk '"'"'$1="";print'"'"' |sed "s/^ *//" |grep -v "^h " | sort | uniq | grep'
Usage: "h fire" will get me a list of commands contain fire. or "h ^fire" will get me a list of commands begin with fire.
This is useful when I want to view a series of commands at the same time.
By the way, '"'"' is ugly but I couldn't find other ways to avoid single quotes in single quotes.
edited 11 mins ago
answered Jun 17 '16 at 1:03
Hosi Golden
113
113
fyi -h="history | awk '$1=""; print'
should work. alternatively create a function and call that.
â DarkHeart
Jun 17 '16 at 1:37
thank you DarkHeart, but I tried your code, found not worked in my environment, maybe there is something different in mine
â Hosi Golden
Jun 17 '16 at 3:09
add a comment |Â
fyi -h="history | awk '$1=""; print'
should work. alternatively create a function and call that.
â DarkHeart
Jun 17 '16 at 1:37
thank you DarkHeart, but I tried your code, found not worked in my environment, maybe there is something different in mine
â Hosi Golden
Jun 17 '16 at 3:09
fyi -
h="history | awk '$1=""; print'
should work. alternatively create a function and call that.â DarkHeart
Jun 17 '16 at 1:37
fyi -
h="history | awk '$1=""; print'
should work. alternatively create a function and call that.â DarkHeart
Jun 17 '16 at 1:37
thank you DarkHeart, but I tried your code, found not worked in my environment, maybe there is something different in mine
â Hosi Golden
Jun 17 '16 at 3:09
thank you DarkHeart, but I tried your code, found not worked in my environment, maybe there is something different in mine
â Hosi Golden
Jun 17 '16 at 3:09
add a comment |Â
up vote
0
down vote
If it is OK for you to use zsh
instead of bash
you cona use oh-my-zsh for that.
It contains the arrow up down navigation through your command history. It allows also, that same navigation when you have already typed text (as Mathlab).
edit: It also contains other developer-focused stuff, such as aliases and themes. That extras make the distro bigger and more complex to use.
https://github.com/robbyrussell/oh-my-zsh
Hope it helps
oh-my-zsh
contains tons of stuff. You might as well say: This exact feature is included in that distro, when all you need is a specific setting or app.
â muru
Sep 24 '15 at 4:17
I edited the post: I warned that it is bigger than a simple app.
â xyz
Sep 24 '15 at 4:44
add a comment |Â
up vote
0
down vote
If it is OK for you to use zsh
instead of bash
you cona use oh-my-zsh for that.
It contains the arrow up down navigation through your command history. It allows also, that same navigation when you have already typed text (as Mathlab).
edit: It also contains other developer-focused stuff, such as aliases and themes. That extras make the distro bigger and more complex to use.
https://github.com/robbyrussell/oh-my-zsh
Hope it helps
oh-my-zsh
contains tons of stuff. You might as well say: This exact feature is included in that distro, when all you need is a specific setting or app.
â muru
Sep 24 '15 at 4:17
I edited the post: I warned that it is bigger than a simple app.
â xyz
Sep 24 '15 at 4:44
add a comment |Â
up vote
0
down vote
up vote
0
down vote
If it is OK for you to use zsh
instead of bash
you cona use oh-my-zsh for that.
It contains the arrow up down navigation through your command history. It allows also, that same navigation when you have already typed text (as Mathlab).
edit: It also contains other developer-focused stuff, such as aliases and themes. That extras make the distro bigger and more complex to use.
https://github.com/robbyrussell/oh-my-zsh
Hope it helps
If it is OK for you to use zsh
instead of bash
you cona use oh-my-zsh for that.
It contains the arrow up down navigation through your command history. It allows also, that same navigation when you have already typed text (as Mathlab).
edit: It also contains other developer-focused stuff, such as aliases and themes. That extras make the distro bigger and more complex to use.
https://github.com/robbyrussell/oh-my-zsh
Hope it helps
edited Sep 24 '15 at 4:41
answered Sep 24 '15 at 3:44
xyz
1195
1195
oh-my-zsh
contains tons of stuff. You might as well say: This exact feature is included in that distro, when all you need is a specific setting or app.
â muru
Sep 24 '15 at 4:17
I edited the post: I warned that it is bigger than a simple app.
â xyz
Sep 24 '15 at 4:44
add a comment |Â
oh-my-zsh
contains tons of stuff. You might as well say: This exact feature is included in that distro, when all you need is a specific setting or app.
â muru
Sep 24 '15 at 4:17
I edited the post: I warned that it is bigger than a simple app.
â xyz
Sep 24 '15 at 4:44
oh-my-zsh
contains tons of stuff. You might as well say: This exact feature is included in that distro, when all you need is a specific setting or app.â muru
Sep 24 '15 at 4:17
oh-my-zsh
contains tons of stuff. You might as well say: This exact feature is included in that distro, when all you need is a specific setting or app.â muru
Sep 24 '15 at 4:17
I edited the post: I warned that it is bigger than a simple app.
â xyz
Sep 24 '15 at 4:44
I edited the post: I warned that it is bigger than a simple app.
â xyz
Sep 24 '15 at 4:44
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f231605%2fsearch-for-a-previous-command-with-the-prefix-i-just-typed%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Besides ^r / ^s history i-search,
alt .
inserts the last "word" of previous commands at the cursor. This is super handy formkdir foo
,cd
alt-dot. Even faster than up-arrow, ^a, alt-d (delete forward word),cd
. More powerful mixing/matching of commands comes from using the kill-ring. ctrl-y to paste the last ctrl-w / ctrl-u / ctrl-backspace / alt-d. alt-y to cycle through older killed text. Multiple ctrl-w or whatever in a row make on kill-ring entry. Use left and right arrow or something to break up the entry if you want to remove two things and only paste one later.â Peter Cordes
Sep 23 '15 at 21:38
converted to an answer.
â Peter Cordes
Sep 23 '15 at 21:44
You could simply use a shell that provides such a feature, like
fish
;)â yoann
Sep 23 '15 at 23:13
@yoann you're assuming bash doesn't. It does, via readline, just not accessible by default.
â muru
Sep 24 '15 at 8:35
1
@yoann check out unix.stackexchange.com/a/76591/70524. Instead of
e[5~
ande[6~
, usee[A
,e[B
.â muru
Sep 24 '15 at 13:02