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?

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 CordesSep 23 '15 at 21:38

This is the most convenient way in all current answers. Could u plz give a little explanation on the working mechanism?
– LeeSep 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 the cat command and then press these keys (the escape byte is represented in the terminal as ^[, in inputrc as \e).
– egmontSep 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.
– egmontSep 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.WanJun 7 '18 at 23:32

To get the 2nd-to-last word, use esc-2alt+. (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.

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?
– JorgeeFGSep 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 CordesSep 24 '15 at 17:38

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?
– GravySep 23 '15 at 17:25

2

@Gravy I suspect PSkocik means the ! history substitution is less safe.
– derobertSep 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.
– derobertSep 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 CordesSep 23 '15 at 22:06