How do you scroll up/down using your mouse/keyboard in Git Bash? - bash

I'm running windows 10 and I've just installed Git version 2.29.2.windows.2
I'm trying to learn Git on Udemy and the instructor can clear his screen by typing clear. However, he can also scroll up and down to see the commands he'd previous typed after he's cleared them. I'm unable to do that. I can't scroll with the mouse. Page up/Down don't do anything and nor do the arrow keys. Once I've typed clear, all my previous commands seem to disappear and I can't access them. I've had a look at the Git Bash options and I can't see anything which would fix this. I've also searched this site for answers but the most recent one I could find was from 2016 and is out of date and doesn't work.
Could anyone explain how I can scroll once I've entered clear so that I can see my previous commands? Thanks.

It's not normal for the mouse scroll to retrieve history entries. It usually scrolls up the terminal window. Here are a few things to try.
Make sure you're using the Git Bash terminal using Mintty, not the Windows command prompt running bash.
Up-arrow and down-arrow are the same as Ctrl-P and Ctrl-N.
Run stty sane.
Do bind -p |less and look at the entries for next-history and previous-history. They should look like:
"\C-n": next-history
"\eOB": next-history
"\e[B": next-history
"\C-p": previous-history
"\eOA": previous-history
"\e[A": previous-history
Check your ~./inputrc and /etc/inputrc files.
Read the Bash man page sections for READLINE and bind.

Related

How to quickly show program help in pager

I often find myself appending --help|less to commands that I am crafting a command in a shell to explore the CLI of the program I am using. What ways are there to accomplish the same thing with fewer key-presses?
For instance, if I wanted to use the new way of switching branches (I don't, I'm stuck in my ways happily doing git checkout -b other_branch) then my history (and thought process) could look like
git change other_branch # Guess what the command should be
git --help|less # Backtrack to where I am confident I know the command and ask for help
git switch --help|less # Gradually build up the command from there
git switch other_branch
For context:
I typically want the pager either to be able to search or because I am working in tmux and activating scrolling takes a few additional, and awkward, key-presses.
I typically use zsh on Ubuntu or Debian.
I typically use arrow up to iterate on my previous command.
Add the following to your .zshrc file:
# Alt-H: Open `man` page of current command.
unalias run-help
autoload -Uz run-help{,-{git,ip,openssl,p4,sudo,svk,svn}}
Then restart your shell.
Now, whenever you're in the middle of typing a command, you can press AltH (^[h) to immediately open the man page for that command. Then, after you quit your pager, Zsh will will automatically restore your command line, so you can finish typing.

how do you enable command line editing with vim keys using mac terminal?

I'm reading this book and I really do not understand what this author is talking about. It appears that you can program your mac to go forward one character by either hitting l or ^F. I do not understand the difference between emac key stroke and vim keystroke. He also says run this command and 'place it in your $HOME/.bash_profile but I cannot figure out how to place the command in the bas_profile.
The Author is talking about make you terminal console behaves like vi, this means typing set -o vi in your terminal, the console will work similar vi. So you will be able to navigate using the motion keys of vi, use INSERT mode, x to delete, etc.
You can set that permanently if you include this command in your ~/.bash_profile file.
If you are not sure what it does, I don't recommend so.

zsh-like history in bash

I really like zsh's history autocomplete feature. Namely, when I press up, I get the previous command. When I type emacs and press up, I get the last emacs command I used. When I type git and press up, I get the last git command I used. When I try to do this in bash, it just goes to the last generic command I used. Essentially, I want to be able to half-type a command and press up to get the last command I used that matches what I've typed so far. I don't know how to achieve this in bash. I can't use zsh on this system, so is it possible to replicate this functionality in bash?
The up arrow is bound to the previous-history command. You want to rebind it to history-search-backward (which is unbound by default) instead. You can check which keys previous-history is currently bound to:
$ bind -p | grep previous-history
"\C-p": previous-history
"\eOA": previous-history
"\e[A": previous-history
In my case, the last two both represent up arrow (the exact escape sequence may differ from terminal to terminal, or depending on what mode the terminal is in, but these two are fairly standard). Especially since previous-history will still be available with Control-P, it's safe to change the behavior of the up arrow.
Add this to your .inputrc file (creating the file if necessary):
"\e[A": history-search-backward
"\eOA": history-previous-history
Or, you can add call bind from your .bashrc:
bind '"\e[A": history-search-backward'
bind '"\eOA": history-previous-history'
You may also want to similarly bind history-search-forward to the down arrow key, \e[B and \eOB.
You can search your history with ctrl+R.
When you first press it, the prompt will change and you will be able to enter the characters you want to search for in the history. The new prompt disregards characters that were entered in the previous prompt and will overwrite them when something from the history is matched.
It will display the last command that matches your input and you can press ctrl+R again to navigate the results from the latest to the earliest.
When you've found the entry you're interested in, you can either press Enter to execute it or the left or right arrows to return to the standard prompt to edit the command line. Pressing the up or down arrows will return to the standard prompt but navigate the history one step forward or backward, which I find more confusing than anything.

Why is my mac terminal acting buggy when I use the up arrow key?

I normally use the up arrow key to scroll through previous commands in my terminal. I've noticed that over the past few months, when I do this, I do not get previous commands but instead a 'buggy behavior'. For example, when I press the up arrows key, it might enter gibberish and apparently random spaces and a small part of my command. Does anybody have any ideas as to what might be causing this? Sometimes it works, sometimes it doesn't work. It's starting to get very annoying. (fyi, I'm using yosemite and osx if that has any relevance)
I know this is an old question, but I recently had the same issue. The problem is likely your PS1 prompt. You may not be fully enclosing your non-printing characters with \[ ... \]
To solve:
in your terminal echo $PS1 - this will show you your current PS1 setting.
Make sure that any options (such as color options) are enclosed by \[ and \] and set it. e.g. PS1='\[\e[95m\]\u \[\e[93m\]\w:\[\e[92m\]\$'
Save your new PS1 permanently in your .bash_profile or wherever you keep your bash settings.
Example:
My faulty PS1 was \[\e[95m\]\u \[\e[93m\]\w: \e[92m\]\$
I was missing the opening encloser, \[ after the \w:
My corrected PS1 is \[\e[95m\]\u \[\e[93m\]\w:\[\e[92m\]\$
In my case, the up-arrow stopped working properly for me in OSX shortly after I changed my .bashrc to use "HISTSIZE=-1". My unconfirmed guess is that the bash version is too old to handle the -1 option, so bash stopped recording a history entirely. My up-arrow key was being interpreted correctly, but there was no history to retrieve. Once I changed my HISTSIZE, the up-arrow (and the history command) in new terminals started working again.
To be clear, I can't say if your problem is from the same cause. I don't recall getting gibberish in the history, though I did see some odd behavior such as an errant "." somehow appearing in ls commands. Still, if someone is having problems with using the up-arrow to retrieve previous bash commands, it seems worth a shot. Try checking your HISTSIZE setting, and/or invoke the history command directly, to make sure that you have a history for the up-arrow to retrieve.
I've had this problem for a while myself on my work computer I was able to resolve it when changed from ksh shell to the bash shell.
I'd recommend this Lynda course it explains most of what you need but simply type bash to change shell.
https://www.lynda.com/Mac-OS-tutorials/Unix-Mac-OS-X-Users/78546-2.html

Why is "git grep" behaving erratic on my Windows PC?

I'm using Github and Git bash on my Windows PC (running XP).
On Ubuuntu I'm happyily using git grep to plough through my code, but every time I call something like:
git grep "some text in my repo"
on Windows I get the results and afterwards I'm stuck with the bash window showing all kind of things [END], ~, [RETURN]... whenever I try to enter something.
Question:
What else besides CTRL+R, CTRL+Q, FN+END, Q, ESC can I try to not having to force-close and reopen git:bash in order to continue to work.
Thanks!
EDIT:
This is what I mean:
As soon as I start typing, the [END] string re-appears (or 50 lines ~) and I cannot write any command on Git, because whatever is in the way swallows half of what I'm typing.... nice description...
I was wondering about this also after installing git and running git bash. It seems "git grep" pipes commands through "less".
Solution: Just type q.
Is git launching less (or something similar) so up/down arrows on your keyboard scrolls through the matches? If you don't want that, try the instructions from https://stackoverflow.com/a/12166923/972216:
set GIT_PAGER=cat
Would disable it for your console session once, or
git config --global core.pager cat
to disable it for your account.

Resources