Transparently edit remote files on Windows, with ssh/Putty and netrw - vim

Ok guys, maybe this one has been asked before, but I searched and ran threw nothing, so i'm taking a chance here.
I'm using the latest vim (gvim 7.3), on Windows 7 64bits.
I've got some remote files that I want to edit directly with vim, using netrw.
Right now, I've got a fully working transparent ssh connexion to my remote hosts, thanks to Putty, Pageant and public/private keys.
I've successfully setup a read/write access to distant files with those fixes:
#### .vimrc ####
let g:netrw_cygwin= 0
let g:netrw_scp_cmd = 'c:\"Program Files (x86)"\PuTTY\pscp.exe -q -batch'
let g:netrw_sftp_cmd= '"c:\"Program Files (x86)"\PuTTY\psftp.exe'
Then I can access a file with :
:e scp:\\user#host:port\\home\me\some-file.txt
And, each time I access remote file, Vim run a windows prompt (cmd.exe) :
C:\Windows\system32\cmd.exe /c c:\"Program Files (x86)"\PuTTY\pscp.exe -q -batch
-P 22 "C:\Users\me\AppData\Local\Temp\VIF215E.tmp" "user#host:/home/me/some-file.txt"
Hit any key to close this window...
My problem is that hitting a key outside Vim each time I want to open/write a file isn't efficient at all.
So my question(s) is(are) :
Am I doing it the right way ?
Is there another way of transparently
accessing a remote file with ssh on
Windows ?
If no, is there a way to
get rid of "Hit any key to close this
window..." when Vim launches putty's pscp.exe ?
Thanks a lot and happy vimming.
EDIT: Note to the myself in the past: Dude, just go with linux and vanilla gvim/ssh. You'll thank me later (and take a look at spf13-vim)

These are what I got working today (gVim 7.3 in Win 7) (took me many hours to finally get something working):
set nocompatible
let g:netrw_cygwin = 0
let g:netrw_list_cmd = "plink.exe -P ##### -pw MyPass user#host.com ls -Fa "
let g:netrw_ssh_cmd = "plink -T -ssh"
let g:netrw_scp_cmd = "pscp -P ##### -pw MyPass -scp"
let g:netrw_sftp_cmd = "pscp -pw MyPass -sftp"
or
let g:netrw_sftp_cmd = "psftp -P ##### -pw MyPass user#host.com"
With the first command make sure there's a [space] after -Fa or you'll get errors.
Then you can connect in Vim via
:e scp://user#host:port#/(path to file)
:e sftp://user#host:port#/(path to file)
I hope this helps some Vimmers struggling to figure this out and get it working.
Cheers!

Just put this line in your vimrc:
let g:netrw_silent = 1
This will run scp asynchronously (like using !start)

This seems a bit too elaborate.
I use SSHFS on Linux to accomplish this. It allows you to locally mount a remote path over SSH.
Something similar is available for Windows now as well: http://dokan-dev.net/en/. It should remove the need for all the modifications on the Vim side.

Instead of
let g:netrw_scp_cmd = 'c:\"Program Files (x86)"\PuTTY\pscp.exe -q -batch'
I had to use
let g:netrw_scp_cmd = 'c:\"Program Files (x86)"\PuTTY\pscp.exe -q'
In windows 7, putty version 0.6, -batch option was not working!

WINSCP is awesome. I think it copies a temp file locally then FTPs it but it is "transparent" and is very user friendly.

I find it's a lot easier to just run cygwin. Install openssh and gvim in cygwin, then run:
$ gvim scp://user#host//path/to/file
Note the double slashes between the host and the path. Without the extra slash, it'll read the path as relative to the user's home directory.

Related

ssh No such file or directory

I had to wipe out my Windows OS. I went to check if I had ab ssh still and there was none so I created one.
Went through the proper steps and even got the agent ID.
Now when I try and find the id/rsa/pub using bash it tells me no file or directory.
But I can find that ssh file using my file explorer.
Trying to get ssh keys to reload up to my GitHub and Heroku.
After searching stackoverflow I did find an article saying to run the command env|grep HOME and make sure HOMEDRIVE=C: was set to HOMEDRIVE=C:Users/Samson/ but mine is not.
If that is the correct fix how do i set HOMEDRIVE=C: = to Users/Samson/
I am on a Windows Machine
If that isn't the correct fix, I'm open to suggestions. I am extrememly green to this.
Problem
There are two problems in the attempt to display the ssh public key, shown in the screenshot:
No command is used, the file path is entered directly. The command cat may be used for this purpose.
The file path is incorrect: id_rsa/pub instead of id_rsa.pub.
Solution
In order to view the public key file content, try the following command in bash:
cat ~/.ssh/id_rsa.pub
Otherwise, you may simply open the file from windows explorer, using a text editor (e.g. notepad).
You have to generate key first
Use this command to generate key
ssh-keygen -t rsa -b 4096 -C "youremail#example.com"
Enter the above email which you have in you github.
And now press enter and then you key will generate, and you will be able to acquire you ssh key
This command --
ssh-keygen -t rsa -b 4096 -C "youremail#example.com"
I am on a Windows Machine
Its okay to be on a Windows machine, but you should run these commands from a Git Bash window and not a windows command prompt or an anaconda command prompt. That maybe your real problem.

How do i make my .bat file run linux command to remote linux

Below is my current .bat content. i run it on window cmd. it will connect to remote linux server and prompt me password. but after i put the password and login as remotehost, linux server wont run my ls command. please help.
#echo off
ssh remotehost#10.1.1.10
ls
You really should do man ssh as this is explained there (and you could also make an internet search to get an answer).
But, to answer your question anyway: you should put all commands you want to run on the remote machine on the same line with the actual ssh command, for example to run directory listing and grep all files containing "foo", do: ssh <user>#<host> 'ls|grep foo'.
I hinted that it is possible to have the code in a batch file in my comment to #Sami Laine. This is what it would look like:
#echo off
setlocal
:: Run the end of this file in remote computer
more +8 %0 | plink user#remote.compu.ter "tr -d '\r'| bash"
endlocal
exit /b 0
:: remote bash stuff to be bootstrapped
pwd
ls -h
I'm using plink, because that what I have installed but it should work with most flavors of ssh too. Works also with ksh and zsh. Probably also with tcsh csh etc. This can sometimes be useful. Same technique can be used for a lot of things. Be careful with the +8 offset value it has to be on the right line.

Auto-rsync when saving file on VIM not working because rsync prompts password

I'm configuring VIM to execute a rsync after saving a file, synchronizing my server with my dev laptop. It doesn't work because rsync prompts for my password, even if I've already set up a public key login. How to fix this?
If you are using ssh-connection with rsync, you can always set up a passwordless connection for it. [Here][1] are some nice tutorials. It can be done on windows too, via PuTTY, PLink, Pageant, etc. Read the docs.
Instead of using vim for this, how about using incron? It ships with most modern Linux distributions and will sync any changes, not just those you do in vim.
Explicitly define which ssh-key you are to use with ssh -i:
:nnoremap <expr> <D-s> ':w!<cr>' . ':!rsync -r -e ssh -i /home/user/.ssh/id_rsa ~/Folder/* user#my_ssh_server.com:~/Folder/ &<cr>' . ':!osascript ~/applescripts/chromereload.scpt &<cr>'
Or you could you try to set your shell variable to interactive, i.e. use ~/.bashrc++ (man bash). Not really sure if it has any effect.
:set shell=/bin/bash\ -i
And/or define an alias in your ~/.bashrc if needed. Maybe combine it with whatever chromereload.scpt does.
alias vimsync='rsync -r -e ssh -i /home/user/.ssh/id_rsa ~/Folder/* user#my_ssh_server.com:~/Folder/'
More information:
vim -- not recognizing aliases when in interactive mode?
I haven't tried this but it should work:
Put this into your ~/.ssh/config file:
ControlMaster auto
ControlPath ~/.ssh/cm_socket/%r#%n:%p
ControlPersist 300
KeepAlive yes
ServerAliveInterval 60
Create the directory ~/.ssh/cm_socket and ssh into the server from another terminal. Now when you ssh into the server again it should use the same socket and no password is required, so your rsync command should work without password.

Using vim to remotely edit a file on serverB only accessible from serverA

Although I have never tried this, it is apparently possible to remotely edit a file in vim as described here. In my particular case the server I need access to can only be accessed from on campus, hence I have to log into my university account like so:
ssh user#login.university.com
then from there log into the secure server like so:
ssh user#secure.university.com
I have keyless ssh set up, so I can automate the process like so:
ssh user#login.university.com -t "ssh user#secure.university.com"
is there anyway to remotely edit a file such as secure.university.com/user/foo.txt on my local machine?
EDIT:
My intention is to use vim on my local machine as it is impractical (move .vim folder, copy .vimrc) and in some cases impossible (recompile vim with certain settings, patch vim source, install language beautifiers) to make vim on the remote machine behave the way I want it to behave. What I want is to issue something like this (this is not accurate scp, I know)
vim scp://user#login.university.com scp://user#secure.university.com//home/user/foo.txt
OK after a little working around I figured it out. First you have to edit (or create) your .ssh/config file as described here. For our purposes, we will add a line like this, which essentially adds a proxy.
Host secure
User Julius
HostName secure.university.com
ProxyCommand ssh Tiberius#login.university.com nc %h %p 2> /dev/null
Then we can simply copy (via scp) the file secure.university.com:/home/Julius/fee/fie/fo/fum.txt to the local computer like so
scp secure:/home/Julius/fee/fie/fo/fum.txt fum.txt
Extending on this, we can load it into vim remotely like so:
vim scp://secure//home/Julius/fee/fie/fo/fum.txt
or using badd like so:
:badd scp://secure//home/Julius/fee/fie/fo/fum.txt
To simplify my life, I added this shortcut to my .vimrc file for the most commonly used subfolder:
nnoremap <leader>scp :badd scp://secure//home/Julius/fee/fie/fo/fum.txt
So far vim has proven to be pretty aware that this is a remote file, so if the C file includes a file like so:
#include "foo.h"
it won't complain that "foo.h" is missing
Once you SSHed in the machine you can run any command(also vim) in remote host on your shell. After logging run vim as you are running in your machine.
Since you are using ssh, you basically have access to the server via the CLI, as if you were sitting in front of the machine itself. With that said, you can use any program on that machine, just like you would use it on your own machine. Assuming that the secure.university.com/user/foo.txt means that there is a text file called foo.txt at location /user on the secure server, then the following commands would work after logging in through ssh:
cd /user
vim foo.txt
You could also use nano or any other CLI based editor that is installed on the machine.

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.

Resources