2

I want gvim to always open in a separate window and return the command prompt immediately. In other words I want

gvim filename

to be the same as

gvim filename &

it seems like I should be able to do this with an alias. Is there a special wildcard character for aliases? Something like:

alias gvim="gvim <wildcard> &"

Where "wildcard" would be replaced by whatever text comes after gvim on the command line.

I also tried:

function gvim() 
{ 
    "/cygdrive/c/program files (x86)/vim/vim72/gvim.exe" "$@" "&" ;
}

But it only opens gvim and doesn't return the prompt like I want it to.

If someone could point me in the right direction, it would greatly improve my understanding of how .bashrc works.

Thanks!

-Derek

UPDATE: I got it to work by getting rid of the quotation marks around the & and the semicolon at the end:

function gvim() 
{ 
    "/cygdrive/c/program files (x86)/vim/vim72/gvim.exe" "$@" &
}
derekswanson08
  • 177
  • 2
  • 13

4 Answers4

5

The last is almost right. What is wrong is that plain & tells shell to run process in the background and "&" adds argument to the gvim command-line (i.e. instructs gvim to open file named &). By using quotes here you just instruct shell not to treat & specially and gvim has no reason to do the shell’s job.

ZyX
  • 52,536
  • 7
  • 114
  • 135
  • Thanks, I got it to work by removing the quotes around the & and by getting rid of the ;. Now it works perfectly. – derekswanson08 Nov 07 '12 at 20:12
  • Can you enlighten me why using a terminating semicolon is a syntax error when I don't use quotation marks around the ampersand, but it is ok when I do use quotation marks? – derekswanson08 Nov 07 '12 at 20:20
  • @derekswanson08 You have nothing to terminate here. Command was already terminated by `&`. – ZyX Nov 08 '12 at 03:23
  • @derekswanson08 you can mark this as accepted answer so it won't show up as an open question. – none Nov 08 '12 at 15:28
0

instead of using alias, use a shell function. It works the same in general, and you've got more options available to you:

function gvim() {
 gvim.bat `cygpath -w $*` &
}

Update Also, there are some hooks in the default .bashrc (on my box anyway) to automatically load functions from a ".bash_functions" file, you just need to uncomment it (about line 117 for me):

# Functions
#
# Some people use a different file for functions
if [ -f "${HOME}/.bash_functions" ]; then
  source "${HOME}/.bash_functions"
fi

then add the function definition to .bash_functions to have it auto-loaded when you start a bash shell.

Update 2 added call to cygpath to handle full paths in unix form

Gus
  • 3,534
  • 1
  • 30
  • 39
  • my answer depends on your having answered 'yes' to the gvim installation question asking if you wanted it to create the batch files for command line access... – Gus Nov 07 '12 at 01:59
0

The first thing to understand about .bashrc is probably that it only gets read when the shell starts -- in the case of cygwin, when you open the command window. It's not clear from your post whether you are restarting your command window, or what.

You can experiment with the aliases/functions without putting them in the .bashrc, just to find out what works. Just enter the aliases or function definitions directly on the command line, and then try to run them.

I think the problem with the function you provided is you put the & inside double quotes. Take off the quotes and it should work.

BobS
  • 2,588
  • 1
  • 15
  • 15
  • You can also manually source files on the command line. `. .bashrc` will do it for .bashrc (`.` is an alias for `source`). – dash-tom-bang Nov 08 '12 at 00:25
0

What I do is have a small bash script that I call gvim with and alias that.

==> alias vi
alias vi='~/include/bin/dvim'

==> cat ~/include/bin/dvim
#!/bin/bash.exe

/cygdrive/c/Program\ Files/Vim/vim73/gvim.exe $1 &
Daniel Haley
  • 51,389
  • 6
  • 69
  • 95