Filtering ZSH history by command - filtering

I was told that in ZSH you could do something like command and then when you hit up it would filter the history based on the given command. But when I try this it just cycles the history like bash does. Is this disabled by default?

Hit Ctrl+R, type some letters, it will find the previous command with these letters, keep hitting Ctrl+R to continue through the previous findings.
Works in bash, zsh (and other shells i suppose).
What i personally like to have is: type some letters, press Up, the previous commands starting with the same letters appear. Very powerful, i love it.
You have to bind the keys you want to history-beginning-search-backward and history-beginning-search-forward.
In case it's not enough for you, zsh has a lot of options, try to look in Zsh Line Editor and tell us.
For bash, less powerful but more common, Bash commands for history.

Use exclamation point:
> !<starts-with this string>
You can arrow up/down through all commands that started with that. I use "!v" all the time to get my previous command for opening a file with Vim.
You can also use a question mark to search beyond matching the beginning of the string,
> !?status
Can find "git status".

Use percol to dynamically search and navigate through your history with Ctrl-r.
install percol: sudo pip install percol
add the zsh-history-search code snippet to your .zshrc file.
After a Ctrl-r, you can see your whole history in the same window. Searching for a keyword (dynamically) narrows that list down. You can use key-bindings (like this emacs like config) to navigate up and down the list and eventually make a selection.
Here is a search for all sudo install commands available in history with sudo make install selected.
Enter issues the selected command.

We can also use fzf to fuzzy search the command history interactively.
Here is how to install:
git clone --depth 1 https://github.com/junegunn/fzf.git ~/.fzf
~/.fzf/install
Say yes to all its configs. After that, restart your zsh shell, and press Ctrl-R, whoa, interactive command history search pops up. Enjoy!

With the vim keybindings activated in zsh you can use vi-history-search-backward (/) when in vi command mode.

That's a feature available in fish, but it seems like someone made a zsh plugin for it. It's not available in standard ZSH.

If you don't want to add any keybindings, you can search by default using Ctrl+R to move backward & Ctrl+S to move forward.
You can start search with any of the key shortcuts Ctrl+R or Ctrl+S

Related

How can I turn off vim in terminal of Mac?

I was trying to setup Mongo DB Community on my Mac(m1).
I installed Home-brew first and followed the install instruction of the Mongo DB.
However I could not run the command brew and I found that it was the problem that I didn't set up the Path in the zshell. So I tried it with my terminal but It just got stuck in this here. I tried to escape it with the :wq, :q! commands and it works. but whenever I restart the terminal it goes right back to this screen not to main screen. How can I solve this problem? plz help :(
(Im trying to learning so I'm not familiar with the terminal and codes stuffs)
You are in this situation because you typed the wrong command in the terminal and I suspect it is because you copy-pasted it instead of typing it out.
Your command is helpfully spelled out in the title bar of the window:
$ vi ~/.zshrcexport PATH=/opt/homebrew/bin:
Judging by the content of the buffer, it is almost certainly truncated and the actual command was probably:
$ vi ~/.zshrcexport PATH=/opt/homebrew/bin:$PATH
So what's wrong with that command? Everything, actually.
For starter, it should have been:
$ vi ~/.zshrc
with ~/.zshrc being your shell's main configuration file.
Everything after that shouldn't be here.
Vim, which is the program providing the vi command, takes one or more filenames as arguments. With that command, you told Vim to open two files:
~/.zshrcexport
PATH=/opt/homebrew/bin:$PATH
the former is the one shown in your screenshot,
the latter is not shown but it is likely to have a rather long name,
neither of those files are supposed to exist.
How to get out of that mess?
Assuming you are in the situation shown by that screenshot, do the following:
Press the esc key to make sure you are in what is called "normal mode" in Vim.
Press the : key to enter "command-line mode".
Type qa!, then press the return or ↩︎ key.
At that point you should be outside of Vim and in your shell. It is time to delete the non-wanted files you created with these two commands, each followed by a press on the return or ↩︎ key:
$ rm ~/.zshrcexport
$ rm PATH=/opt/homebrew/bin:$PATH
Now you should finally be able to edit your shell's configuration file but I recommend you don't do it with the vi command. nano is a much simpler editor that doesn't require as much learning.
Open the configuration file in nano with:
$ nano ~/.zshrc
Move around with your cursor keys until you find the right spot, just like in a regular text editor. The file is probably empty anyway.
Type that "export" line:
export PATH=/opt/homebrew/bin:$PATH
Press control+X to quit, as instructed at the bottom of the screen.
Press the appropriate key when asked if you want to write the file.
Avoid vi or vim in the future until you actually have or want to learn it.
As for why you end up in Vim when you open a new terminal window I have no clue. Maybe another one of your mistakes?

Why is ctrl+~ not working in vscode on linux?

When I press Ctrl+~ to open the terminal in vscode, instead it will show notifications.
How can I find out which program is intercepting this key combination?
Ctrl+~ is actually Ctrl+`
And by default is bound to showing dunst history, check ~/.config/dunst/dunstrc to see the following line is there under the [shortcuts]:
history = ctrl+shift+grave
If that's not it, then the problem is probably you're looking for the ~ (tilde), instead of the ` character.
From: https://forum.endeavouros.com/t/ctrl-does-not-work-on-i3/12645
To answer the question there is a way to troubleshoot keybinding issues. See VSCode Wiki: Keybinding Issues.
In your case you could run the command Developer: Toggle Keyboard Shortcuts Troubleshooting (from the Command Palette) and you presumably would have seen the Ctlr+` resolved to some other command with the source being listed as the extension which overrode the built-in command.

backspace does not work git bash

The first time I used git bash, backspace was working fine. Then I installed Perl and didn't use git bash for a few days.
When I use backspace in git bash now it doesn't work correctly.
For example, trying to remove the second s and everything after it in ussers:
Now when I use the backspace this happens:
Can anyone help?
I had this same problem today after switching to mintty with a fresh version of git. In my case, it was an old .bashrc file I'd been carrying around for many years with the line export TERM=ansi. After removing that and starting a new bash, everything works again.
More generally, to debug, I was suspicious of all the dot-config stuff in my home directory--so I moved things like .bash_profile, .bashrc, .inputrc, etc. into a temporary directory where they wouldn't be read by bash. Then, I started bash and saw that it worked. I mention it because, if it isn't specifically the TERM issue I had, you might be able to debug using the same technique.
Not sure if this is the same issue.. But I ran into the same after upgrading git
This helped me.. Just enter TERM=XTERM in the bash
I got this from an issue log in github..
screenshot
Just in case people run into a capitalization issue please type TERM=xterm-256color directly into git bash and hit enter. This fixed my issue.
Right click on the menu/title bar of git bash, select options, click on terminal from the options on the left, under the 'type' dropdown, select 'xterm', then apply and save.
Now open a new git bash and it should work :D

Emacs term pastes strange line into terminal when changing directories

I am using fish terminal inside of Emacs term
My normal prompt on load looks like the following
Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
$>
Ok, when I load fish term inside of term.el it looks like this
Welcome to fish, the friendly interactive shell
Type helpB for instructions on how to use fish
7;file://Collins-MacBook-Air.local/Users/collinbell/Programs/riddley⏎
$>
A cd command in my normal terminal looks like this
$> cd ~/
$>
However in the emacs term.el it looks like this
$> cd ~/
7;file://Collins-MacBook-Air.local/Users/collinbell⏎
$>
I have no idea why it is pasting the cwd into the buffer, but it does it every time a directory changes. Emacs also makes the system sound after this, while other commands like ls do not make the system sound.
This is obviously not the biggest issue in the world, but I do run clear as a pre-command to keep my terminal looking clean (although I turned it off for this example) and Emacs pasting this line into the buffer really messes with sublime usage.
You seem to be experiencing a known issue.
Try this fix:
In your fish config file ~/.config/fish/config.fish add the following:
function fish_title
true
end
Also, see this from the fish documentation, though according to the github issue, the fix suggested in the docs might not work, while the above function does.
According to the fish documentation, this is what's going on:
Fish is trying to set the titlebar message of your terminal. While
screen itself supports this feature, your terminal does not.
Unfortunately, when the underlying terminal doesn't support setting
the titlebar, screen simply passes through the escape codes and text
to the underlying terminal instead of ignoring them. It is impossible
detect and resolve this problem from inside fish since fish has no way
of knowing what the underlying terminal type is. For now, the only way
to fix this is to unset the titlebar message, as suggested above.

.emacs Edit to Always Start Emacs in Terminal Mode?

I use emacs as my editor-of-choice, and since I'm doing a lot of work in a terminal I always run emacs as
emacs -nw
so that it runs in the terminal instead of in a window.
I'd like to just run emacs and have it know that it should run in a terminal. My question is - how do I edit my .emacs file so that this is the default behavior?
You can't do this in the .emacs file. By the time that file is being parsed, the "chosen" emacs binary is already running.
You can install the emacs-nox package as one commenter suggests, or create an alias in your shell so that "emacs" is always treated as "emacs -nw".
Randy
I'm using a bash alias instead of .emacs to do that.
Add this line to your ~/.bashrc.
alias emacs='emacs -nw'
There is any easy way to solve the problem in general that has nothing to do with emacs at all and will work for any program that can choose between running in the console vs X:
unset DISPLAY
Of course you may not want to put that in your configuration file to be applied globally to all your shell sessions, so if you want it to apply to only emacs, then either call it from the command line like this:
DISPLAY= emacs
note the space!!! if you leave the space out it means you're setting the DISPLAY to emacs instead of setting DISPLAY to nothing... this command is a shorthand for:
DISPLAY=; emacs
So either use the above from the command line(s) or put that in a wrapper script that would look something like this:
#!/bin/bash
unset DISPLAY
exec emacs
I recommend the exec there because it will replace your wrapper script with emacs; to see the difference between the two you can run:
pstree -p
When I was first setting up a "emacs -nw" alias for emacs in windows I got stuck in a situation where I thought tototoshi's explanation hadn't worked. Yet all that was required was a restart of my terminal. Therefore, i think its worth mentioning that in windows (at least) if you are using emacs within the git bash terminal to create the .bashrc file and add "alias emacs='emacs -nw" to it (as tototoshi mentions) you have to close and reopen your terminal for it to work.