How to search backwards in emacs shell? - shell

I love emacs shells, but there is a confusing thing for people used to use the gnome shell (like me). The backwards search does not work the same way. C-r searches in the buffer, not in the command history, and M-r searches in the command history but when pressed twice toggles between direct and regexp search, i.e. does not search for the previous match in the command history. So, how to do that?
Edit: The shell I was looking when I wrote this is what you get with M-x shell, I didn't know there were other shells in emacs. See e.g. https://plus.google.com/112537550357635435516/posts/Wgpk2mH6hQh

Type M-r , search for something, and then press C-r to repeat the same search in the command history.

Press M-r, then write something, then press C-r as many times as needed.

Related

LLDB command history search by starting characters similar to GDB readline?

Is there a way to search LLDB commands similar to the readline library's history-search-backward in bash and GDB?
I want to be able to type some characters, and use up arrow to cycle through all commands that start with those characters.
lldb uses editline rather than readline. Editline has a backward search in command history feature (em-inc-search-prev) that's bound to ^R by default (though you can change this in your .editrc) It works like emac's ^S, you type characters to refine the search, and ^R to go to the previous match. To reverse direction and start searching forward you would use em-inc-search-next, but we haven't bound that to anything.

Can I press enter twice to execute a command?

I'm using ZSH and I'm wondering if it's possible to map [enter, enter] to execute a command. Specifically, I'd like to move to my home directory when I press enter twice without typing any other text.
I don't want to make someone go through the trouble of writing me a script, but if any of y'all could point me in the right direction (zsh script/applescript/whatever it should be) and tell me if this is possible I'd really appreciate it!
I use iTerm2 on OSX, and zsh is my primary shell. Let me know if you need any more information!
Have a look at preexec in the section SPECIAL FUNCTIONS of the zsh manpage. If you define a function by this name, for instance in your .zshrc, and you have the history mechanism enabled (as is common in interactive shells), this function receives as argument the command line you have entered. If you just typed enter, the command will be the null string. You can catch this and then do whatever you want - for instance doing a chdir.

Zsh tab completion: Is there any plugin or feature that can allow for a MRU tab-completion scheme?

I find myself doing stuff like typing the last part of a command name because the beginning of it gets completed with other commands. Of course most other shells can't even complete stuff in this way yet.
Anyway, as an example, I've got a script in my $PATH called git-verbose-status or something like that. Along with many others prefixed with git-. It then becomes increasingly difficult to differentiate between them as the prefix-namespace is populated. It really makes sense to think "git" because I named my program that for a reason.
So it got me thinking. A most-recently-used ordering can be used on the string already-typed in order to perform matching. This should be strictly more helpful than doing completion alphabetically as I believe it is normally done.
It could just be a simple reverse scan on command history.
I think the history substring search does exactly what you want; I have setup the following keybindings
bindkey '^[[A' up-line-or-search
bindkey '^[[B' down-line-or-search
bindkey '^P' up-line-or-search
bindkey '^N' down-line-or-search
That way I start typing any part of a previous command, press the up-arrow or Ctrl-P and get commands matching that written part anywhere from my history in reverse order - as you described.
Edit: And of course, if you don't want to install the history-search plugin, C-r does also match complete command lines in your history.

up-arrow key to search for 2 words given with zsh shell

With zsh/oh-my-zsh/iterm2, excellent way to navigate through the terminal command history.
If I issued a command say knife cookbook upload application few days before and to get the command, I can do k or kni or knif and press the up-arrow key, it'll iterate through the commands history that starts with knife word.
But if the similar commands have ran that starts out with knife, I'll have to iterate over the commands using up-arrow key.
But if I wanted to search through more than one word, say to get commands with both knife node, zsh doesn't support this and starts to show the commands that starts with just knife.
So, is there any way to get the commands that starts with two words? so that I just type two words and pressing up-arrow key to show only those commands that starts with two words typed.
In addition to #Shep answer I would suggest changing <C-r> behavior from searching just only plain strings to searching glob patterns:
bindkey "\C-r" history-incremental-pattern-search-backward
. In this case you will achieve desired result by searching for knife*node. By the way, if you did knife node<Up>, zsh should search for commands that start with knife node, not just with knife. But it won’t search for commands that contain both knife and node separated by other words, as well as it won’t search for commands that contain knife node not at the start of the line (talking about history-beginning-search-backward widget).
Maybe not exactly what you're looking for, but typing C-r will open a backward command search, which will search for any string you entered, starting with the most recent. To get the second most recent, press control-r again (and so on).
Also see a related thread on superuser.

How to quickly scroll to the latest / end of command history in bash?

Lots of times I'll use Ctrl-R for reverse search and mistype some letter. Bash jumps up hundreds of lines and I'm in the middle of commands I was using a week ago.
Is there a shortcut for jumping back down to the lastest commands I had typed?
Edit: after testing it out on a CentOS server and Mac OS X, it looks like this only happening on OS X.
I've struggled with this same issue.
You can solve this by aborting with ctrl-c. Whether you're in the middle of a reverse search or scrolling through history with the arrows, aborting returns you to a prompt with the history scroll just after the last command.
UPDATE
Here's a nice trick I just learned. Bash and many other programs use Readline under the hood for command-line interpretation. Key bindings for Readline can be configured in a .inputrc file or with the bind command. The bindings can make use of a few functions provided by Readline. For example, I use Bash in vi mode but I still like to use Emacs-style ctrl-A so I have this line in my .bashrc file:
bind '\C-a:beginning-of-line'
To list all the available Readline functions:
bind -l
Among the functions is end-of-history. The function does like its name suggests. The difference between this approach and just using the abort command is that this keeps you on the same prompt.
If using libreadline, Alt-> (or Meta->). More info on Readline shortcuts or search for Commands for Manipulating the History in the man page.
On Mac, try command + .
It works for me.
I was trying alt+. and alt+shift+. , neither works for me. And then found command + . actually works
Maybe not exactly what you want, but you can fix your mistyped character(s) by using backspace when you're in the CTRL-r (reverse-i-search) mode.
You may wan to try "suggest box"-like history HSTR. It reads the bash history and allows quick navigation and filtering - you can see the context of similar history entries. Once you select a history entry it can be edited on the command line.
In Zsh with emacs binding set the actual default key sequence is ^[> binded to end-of-buffer-or-history command rather than command-. suggested above (or end-of-history depending on effect you want to achieve)
Cmd-. produces in Apple Terminal the similar or the same key sequence as Ctrl-C, which can be confirmed by running something useless and long, e.g. find . >/dev/null 2>&1 and pressing one and then other keys on keyboard.
Ctrl-C forces input to be ended and reset. and history scroll is just a side effect for it.

Resources