Git Bash: Not accepting my keyboard input - windows

So, I have reproduced it. Running:git ls-files|grep navbar.html|xargs vim
in my Git Bash and exiting vim making my Git Bash not accept keyboard inputs anymore. However my other Git Bash windows working fine.Any ideas how can I get arround this problem?

If you execute reset, it will be fixed.

This is a common vim usage error (see also https://unix.stackexchange.com/questions/77395/grep-l-xargs-vim-generates-a-warning-why). This command will do what you want:
git ls-files | grep navbar.html | xargs sh -c 'vim "$#" <$0' /dev/tty

Sometimes like when you are not on full screen and you insert command like
git log | git diff
or something like them that need a bigger window size to show bunch of data you'll get colon sign
:
that is for displaying data summary and you can scroll data to see more by using arrow keys.
at the end you can quit this mode using q key
press "Q" keyword to exit.

Related

aliases are not stored in zsh history

Whenever I execute a command using an alias, this command is not stored in the shell's command history.
So if I run history these commands do not appear in the list.
Nor do they appear when I press CTRL + r for reverse searching the command history.
When I press the keyboard's arrow up for scrolling through the last commands, I will see an aliased command only if it was the last command I ran. Other aliased commands are will not be displayed.
For example:
$ cd my-repo
$ gs # an alias to git status
$ history
Outputs the following:
2374 cd my-repo
(the gs command is not displayed)
A few notes:
gs is only an example. The issue is far more annoying in more complex commands since I have to retype them all over again instead of executing them from history (e.g. k get pods | grep <pod_name>, where k=kubectl).
gs is defined so: alias gs=' git status'.
I also have a few functions in ~/.alias, e.g.:
mkcd () {
mkdir -pv $1
For some reason, mkcd (or any other function in the alias file) is included in the history.
I do not mind if it prints out gs or expands to git status, I'll take any of the two...
I am using zsh with oh-my-zsh on macOS (Monterey). My shell aliases are defined in ~/.alias which is sourced in ~/.zshrc (source ~/.alias).
This happens both in iTerm2 and in the default Mac terminal.
Thank you for taking the time to help :-)
I will assume that your example alias is exactly what you have in your ~/.alias file.
So you have aliases like this (notice the space character in front of git command):
alias gs=' git status'
There is an shell option called HIST_IGNORE_SPACE which is doing exactly what you are experiencing - in short it will not add command to the history when it starts with space character. Example:
echo 'This command will make it to the history.'
echo 'This poor command will be forgotten.'
You can check your current options using setopt or specifically:
setopt | grep 'histignorespace'
So there are two ways how you can fix this - either by fixing your aliases to not start with space or, If you really don't want this functionality at all, by unsetting it in your ~/.zshrc like this:
unsetopt HIST_IGNORE_SPACE

Broken tab completion?

For some reason,
whenever I want to run a git command and use tab completion to achieve something like :
$git checkout master
When I press tab after writing git checkTab, something hello -n is being written automatically, so it results in
$git checkhello -n
I can't use git CLI with tab completion because of this. Does anyone know what's going on here?
Checked my aliases in .bashrc, one of the alias had the term 'hello' echoed in it. Removing it solved the problem.

SourceTree custom action doesn't work but works through Git terminal window

I'm trying to setup Source Tree custom Git action to extract and zip files for deploy from selected commit till the HEAD, excluding deleted files with --diff-filter.
archive -o deploy.zip HEAD $(git diff --name-only --diff-filter=ACMR $SHA)
Source Tree custom action screenshot
The problem is that this Git command works perfectly through Git terminal window without error but when I try to use it in Source Tree I get this error.
git archive -o deploy.zip HEAD $(git diff --name-only --diff-filter=ACMR fbf360c607b01fc8c504c9fcf98114eb4bc1896c)
error: unknown option name-only
Source Tree custom action error message screenshot
I've tried to figure out what is the problem with this issue, but without success. I would like to hear if anybody had this or similar problem in Source Tree because it looks like a bug to me.
$(...) is bash commands substitution, which runs the command inside and inserts its output instead into command line. SourceTree does not use bash for calling commands (it is Windows, isn't it?), and your command is not substituted.
You should instead put your command in a script, approximately like this:
------c:..\script.sh----
#!/bin/sh
git archive -o deploy.zip HEAD $(git diff --name-only --diff-filter=ACMR "$1")
and set up calling it from the source tree:
script to run: [ ...\bash.exe ]
parameters: [ -c c:..\script.sh $SHA1 ]
For Windows machines especially, it seems you need one more step from #max630's answer.
Put your command in a script (e.g. c:\scripts\deploy.sh)
#!/bin/sh
git archive -o deploy.zip HEAD $(git diff --name-only --diff-filter=ACMR "$1")
Setup the custom action as follows:
Script to run: Select bash.exe from the browse window, it might be in:
C:\Users\{user}\AppData\Local\Atlassian\SourceTree\git_local\bin\bash.exe
Parameters: --login -i c:\scripts\deploy.sh
(the --login -i is shown briefly when you open the Terminal)
Check Show Full Output and Run command silently to see the output in the action box, uncheck Show Full Output to hide it unless clicked while running.
It may be useful to add the following to the script to pause the screen to see any error messages (with Open in an Separate Window checked, I believe)
read -p "Press [Enter] key to continue..."

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.

After doing grep and passing it to vim then quitting, why am I experiencing this weird console malfunctioning?

I tried this just now:
grep -RlI "id=\"kw\"" * | xargs vim
That gave me 16 results. It opened the first result in Vim. I made my very first edit and hit :q since I didn't know the shortcut to jump to the next file.
It threw me back to the console ( I am SSHed in to a server ). My console is messed up now. Anything I type I can't see, and anytime I hit enter it seems like it processes the command but the display/view is screwed up so
[meder#linode] is tabbed in on my console, at least halfway. reset does nothing since it seems to have messed up my real console.
Can anyone offer a solution that doesn't have this same downside? Or can anyone provide an explanation for why :qing out of the very first file messed up my console?
Background information: My PC is Debian Ubuntu, I am SSHed into a RHEL box. The files I opened were text/ascii files phtml/php files and not some weird binary files with crazy characters in them.
Here's a screenshot of what happened
EDIT #1: I just typed reset again and it seemed to work. The first reset did not work I think because somehow the console inserted some whitespaceish character inside it? Anyways, I would like an explanation for this weird behaviour.
Try:
vim -o `grep -RlI "id=\"kw\"" * `
From the man page for xargs:
Undefined behavior may occur if utility reads from the standard input.
That line isn't in the Linux man page but it is present on my Mac. If you want to run a program that you intend to read standard input, the usual linux version of xargs will need an argument to read its input from a file:
OPTIONS
--arg-file=file, -a file
Read items from file instead of standard input.
If you use this option, stdin remains unchanged
when commands are run. Otherwise, stdin is
redirected from /dev/null.
Vim is intended to run with both standard input and standard output connected to real (a very rare case these days) or pseudo tty devices. Wierd things will happen if you upset this arrangement.
The fundamental problem with your command was that, with standard input redirected to the pipe, xargs had no way to run a vim with a "normal" standard input. So the vim mode changes and command input were not what you expected.
You can probably fix this by typing a return, a tilde, and a period. This will force your ssh session closed from your end, you can then ssh in again, and run "ps" to check for anything left hung in the background that you should kill(1).
You can use :next or :n to get to the next file to edit. You can also use vim -o to open up all the matching files in different windows in Vim.
Not sure why your console is messed up though. I tried using your command and my console was fine.
Console options are set by stty, so you may want to save its options to a bash variable and restore them after vim exits, like this:
function vim()
{
STTYOPTS="$(stty --save)"
vim "$#"
stty "${STTYOPTS}"
}
But it is probably the better way to use zsh for this task: if you put the only line ttyctl -f into your ~/.zshrc, then zsh will automatically restore terminal options after program exits. ttyctl is a zsh builtin, so you cannot use it from bash.
Other folks covered what happened and what to do about it. As to why, the answer to that probably lies in what input Vim received from the xargs command and tried to execute as if that input came from a terminal. I don't know how to talk terminal, but you can imagine that Vim got some strange commands that crashed it or told it to quit. Similarly unpredictable things happen when you cat a binary file.
Anway, I have another idea. Have you tried using vimgrep to browse a list of files matching a pattern?
:vimgrep /id="kw"/ *
:copen
This greps for id="kw" in all files in the current directory. :copen opens up a window with a list of matches. You can browse that list, clicking enter to jump to a file position.
For more information, see
:help grep
:help :vimgrep
:help :copen
:help quickfix
If you really need that -I option, see
:help :grep
:help 'grepprg'
See also: Vim: Warning: Input is not from a terminal
Try to use ... | xargs sh -c '...' and then read from the controlling terminal device /dev/tty.
echo ~/.profile ~/.bashrc | xargs sh -c 'vim "$#" </dev/tty' dummy_script_name
# based on a tip by Laszlo Ersek on http://unix.derkeiler.com/Newsgroups/comp.unix.programmer/2010-03/msg00051.html
#find . -maxdepth 1 -type f | xargs sh -c 'rm -i "$#" </dev/tty' dummy_script_name

Resources