Opening files from Filezilla to console Vim? - 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.

Related

open vim file in new unix terminal

How to open existing vim file from unix shell (bash) in new terminal (not in same/new tab of existing terminal) on local machine ?
Also is there any way to split file on new terminal (not in same/new tab of existing terminal) from inside vim ?
How to open a new terminal is platform dependent; This doesn't really have a whole lot to do with vim itself.
For example, if you're using GNOME you could do this by running gnome-terminal -e "vim $filename" & disown. Look up the documentation for the terminal emulator you're using to find out how to launch a new terminal and execute commands in it.
Another (IMHO much better) solution is to simply use GVim for situations like these, unless you have a very good reason to run vim in the terminal (if you're running this over ssh this won't work anyway, in that case you're better off using a terminal multiplexer like screen or tmux).
PS: bash isn't a terminal (emulator); bash is a shell. If you just run a new instance of bash it'll run in the same terminal, which is not what you want here.
Try this:
vim [your file]
If this isn't working for you, make sure you have it installed with:
sudo apt-get install vim
If you're already IN vim do
:edit [your file]

vimdiff immediately becomes stopped job, crashes terminal when I try to fg it, but works with GUI

SOLVED: the problem was this line in my .vimrc:
set shell=/bin/bash\ -li
UPDATE: it appears the problem is due to my .vimrc (renaming the .vimrc to 'hide' it fixed the problem), so it probably has nothing to do with Mint.
I'm running Linux Mint 17 Cinnamon. When I run vim -d file otherversion in the terminal, I get a blank screen with
"otherversion" 18L, 218C
[1]+ Stopped vim -d file otherversion
user#machine:~/path/to/directory$ ;2R
If I do fg 1 that terminal window just closes. Also, there's no blinking cursor, though I am able to type. Same with vimdiff instead of vim -d. I've tried multiple distributions of vim, some that include the GUI, some that don't.
However, with versions that do include the GUI, if I run vim -d -g file otherversion, it works as expected.
I'd rather not depend on gvim to use vimdiff.
Any advice?
Thanks
If you need an interactive bash shell (to get your bash aliases for instance), use this code in your .vimrc instead of removing the option:
if &diff == 'nodiff'
set shellcmdflag=-ic
endif
This will only enable the interactive bash shell when not running vimdiff, so that you will have you bash aliases when running vim.
After removing the following line from my .vimrc, the problem went away:
set shell=/bin/bash\ -li
Thanks to comment from #FDinoff, the same problem at my end got solved.
The problem was "bash" command in my ~/.cshrc.
I had put that to avoid entering into bash every time after login. It was working fine until this issue started coming whenever I used to open vimdiff.
Removing ~/.vimrc didn't help me either. Then I read comment from FDinoff. There, he suggested that it could be related to shell, if "less" and "man" have also stopped working. I observed the same issue in my case, that helped me rule out the corruption in .vimrc.
After removing "bash" from my ~/.cshrc, the problem went away completely.

How to run windows gvim from cygwin?

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.

Is it possible to "extend" IntelliJ such that I can open the current file in VIM

I use IntelliJ for most of my Java programming, but every once in a while I need to make certain changes which are much easier done in VIM.
Thus, the question:
While I'm editing a file in IntelliJ, it is somehow possible to tell IntelliJ to start VIM with the same file that is currently under cursor.
Thanks !!
Absolutely!
Go to File -> Settings -> External Tools and add a new one.
Program and Working Directory fields should be self-explanatory. The parameter field should be $FilePath$. Click the Insert macro button to see more token values that you could use.
You can also bind keyboard shortcuts to the external tools you define.
See the help page on configuring third party tools for more info.
Also, see the answers below with helpful screenshots for Windows or Mac.
Even though the original question is about VIM, I'd like to share my settings for GVIM. Especially useful is the parameter used to take it to the current line.
I find it useful to have Gvim open up the file AND take me to the current line. This is done using the following parameters: +$LineNumber$ $FilePath$.
When executed, Gvim should open up and take you to the line you were at in Intellij.
Here's how to set this up with the Homebrew version of MacVim (i.e. the mvim command) on Intellij IDEA 14.
Go to Preferences->Tools->External Tools and click the plus symbol.
Set up the new tool with these parameters
Program:
/usr/local/bin/mvim
Parameters:
+"silent! "$LineNumber$"|norm! zz" $FilePath$
Working Directory:
$SourcepathEntry$
For macOS 10.12+ users, If you want to open it in iTerm2, here is a latest solution:
#!/bin/sh
vim="vim $1"
osascript -e "
tell application \"iTerm\"
set myterm to (create window with default profile)
tell myterm
activate current session
launch session \"Default Session\"
tell the current session
write text \"$vim\"
end tell
end tell
end tell
"
Date: Apr 5, 2018
iTerm2: 3.1.5
IntelliJ IDEA: 2018.1
https://gist.github.com/backslash112/open_vim_in_iterm.sh
This was somewhat more involved on the Mac OS X (Lion), especially if you'd like to use Terminal.app
The only difference on Linux is the shell script would be much simpler... (really it'd be simple to not even use a shell script for this on Linux, with a terminal that accepts the -e argument!)
Here's my working solution:
#! /bin/sh
vim="vim $1"
osascript -e "tell app \"Terminal\"
activate
do script \"$vim\"
end tell"
I am using these parametrs for launching vim: +$LineNumber$ --remote-tab-silent "$FilePath$". It allows you to open each new file in vim's tabs. Works fine)
Go to Settings -> Tools -> External Tools
Click the + to add a tool.
Here are the settings I use:
Name: Vim
Program: /usr/bin/gvim
Arguments: --servername $ProjectFileDir$ --remote "+call cursor($LineNumber$,$ColumnNumber$)" $FileDir$/$FileName$
Working directory: $ProjectFileDir$
I turn off "Open console for tool output".
The arguments will use Vim in client-server mode, so if you jump to Vim from IntelliJ in the same project dir multiple times, the existing Vim instance will be reused, and they will also place the cursor in Vim exactly where it is in IntelliJ.
Try out the VIM Plugin, http://plugins.intellij.net/plugin/?id=164
I have experienced some bugs but for the most part it works.

Screen and Cygwin: no tab completion?

I'm having some problems working with my development environment. Specifically, after I invoke the screen utility from within Cygwin I lose the ability to do tab completion. Before I invoke screen however tab completion works just fine.
I messed around with setting different values for the TERM env variable (VT100, xterm etc) but without success. It must be something trivial but I have no idea anymore. Does StackOverflow have any suggestions for me?
when you issue 'screen' from inside cygwin it might put you in another shell like /bin/sh instead of /bin/bash (and bash is where you're getting the tab completion from).
To fix the problem you could edit your .screenrc file (found in your home directory) and add in this line:
shell bash
Then try running screen again and you should see tab completion work within this new window.
The problem is that bash needs to be run as a login shell in order to have tab completion in the default cygwin setup. If you run bash in a cygwin bash you won’t have tab completion either. To set screen to run bash in login mode, add this line to your ~/.screenrc file:
shell -bash
I had a similar problem with git autocompletion not working when using screen on a linux machine, but wasn't due to a different shell. I fixed it using this question: Git autocomplete in screen on mac os and doing the following:
Get the git autocompletion script
curl https://raw.github.com/git/git/master/contrib/completion/git-completion.bash -OL
why would you want that hanging around?
mv git-completion.bash .git-completion.bash
add this line to your ./bashrc
source ~/.git-completion.bash
Then in your terminal
source ~/.bashrc
That worked for me.
(I imagine after three years you've probably solved your problem, but I hope this helps someone else)

Resources