6

On my Windows machine, running gVim appears to tie up the git for windows shell, whereas if I run from the command prompt, this is not the case.

How might I be able to run gVim (or any other program, really) without tying up the shell and having to add an '&' to the end of each command?

Any help on this issue would be much appreciated.

Git Bash:

$ gvim hello.txt
[Terminal is now blocked]
[Hit ctrl+c or close gvim, terminal takes input]
$ 

Windows Command Prompt:

> gvim hello.txt
> [ Can still access cursor and execute commands]
funseiki
  • 339

4 Answers4

3

The quickest (and possibly dirtiest) solution I've found to be able to use gvim in the way I'm used to was to modify the .bashrc file.

I added the following function to the end of it:

gvim()
{
    /path/to/gvim.exe "$@" &
}

The "$@" takes arguments passed in from the shell, and the & forces gvim to run in the background.

I'd like to not mark this answer as accepted, as there might be a better solution (e.g. I screwed up my msys/vim settings somewhere).

funseiki
  • 339
3

The answer I've found that works most like it does on my Linux box is to set an alias in your .bashrc:

alias gvim='start gvim'

This is still not the most ideal fix, but it feels a little cleaner to me than creating a function. Also it does not print the process ID line (example: "[1] 6840") when you run it or when you close the external window.

This still has the problem from the function fix that it only works for things you create an alias for. So notepad for example still blocks your bash unless you create a start notepad alias. Based on this post at the MSDN blog it looks like Windows executables have a flag that tells the console whether or not it is a GUI application, but from what I can see, git bash does not differentiate based on that flag in the same way that cmd.exe does. If you could attach something to every command entered that would check the .exe for the GUI flag, and prepend start based on your result, then you'd be good to go.

Zurcher
  • 46
0

I found a much better solution, at least for 2025, and using Windows 11 as the OS I tested on:

In your ~/.bashrc, or type it in the shell each time: alias gvim='setsid /c/vim/vim91/gvim.exe'

You may already know, but just in case: ~ in Git Bash defaults to your directory under C:\Users (or /c/Users/ within the shell), so you can still have a .bashrc.

This solution doesn't leave behind a CMD window for each launch, like the accepted answer. It also works better than winpty, which I've seen suggested for this issue. winpty works but leaves behind a stopped process per gvim that you launched, which needs to be manually killed.

Starman
  • 121
  • 4
0

You can run any program in the background from bash by using &:

gvim &

Gvim is different from most programs and normally puts itself in the background without the need for the &. To run it in the foreground, you normally have to start it with the -f option. I don't know why gvim is not behaving that way from bash on Windows.

garyjohn
  • 36,494