How to run windows gvim from cygwin? - vim

I am not able to run gvim from cygwin. When I try to open a new file with :
gvim filename
gvim opens a file with 'No name' and displays error as :
Error detected while processing command line
E492: Not editor command: C:\cygwin\home\chandan\l
Press enter or type command to continue
More problematic is that I can't open existing file in the path
>which gvim shows /usr/bin/gvim
I have put alias gvim=/cygdrive/c/Program\Files\(x86\)/Vim/vim73/gvim.exe still

I wrote cyg-wrapper.sh for this sole purpose.
It supports:
symbolic links
options that must not be interpreted as pathnames (see -c with gvim, or any flags starting with a minus sign).
In other words, it enables us to type:
gvim /etc/profile -c /PS1 -c "echo 'correctly opened'"
# or even:
cd ~/tmp ; ln -s ~/bin/cyg-wrapper.sh
gvim -d http://hermitte.free.fr/cygwin/cyg-wrapper.sh cyg-wrapper.sh
explorer -e
explorer "$vim"
explorer http://hermitte.free.fr/
Note: I use the following function in my .profile to run gvim with cyg-wrapper.sh
gvim() {
opt=''
if [ `expr "$*" : '.*tex\>'` -gt 0 ] ; then
opt='--servername LATEX '
fi
cyg-wrapper.sh "C:/Progra~1/Edition/vim/vim73/gvim.exe" --binary-opt=-c,--cmd,-T,-t,--servername,--remote-send,--remote-expr --cyg-verbose --fork=2 $opt "$#"
}
EDIT: Currently (Sept 26 2014, using Vim 7.4), Windows gVim uses C:\Windows\gvim.bat to launch gVim from the command line. Replacing the gvim.exe path in the gvim() function with this script allows launching gvim without changing the path to match the current Vim version (which may actually be in Progra~2); however, it appears to also open a superfluous cmd.exe window.

Found this thread, I find the answer from jens unacceptable. We're not asking to be told not to do it. I didn't like the other answers either there was always some quirk, like settings not used or an extra command line window popping up. I Did some digging and this works for me. No extra command line box for nothing and it uses my proper gvim settings.
alias gvim='HOME=/cygdrive/p/ cygstart /cygdrive/c/Program\ Files\ \(x86\)/Vim/vim74/gvim.exe'
You simply need to alter the HOME to your own. To find out what to put there run gvim from windows then put in ":echo $HOME" and hit enter in my case it shows P:\ so that translates to /cygdrive/p/
Also if your gvim.exe is in a different directory/version you'll need to adjust.
Now when I type 'gvim script.sh' at a cygwin command prompt it launches gvim with the file, all nice and neat!
UPDATE
I found a slightly better way to do this. Using the alias was tying up my session that I ran the gvim from, I wanted it to launch as a separate process, using "gvim &" is inelegant as it lists job number when launching and displays a "done" line when completed. I'm too fussy so I figured out how to get that all tidy by using a function.
Just add this to your .bash_functions file, it builds on the previous section regarding home directory and backslash use.
gvim() {
ORIGHOME=$HOME
HOME=/cygdrive/p/
/cygdrive/c/Program\ Files\ \(x86\)/Vim/vim74/gvim.exe $1 & disown
HOME=$ORIGHOME
} 2>/dev/null
UPDATE 2 for babun users!
Ok since having wrestled with this originally I have ended up scrapping the original cygwin install in favor of babun which seems to be a less troublesome setup for those wanting linux functionality in windows without a full blown virtual. Of course my gvim launch script broke, and I had to do the following in my .zshrc file (babun uses zsh, at first I resisted and switched it to bash but then relented as I figured they must have reason, and they did, I like it)
gvim() {
OLD_HOME=$HOME
OLD_VIMRUNTIME=$VIMRUNTIME
export HOME=/cygdrive/c/Users/gmitchell/
export VIMRUNTIME="C:\Program Files (x86)\VIM\vim74"
TARGET=$(cygpath -w $1)
(/cygdrive/c/Program\ Files\ \(x86\)/Vim/vim74/gvim.exe $TARGET &)
export HOME=$OLD_HOME
export VIMRUNTIME=$OLD_VIMRUNTIME
}
*Note: the surrounding curved braces ( ) stops the job id from messing up your clean shell, and you no longer need the "disown"
P.S. The only remaining annoyance with this now is that you cannot "exit" the babun shell until all gvim instances you've launched are closed. Maybe someone can figure that own out. I've tried. When you type exit it just hangs there until you've exited all gvims.

Do yourself a favor, don't attempt to run a windows gvim from cygwin. The set of environment variables is likely completely different, and the pain of backslash directory separators, blanks in filenames, inability to understand /cygwin paths makes this an exercise in futility.
Then, what? Install the cygwin version of gvim and forget about all these problems.

Here is all you need to do:
alias gvim="run.exe /cygdrive/c/Programming/Tools/Vim/vim74/gvim.exe"
Works also with Notepad++, like so:
alias np="run.exe /cygdrive/c/Programming/Tools/Notepad++/notepad++.exe"

I have the same problem
because of the $SHELL var
so, I simple do like this
alias gvim='cmd /c "set SHELL=cmd & start gvim"'
It works for me, may be this will be help you too.
and maybe you want use the same alias for vim.

I put the following function in my .bashrc:
function gvim
{
GVIM_CMD=/cygdrive/c/path/to/gVimPortable.exe
if [[ -z "$1" ]]
then
$GVIM_CMD
else
$GVIM_CMD `cygpath -w $1`
fi
}
This allows me to launch gVim from anywhere.
It works fine for files with spaces, too...

This is a take off on low351's answer. I was unhappy with leaving HOME changed in the cygwin terminal, since I use that locally for a cygwin HOME, not my windows home. But gvim is all setup using Windows HOME. I added this to my .zshrc:
# run windows gvim from command line
gvim() {
local OLD_HOME=$HOME
HOME=/cygdrive/c/Users/jason/
local TARGET=$(cygpath -w $1)
/cygdrive/c/Program\ Files\ \(x86\)/Vim/vim74/gvim.exe $TARGET & disown
HOME=$OLD_HOME
} 2>/dev/null
really, just storing and restoring HOME. Being local, OLD_HOME goes away when the function returns, so it doesn't pollute the environment. If you're editing a cygwin file, running it through cygpath -w will make it a file path that windows gvim understands. It also lets you use window paths (e.g. C:/tmp/foo.txt) but w/ unix '/' separators, so you can dispense with /cygdrive/c. I believe this is all compatible w/ bash. This gives the following output:
> gvim
[2] 5060
>

It seems like the main problem is the HOME and VIM variables which are appropriate to the cygwin environment get exported when running Windows gvim, causing problems because Windows gvim knows nothing of cygwin. This fixes it:
alias gvim='env -u HOME -u VIM /cygdrive/c/Program\ Files\ \(x86\)/Vim/vim74/gvim.exe'
I have Windows gvim set as the default application (in Windows) for many filetypes, so Windows gvim frequently gets opened via the open alias. This fixes that use case:
alias open='env -u HOME -u VIM cygstart'

I just renamed gvim.bat which comes with the standard Win32 vim installation to gvim and put it into /usr/bin.
I am also sharing settings beetween Win32 GVim and Cygwin VIM referring to this article:
http://vim.wikia.com/wiki/Synchronize_configuration_to_many_computers
This way i can have both Vim and Win32 Gvim running with the same plugins and settings.

Since I've tried all of these and had issues I'll show what I'm using. I wrote it in ruby and it handles everything I throw at it (files, directories, or nothing) without any errors/popups/etc.
I saved it as gvim in ~/Dropbox/bin/cygwin/ (which is in my PATH)
#!/usr/bin/env ruby
args=''
filepath=''
arg=ARGV[0]
if arg
if File.exist? arg
if File.file? arg
args+="--remote-tab-silent "
end
filepath=`cygpath -w '#{arg}'`.strip
filepath="'#{filepath}'"
end
else
end
exe="'/c/Program\ Files\ \(x86\)/vim/vim73/gvim.exe' #{args} #{filepath}"
spawn exe

Here's the script I use for ~/bin/gvim. It runs it as if I called gvim FOO from the "Run" dialog (thanks to the batch file being invoked by explorer.exe). Can't play too nicely with the command line, but a self-deleting batch file comes to the rescue.
#!/bin/bash
TEMPFILE_NAME=gvim-`date +%s`-${RANDOM}.bat
TEMPFILE=$TMP/$TEMPFILE_NAME
TEMPFILE_W=`cygpath --windows --absolute --long-name "$TEMPFILE"`
TARGET=
if [ "$1" = "" ] ; then
TARGET=`cygpath --windows --absolute --long-name "."`
else
TARGET=`cygpath --windows --absolute --long-name "$#" | tr '\n' ' '`
fi
WIN_GVIM=`where gvim.exe | tr -d '\r\n'`
WIN_GVIM=`cygpath "$WIN_GVIM" --windows --absolute --long-name`
unix2dos > $TEMPFILE << EOF
start "gvim" "$WIN_GVIM" $TARGET
DEL %~f0
EOF
chmod +x $TEMPFILE
explorer.exe "$TEMPFILE_W"
Special thanks to this answer for the explorer.exe technique.

Related

Shortcut Command "ed" to START Sublime Text

I'm used to creating the following ed.cmd file on my Windows PATH which launches my favorite editor:
START "Editor" "C:\Program Files\Sublime Text\sublime.exe" %1
I'd like to do the same thing in Linux but run up against a few challenges:
ed seems to be reserved/exist. I can compromise on this an use edi if need be.
I need to launch the editor asynchronously (as in START in Windows). Typing subl myfile.js from the console "interrupts" my console session until sublime is closed again.
I don't know how the .cmd file equivalent (in Bash) should look
So I'd be grateful for a bash script to do the above and instructions on how to install it on my path so it's globally accessible.
ed isn’t reserved; it just already exists. You can prioritize your own script by listing its directory earlier in $PATH than /usr/bin (or wherever ed is – see command -v ed).
It’d be fun to use ed for this, so, in your shell of choice:
$ cd
$ mkdir -p .bin
$ ed
i
#!/bin/sh
subl "$#" &
.
wq .bin/ed
$ chmod +x .bin/ed
where $ is the prompt and everything else is something you type. Now just put PATH=~/.bin:$PATH in your .profile.
You could also make it a function in your .bashrc. That’s probably cleaner.
ed() {
subl "$#" &
}

How do I get Cygwin xterm to use bash and not sh?

Just updated cygwin to 1.7.28 on Windows 7.
Previously when starting X, the xterm would open with bash. For some reason it is now opening with sh?
What configuration changes do I need to make so that bash is the default shell again?
Not sure why this change happened.
The shortcut to open the xterm is the same as it was during my initial installation.
C:\cygwin\bin\run.exe /usr/bin/bash.exe -l -c /usr/bin/startxwin.exe
But it still starts with the default shell set to sh.
I don't understand what changed.
My passwd file is the same as it was before.
It appears that everything starts fine with the standard shortcuts, but the X and xterm startups are not sourcing /etc/profile
I had /etc/shells already (upgraded from ??? to 1.7.29), might have been new with upgrade, but still didn't work (xterm running sh instead of bash). Changed permissions on bash to fix.
It was 700 changed to 755
chmod 755 /bin/bash
xterm seems to need the /etc/shells file to be present to work. Add an /etc/shells file with the following contents:
# /etc/shells: valid login shells
/bin/csh
/bin/sh
/bin/bash
/bin/tcsh
/usr/bin/csh
/usr/bin/sh
/usr/bin/bash
/usr/bin/tcsh
Chris
Run following command to set bash as default shell.
set shell=C:/cygwin/bin/bash
Note path C:/cygwin/bin/bash may vary.
(Removed answer regarding /etc/passwd)
I tried your command on my cygwin and got the same behavior, i.e. xterm loaded with /bin/sh.
However, if I simply ran startxwin.exe directly, I get an xterm loaded with /bin/bash.
Dunno if this works for you, but, worth a try.
I had the same issue with sh launching, but managed a different workaround after having issues with /etc/shells
I also wanted to get rid of the default white /bin/sh xterm that startxwin.exe created.
It turns out there's a .startxwinrc that startxwin.exe sources, so I had it do this:
# Launch prettier xterms with bash
. ./.profile
# Exit the cruddy white xterm launched by startxwin
exit
The dot-space syntax above is equivalent to "source" in bash, but is more shell-independent.

Defining aliases in Cygwin under Windows

I am trying to define some aliases in cygwin, but with no success. I am doing so like this at the end of the .bashrc file.
alias foo='pwd'
I have tried to add this line in a .bashrc file in both inside the home folder of cygwin and in the home folder for the Windows user I am on C:\Users\Nuno\. In both cases I have just appended this line to a copy of the /etc/skel/.bashrc file. In either cases, it didn't work.
I had this working before. I had to reinstall Cygwin and ever since it never worked properly again. I have removed all files (or at least think so, when doing the reinstallation). I have also noticed that in the first install (when it was working) cygwin already was creating .bash files in the home folder. Now, it doesn't.
I am on a machine running Windows 7.
EDIT: My cygwin home folder is set to the Windows home folder C:\Users\Nuno\. I have placed what I think is a valid .bashrc file there, but it still doesn't work.
Thanks in advance.
As me_and already explained what's going on I just want to add a workaround should you for whatever reason not be able or willing to remove Windows' HOME environment variable.
Normally the shortcut for Cygwin executes
C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico -
Instead you can create a batchfile with the following content and start that:
#echo off
set HOME=
start C:\cygwin\bin\mintty.exe -i /Cygwin-Terminal.ico -
That will start a a Cygwin windows whose home directory settings are not overridden by a Windows environment variable.
Your .bashrc file will be loaded from wherever Cygwin Bash thinks your home directory is when it starts. You've mentioned in your edit that you've changed your home directory, but not how, so it's possible you've made a mistake there.
Cygwin will load your home directory from one of two places, and if they differ it can cause problems:
The HOME environment variable. This will be picked up from however you launch Cygwin, so normally from Windows itself. You can see what environment variables you have defined by pressing Win+Pause, going to "Advanced system settings", "Environment Variables…". If "HOME" is in either "User variables" or "System variables", delete it – it's unnecessary and only causes problems.
Cygwin's /etc/passwd file (normally C:\Cygwin\etc\passwd from Windows). This will have a number of lines containing details of each user on the system; the seventh : separated field is the home directory. You can tell which user it's looking at by running whoami from a Cygwin bash shell.
If whoami reports nunos, you should have a line in Cygwin's /etc/passwd that looks something like the following:
nunos:unused:1001:513:U-System\nunos:S-1-2-34-567890-123456-7890123-1001:/home/nunos:/bin/bash
It's that /home/nunos that's important; if it's something different you should probably reset it to that, at which point you want to use the .bashrc in Cygwin's /home/nunos/.
You should also be very wary of directories that contain spaces for this. C:\Users\nunos should be fine, but beware in particular C:\Documents and Settings\nunos, which just won't work with Cygwin.
I had the same issue, where the aliases added to ~/.bashrc didn't work.
It seems that, for some reason, the ~/.bashrc was not executed when launching the console.
I stumbled upon a response that fixes the issues
So, you need to create a .bash_profile file. This one seems to be the default script, and put this code in it, to ensure that the .bashrc is executed.
# ~/.bash_profile: executed by bash for login shells.
if [ -e /etc/bash.bashrc ] ; then
source /etc/bash.bashrc
fi
if [ -e ~/.bashrc ] ; then
source ~/.bashrc
fi
That works for me, just make sure that .bash_profile is executable. (chmod +x ~/.bash_profile)
Here's a really quick and dirty way to do it, but it works fine for most things!
Let's say you want to always run 'ls --color' instead of just 'ls'. Instead of messing around with .bashrc stuff, you can create a simple .bat file that essentially bootlegs the original ls command.
Here's what I did:
cd /bin
echo ls2.exe %* --color > lsNew.bat
mv ls.exe ls2.exe
mv lsNew.bat ls.bat
So now, whenever you type in ls from CMD, you actually are calling ls.bat, which in turn calls ls2.exe --color, the original ls command with the --color flag, along with the rest of the arguments, which are nicely passed through %*.
I had the same problem, but I was using ConEmu to run my console. I had to go into settings and change the settings from this :
set CHERE_INVOKING=1 & %ConEmuDrive%\Programs\Cygwin\bin\sh.exe --login -i -new_console:C:"%ConEmuDrive%\Programs\Cygwin\Cygwin.ico"
to this:
set HOME= & set CHERE_INVOKING=1 &
%ConEmuDrive%\Programs\Cygwin\bin\bash.exe --login -i
-new_console:C:"%ConEmuDrive%\Programs\Cygwin\Cygwin.ico"
Then it would work correctly.
It works as explained from cygwin:
Create a file ".profile" in your windows home dir. This will load every time when you start cygwin.
You can edit the file with your alias or you can source the .bashrc.
If you'll source, insert "source .bashrc" and save .bashrc also in your windows home dir.
Now you can start editing the .bashrc.
This is working for me On windows 10 with Cygwin64. Don't worry "kubectl" is just the program that I want to run when I type "k". restart Cygwin terminal after the change.
Smith#NB-Smith-3 ~ echo "alias k=C:/Users/Smith/kube/kubectl" >> $HOME/.bash_profile
changes this file
C:\cygwin64\home\Smith.bash_profile
I had same problem is why the path not is correct, the path correct is: D:\C++\cygwin\home\USER_WINDOWS.bash_profile

Opening files from Filezilla to console Vim?

Sometimes I have the need to modify files that are in a FTP server, currently I have Filezilla opening them in sublime. But I'm moving to VIM and I haven't found a way to make the file open in VIM console. Probably works flawless for gVim but anyone have this working on the console?
I managed to have a console pop up with vim by invoking a custom shell script wrapper instead of vim itself.
#!/bin/bash
gnome-terminal -e "vim $1"
One of the drawbacks is that everytime a new window will pop up. Hope this helps.
This topic is quite old but Vim stays the best ! So, I'd like to share my experience.
I'm using Guake Terminal and Vim v8 on a Debian 9.2 environment. The solution posted by #soulseekah is great but does not allow multiple files open in this configuration.
As the accepted solution, we will need a bash script. The difference will be that we check if vim is running. If no, run it. If yes, open the file in a new tab.
Here we go :
#!/bin/bash
if pgrep -x "vim" > /dev/null ; then
guake -e ":tabedit $1";
else
guake -e "vim $1"
fi
Downsides :
Need to be in normal mode to open new files.
Not working with gnome-terminal.
Enjoy guys !
I use Midnight Commander to do the same thing & it works with console Vim as well as a lot of very useful other commands.

Gnome-Terminal, how to start in a different directory?

Whenever I start my console gnome-terminal in Ubuntu, it starts in the home directory. How can I make it start in a different directory say ~/myfolder?
I tried to write cd ~/myfolder in ~/.profile but nothing happens.
If you start gnome-terminal like gnome-terminal --working-directory=myfolder it will start with the working directory at ~/myfolder so you could add a new entry to your menu to use that command instead of the other one.
I did this way - with script:
open 3 tabs in the same window size 170x40, each "tab" starts in a different directory.
gnome-terminal --geometry=170x40 --working-directory=myfolder1 \
--tab --working-directory=myfolder2 \
--tab --working-directory=myfolder3
Add the following to your ~/.bashrc
cd ~/myfolder
You could use the nautilus-open-terminal extension. This allows you to right-click on a folder in nautilus and open a terminal window with that directory as its working directory.
You can also run a terminal in the normal way, type "cd ", and drag a folder icon from nautilus to the window. This will paste the path of the folder into the command line and you then type return to change to that directory. You can do the same thing with regular files to paste their path and run commands on them.
Directory option
There is the option --working-directory to specify the startup directory of the terminal (no short option form).
The basic approach to open the terminal in /some/dir is
gnome-terminal --working-directory=/some/dir
but there is a trap...
Bad trap
Assuming we want to start the terminal in the directory ~/dir.
This does not work:
gnome-terminal --working-directory=~/dir
The command looks perfectly fine according to the option syntax, but the terminal starts in the home directory.
It's because it does not expand the tilde (~), for confusing reasons - see below.
Thesse do work:
gnome-terminal --working-directory=/home/auser/dir
gnome-terminal --working-directory=$HOME/dir
gnome-terminal --working-directory ~/dir
Tilde expansion
Note there is no = in the last variant. Because of this, the ~ is at the start of a shell word, and therefore is handeled by tilde expansion.
The problem is that ~ does not get expanded everywhere, but only in certain places. One of them is in variable assignments, like directory=~/dir. That's ok, ~ gets expanded to $HOME, but --working-directory=~/dir does not expand ~, because that is not a variable assignment, it only looks very similar.
Actually, this is how I turn it off for everyone by default.
gconftool-2 --direct \
--config-source xml:readwrite:/etc/gconf/gconf.xml.mandatory/ \
--set -- type=bool /apps/nautilus-open-terminal/desktop_opens_home_dir true
From GNOME Terminal - Getting Started:
You can also specify a command that runs automatically when you start GNOME Terminal in the profile.
If the folder has a complicated path, ie not just ~/myfolder, you could create a symlink to it in your home directory so you can get to it quickly. You can also set the CDPATH environment variable to tell bash to search a list of directories when you type cd myfolder.
To open in my desired directory as root user I ran:
gnome-terminal --working-directory=/home/my-project/ -x bash -c "sudo su"
I'm impressed by
Neil's Mayhew comment
and
Volker's Siegel answer. I've tried to not only set default directory for
gnome-terminal but preserve habitual behavior of desktop environment as well (I'm using Linux Mint 17.1 Cinnamon,
GNOME Terminal 3.6.2, perhaps it also can be applied for other Gnome-congenered DEs). So let me put my two cents in.
Adding cd ~/myfolder at the very end of ~/.bashrc does the job. But as already mentioned it will affect every
interactive shell. Even more, if you open some directory in a file manager (Nemo or Nautilus or something like
this) and appeal to the context menu from there (e.g. right click and then select Open in Terminal) new instance of
gnome-terminal will be started in ~/myfolder regardless of the folder which was loaded in the file manager.
Even if you run gnome-terminal --working-directory=/some/other/folder explicitly it will still open ~/myfolder. Seems that the
approach with .bashrc is unusable.
gnome-terminal --working-directory=myfolder works fine but only when you use custom menu entry in you DE
(or custom shortcut on desktop) which runs terminal with this parameter. If you would like to run gnome-terminal from command line
or from mini-launcher (press Alt + F2), you have to type the parameter every time. Anyway this approach is more-or-less usable.
How gnome-terminal determines which folder to open? When --working-directory is not specified it opens current
working directory (e.g. $PWD) otherwise it opens directory specified explicitly.
I've found the following solution.
Create a file named gnome-terminal in your ~/bin folder. It will act as shortcut but from everywhere (start menu,
mini-launcher, other terminal instance, etc) because ~/bin is already in $PATH (at least in Linux Mint...). Make this file
executable. Then put the following content into the script:
#!/bin/bash
home_directory=~
if [ "$PWD" == "$home_directory" ]; then
# When 'gnome-terminal' was ran from either
# - start menu
# - mini-launcher
# ...
# parent directory is set to $HOME.
#
# We respect original command line arguments.
# For example, when terminal is ran from another
# terminal instance and '--working-directory' is
# specified explicitly we should left it as it is.
# If there are two '--working-directory' switches
# in the command line 'gnome-terminal' will pick up
# the last one.
#
# Also we use full path to executable here in order
# to prevent recursive calling of 'gnome-terminal' from
# '~/bin'.
/usr/bin/gnome-terminal --working-directory=/ "$#"
else
# 'gnome-terminal' was from another directory.
# We don't change anything.
/usr/bin/gnome-terminal "$#"
fi
If you run gnome-terminal from start menu you will see that current folder in new terminal is / (you can use any folder, for example, ~/myfolder because our custom wrapper is a Bash-script, so shell's expansion with work fine).
If you appeal to Open in Terminal in a file manager you will get current directory in the new terminal.
If you run gnome-terminal with explicit --working-directory parameter (perhaps, from existing terminal instance) new terminal instance will be opened in the directory you specified.

Resources