Remotely Programming - vim

I'm doing my development work on my Windows machine, but my compiling on a remote Linux machine. What I currently do is start an X server on Windows, ssh into the Linux machine, then do the development remotely.
What I'd like to do is edit my source on the Windows machine, and have it automatically copy files over to the Linux system when I save. I'd also like for my built-in compilation commands to perform a build on the remote system.
If it makes a difference, the source is all in C, using GCC. In descending order of preference, I have Emacs, Vi, and Netbeans on my desktop, and am willing to install another IDE for a last resort.

This is certainly doable in vim. You can use the scp:// protocol within vim to edit remote files, and set up a command that writes a local copy. You can also change what program vim uses for :make to do an ssh make instead on your server.
You'll need to set up your ssh-keys to keep this painless (otherwise you'll be entering your password all the time) but that's fairly easy.
Another alternative would be to push to a remote repos as part of your make command, instead of editing remotely.
EDIT:
First, using the scp:// protocol within vim. From :help netrw-start (or down the page from :help scp)
Netrw supports "transparent" editing of files on other machines using urls
(see |netrw-transparent|). As an example of this, let's assume you have an
account on some other machine; if you can use scp, try:
vim scp://hostname/path/to/file
Want to make ssh/scp easier to use? Check out |netrw-ssh-hack|!
You can also use scp:// paths in :edit commands, or really anywhere that you could use a normal path.
And, from the mentioned :help netrw-ssh-hack, instructions on how to set up your ssh keys:
IMPROVING BROWSING *netrw-listhack* *netrw-ssh-hack* {{{2
Especially with the remote directory browser, constantly entering the password
is tedious.
For Linux/Unix systems, the book "Linux Server Hacks - 100 industrial strength
tips & tools" by Rob Flickenger (O'Reilly, ISBN 0-596-00461-3) gives a tip
for setting up no-password ssh and scp and discusses associated security
issues. It used to be available at http://hacks.oreilly.com/pub/h/66 ,
but apparently that address is now being redirected to some "hackzine".
I'll attempt a summary based on that article and on a communication from
Ben Schmidt:
(1) Generate a public/private key pair on the local machine
(ssh client):
ssh-keygen -t rsa
(saving the file in ~/.ssh/id_rsa as prompted)
(2) Just hit the when asked for passphrase (twice) for no
passphrase. If you do use a passphrase, you will also need to use
ssh-agent so you only have to type the passphrase once per session.
If you don't use a passphrase, simply logging onto your local
computer or getting access to the keyfile in any way will suffice
to access any ssh servers which have that key authorized for login.
(3) This creates two files:
~/.ssh/id\_rsa
~/.ssh/id\_rsa.pub
(4) On the target machine (ssh server):
cd
mkdir -p .ssh
chmod 0700 .ssh
(5) On your local machine (ssh client): (one line)
ssh {serverhostname} cat '>>' '~/.ssh/authorized\_keys2' < ~/.ssh/id_rsa.pub
or, for OpenSSH, (one line)
ssh {serverhostname} cat '>>' '~/.ssh/authorized\_keys' < ~/.ssh/id_rsa.pub
You can test it out with
ssh {serverhostname}
and you should be log onto the server machine without further need to type
anything.
If you decided to use a passphrase, do:
ssh-agent $SHELL
ssh-add
ssh {serverhostname}
You will be prompted for your key passphrase when you use ssh-add, but not
subsequently when you use ssh. For use with vim, you can use
ssh-agent vim
and, when next within vim, use
:!ssh-add
Alternatively, you can apply ssh-agent to the terminal you're planning on
running vim in:
ssh-agent xterm &
and do ssh-add whenever you need.
For Windows, folks on the vim mailing list have mentioned that Pageant helps
with avoiding the constant need to enter the password.
Kingston Fung wrote about another way to avoid constantly needing to enter
passwords:
In order to avoid the need to type in the password for scp each time, you
provide a hack in the docs to set up a non password ssh account. I found a
better way to do that: I can use a regular ssh account which uses a
password to access the material without the need to key-in the password
each time. It's good for security and convenience. I tried ssh public key
authorization + ssh-agent, implementing this, and it works! Here are two
links with instructions:
http://www.ibm.com/developerworks/library/l-keyc2/
http://sial.org/howto/openssh/publickey-auth/
For making on remote systems, you need to set your makeprg variable to
do an ssh make. From :help makeprg
Program to use for the ":make" command. See |:make_makeprg|.
This option may contain '%' and '#' characters, which are expanded to
the current and alternate file name. |:_%| |:_#|
Environment variables are expanded |:set_env|. See |option-backslash|
about including spaces and backslashes.
Note that a '|' must be escaped twice: once for ":set" and once for
the interpretation of a command. When you use a filter called
"myfilter" do it like this:
:set makeprg=gmake\ \\\|\ myfilter
The placeholder "$*" can be given (even multiple times) to specify
where the arguments will be included, for example:
:set makeprg=latex\ \\\\nonstopmode\ \\\\input\\{$*}
This option cannot be set from a |modeline| or in the |sandbox|, for
security reasons.

One option is to use the TRAMP remote-editing package (built into Emacs 22 and newer, and you can install it into older versions). Every time you save your file, Emacs sends its contents over ssh (by default; of course every detail is totally configurable) to the Linux machine. Commands like M-x compile and M-x grep are TRAMP-aware and execute on the remote host.

I would look into continuous integration for your environment. This way you can commit the changes to source control, and have the linux box act as a build server. You can have tests associated and other related interesting stuff you want to be run on the builds.
Update 1: Also this might work for you: http://metamod-p.sourceforge.net/cross-compiling.on.windows.for.linux.html (it is also worth a try doing some searches on similar tools)

Other have suggested SAMBA which may not be feasible on your Linux box. A good alternative is to use Dokan SSHFS on your Windows box to mount a remote directory over SSH.

You could try sharing a disk between your Linux and Windows machines using Samba or something like that. Then you could edit on your local machines and the files would be visible immediately on the remote machine since the drive would be visible to both.
Where I work we have all files on NFS that is accessible from all Linux machines and Windows machines. I don't know how hard it is to set that up since I work in a large corporation and IT is abstracted away from me, but simple disk sharing should be pretty straightforward.

Why do you start an X server on Windows? Personally, I would set up a Linux VM with VMware or whatever your favorite VM technology is (VMware is free and works well). Then choose any Linux distribution you want. You just need very basic functions, mostly the standard "toolchain." You could pick Centos, Ubuntu, Fedora, Debian, whatever. I usually use Centos or Debian. Set it up, and just use PuTTY into your VM. From there, you can scp files to your remote server and so forth. This way you don't have to bother with cygwin or an X server or any of that.

Can you just use a samba share to save the files directly on the remote machine? I often do PHP this way.
Then just have a putty window open to run commands on the remote box.
Or am I missing something?

Set a source control system and use it. Then you can just make a commit after saving in your IDE, and on server you can have something happening on-commit.
This can trigger tests, build, mail any errors to you...

One solution might be to have some sort of polling app that checks the timestamp on the files to see if they have changed. If they have then get it to save and then compile. Kinda hackish this way but it would be workable.

I personally use XMing with PuTTY. I ssh using PuTTY while XMing is running. I can open up any editor (gvim, emacs, gedit, etc) and it will appear.
You will need to do some setup on PuTTY though:
Expand Connection
Expand SSH
Click on X11
Check the "Enable X11 Forwarding"
In the text field for display location, enter (without quotes): "localhost:0"
Save session and connect.

Related

rcp Vs rsync prompting for password

This might be a repeat question but exact explanation I am not getting. I am new to LINUX environment.
I was trying to copy files from one system to other system. If I issue rcp command it copies without asking for password but if I issue rsync command then it asking for permission. Can you please explain why?
rcp is an ancient and insecure program for copying files between remote machines. Avoid it. scp (of the ssh suite) is the secure, modern alternative.
rsync is a relatively recent program. When connecting remotely, it uses ssh in the background which, being secure, is what is asking for credentials.

How to copy files on remote Windows to local Linux on a Linux machine?

Currently, I'm working on a local Linux machine. I'm trying to use scp or similar Linux command-line tools to copy files from a remote Windows machine to my local Linux. I did some searching and found that most of the solutions are for local Windows cases (like putty and winscp), which don't really help.
Please advise.Thank you.
[UPDATE] Solved by installing cygwin's sshd service on Windows.
If you really want to use SCP, you will need an SCP server (actually an SSH server) on the Windows machine.
For example freeSSHd.
You will need to choose one of the options based on your own needs, there are a number of similar tools and freeSSHd was the first in the list on Google. I've used the Bitvise SSH Server in the past but it is only free for non-commercial use.
They are usually very easy to set up. You install them the usual way and run them for the first time. Depending on the tool, they may pick up your existing Windows users or you may need to manually create some users with passwords within the tool. Then, armed with your PC's IP address, you should be able to connect to the PC using SSH from the Linux command line.
If the windows system has a shared folder you should be able access that with smbclient which is part of the terrific samba project.
Usually somthing like:
smbclient //winmachine/share
Possibly using the -U username options to specifiy the username on the windows box.
Once connected, you can use cd to change folders, and get to retrieve files.
If there is no file share.... I dunno. Create one?
Syntax for copying from remote Windows 10 machine with built-in SSH server. Note forward slashes and drive style. Domain is not necessary.
scp user#domain#example.com:c:/path/to/file.txt .

Is there a way to have Vim reconnect to a different X server?

I am using Vim inside tmux over an ssh connection on a remote machine. I access the remote server from several locations including work and at home. This usually causes stale $DISPLAY variables and associated problems, most of which I have resolved except for one:
If I have a vim session already opened, is there a way to change which X server it is connected to without closing and re-opening it? I need to be able to do this so I can copy snippets of text from Vim using the X clipboard. I normally keep multiple vertical splits, linebreak and line numbers on, so doing this through tmux is usually a giant hassle and would prefer to do it with vim.
You can use xpra to display a running X11 program (specifically, vim) on different X11 servers.
This program functions for X11 programs like GNU screen or tmux do for terminal programs. xpra provides an X server for running programs and itself acts as an X client for the X server for the user's display.
You mention that you are running vim through tmux; you should maybe run vim outside of tmux and use xpra directly. As far as I know the program has to be run through xpra from the start.
There's a relatively new command called xrestore which does this:
:xrestore :0
https://groups.google.com/g/vim_dev/c/G54XTJHAqXI
https://github.com/vim/vim/issues/3649
If the sole purpose of using X is to transfer large amount of text, consider using scp instead.
From within vim mark the lines you're interested in and do :w /tmp/foo
From a shell (or suspend vim using ctrl-z ; bg) do
scp your_username#remotehost.edu:/tmp/foo /some/local/directory
I added the following line to ~/.tmux.conf on the server and reloaded the tmux sessions there. This one-liner does the trick of automatically attaching to the local X-server when I connect to the tmux-session from multiple local clients using SSH. Each local client has its own X-server (X410 in my case).
set-option -g update-environment " DISPLAY"
How it works? According this post, the line is supposed to refresh the $DISPLAY variable for all panes. Here is another related post.
Setup: all my local clients are Windows 10 machines, running WSL (Pengwin distro as WSL2). My X-server is X410 downloaded from the Windows Store. The server that hosts the Tmux+vim sessions is a Linux Mint machine. On the server, the Tmux version is 3.3a.
Per my workflow, this means a lot. Especially that I don't have to repeat the following procedures: 1. SSH from another local machine; 2. Finding that clipboard won't work; 3. Save the work, exit Vim; 4. Start a new pane in the current Tmux session, and 5. Start Vim again, trying to load back to where I was.

Call external script within chroot environment

I use a chroot development environment for developing software for devices. The chroot dev environment isolates the rest of my system from my build-system hacking. The chroot environment is text-based, but I prefer to use a graphical text editor. Right now, I keep one terminal chrooted into the dev environment to build packages and one terminal pointed at the chroot environment from the outside to edit files.
I'm tired of constantly switching back and forth between these terminals, but I don't want to install X and Gnome on my compact dev environment for obvious reasons. I need a way to forward certain commands to the exterior environment, but I can't think of a simple solution. How can I execute a command on the exterior system from within a chroot environment?
Yes, SSH can be used, but without X forwarding, because you want to run the app outside of chroot, not inside. This means you have to tell the app where is its X server, because SSH won't do it for you. It is done by setting DISPLAY environment variable prior running any X app to the same value as your non-chrooted terminal has, usually it is:
export DISPLAY=:0
You could SSH into your own system, enabling X forwarding, and set it up with keys so no password is required. At minimum, something like:
ssh localhost -c my-graphical-editor
When an account is chrooted, everything that you need has to exist in the chroot / environment. That means /usr, /opt/, etc., has to be "local" and populated with whatever code is required. Graphical interfaces typically require a boatload of support code.
You may personally prefer a graphical interface but is it necessary? Or more correctly will it compromise the jail; make it easier to break out of jail?
You can su back and forth pretty quickly...

Way to use vim work on a cache of a file instead of the actual file?

I typically ssh to another computer to do my development, using vim (don't post "Use Emacs" please). However, I notice that vim is very slow when my internet is slow (duh).
EDIT:
I use a terminal on my local machine and open the remote file with a vim scp://host/file command. However, when I do this, every keystroke that I put in causes vim to go to the network, dramatically slowing things down. Instead, I want vim to read the remote file, let me do my local editing, and only go to the network when I do a :w command. Think of it like a write-back cache option.
I know Komodo Edit offers this functionality, which is where I got the idea from.
You can edit from ssh directly in vim like this:
:e scp://remoteuser#server.tld//path/to/document
You can find more information in this serverfault question.
Vim has a plugin (which I believe is standard on most recent vims) called netrw. This basically does what you want I believe: it allows you to open a file on a remote server by copying it transparently.
See :help netrw or The Vim Script page
If you open vim scp:/// and it is slow, and there is network traffic on every keystroke, then something is definitely wrong. Vim should generate network traffic only on save.
Perhaps you turned on autosave? If yes - turn it off for remote files, or at least make it less intrusive.
I find sshfs very useful for this. It can mount a remote directory over sftp, and then you can run vim (or anything else) locally. On current Ubuntu you can sudo apt-get install sshfs.
I don't know if it is possible to do that in vim itself, but if you're using Linux you may be interested in sshfs. It uses FUSE (Filesystem in Userspace) to mount a remote directory into a local one using ssh, and you can then use this remote directory as if it were on your own local computer. It is very robust and reliable (if your ssh session is closed, it will reconnect automagically and you probably won't even notice it...).
I use it almost every day, and it's really a great piece of software.
:e scp user#host:/path/file
See :help netrw and :help ssh in Vim.

Resources