I have to edit some files placed on some server I could reach via ssh.
I would prefer to edit these files in customized vim on my workstation (I have not rights to change vim settings on remote server). Sometimes I would like to edit a file with sublime text or other GUI editor.
Of course, I can download these files, edit them locally and upload them back to server. Is there more elegant solution?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can do that via scp like this:
vim scp://<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3346405641735e4a405641455641">[email protected]</a>[:port]//path/to/file.txt
Notice the two slashes // between server and path, which is needed to correctly resolve the absolute path. (The first slash is syntactic, while the second slash specifies the remote user’s root directory, as usual. To start at the home directory, you’d do [:port]/~/path/to/file.txt.) [:port]is optional.
This is handled by vim’s netrw.vim standard plugin. Several other protocols are supported.
Method 2
You could do this by mounting the remote folder as a file-system using sshfs. To do this, first some pre-requisites:
#issue all these cmds on local machine
sudo apt-get install sshfs
sudo adduser <username> fuse #Not required for new Linux versions (including Ubuntu > 18.04)
Now, do the mounting process:
mkdir ~/remoteserv sshfs -o idmap=user <username>@<ipaddress>:/remotepath ~/remoteserv
After this, just go into the mounted folder and use your own local customized vim.
Method 3
Depending on what you mean when you say you do not have the rights to edit the Vim settings, there may be a way of using Vim on the server in the way you want anyway. If you can’t change your user .vimrc (because you’re logging in as a shared user, for example) but you can still create files, create it as a file called, say, Loom.vimrc and then call Vim using the -u switch:
vim -u ~/Loom.vimrc file_to_edit
You can even then use an alias: alias vim='vim -u ~/Loom.vimrc' will allow you to use Vim in the usual way, and it’ll still load your custom .vimrc file. This alias won’t persist after you log out, so you don’t need to worry about anyone else accidentally using your customised Vim.
Method 4
Multiple options are usable, not just “scp”; see: https://www.vim.org/scripts/script.php?script_id=1075
I like using “rsync” more, because for me “diff” didn’t work correctly with scp.
Example – start vim with the “/tmp/test”-file on different remote servers (hostname01.domain.my and hostname02.domain.my and …), using bash-extension, vertically split:
vimdiff -O rsync://hostname{01,02,03}.domain.my:/tmp/test
Method 5
Depending on how many files and what kind of files you are expecting to edit, this is maybe not exactly what you want to do here, but I think it’s worth mentioning. If you have to edit files in a remote server, but want to use everything you have in your own working station, then you may want to start thinking of using some kind of Revision Control system in your machines. That way, you can modify your local copies in your own machine using your software of choice, commit the changes, and then just update the local copies in the destination machine. Besides editing the files with whatever software you feel comfortable with, you have the added value of having a history of changes related to each file, which is always good.
Here’s a list of Revision Control Software, just in case.
Method 6
To expand on Mr. Potts answer:
You can also do the above, then put something like this in .bash_profile (or whatever your shell uses):
if [[ "$(who mom loves | awk ' { print $1 }' )" == "Loom" ]]; then
alias vim="vim -u ~/.Loom_vimrc"
fi
where Loom is your original userID that you login in as.
If you’re logging in as a shared account (and not an individual account then sudo su – ing, then may Von Neuman have mercy on your soul for you are lost.
I would have put this in a comment, but I couldn’t get the code formatted at all.
Method 7
If you have a vim sessions running already use
:silent e scp://[email protected][:port]//path/to/file.txt
The :silent in front will suppress the Press Enter to Continue message
and
e scp://[email protected][:port]//path/to/file.txt is the Ex mode command to edit the remnote file.
Tested with BitVise SSHD running on Windows 10, and using VIM runing on Ubuntu 16.04
Method 8
If you are more GUI-oriented and use one of the more newbie-friendly Linux distros like Ubuntu or Mint, this is another option and does not require any more installations.
You should have nemo as your default file manager. It may not be called “Nemo” on the menu, so go under Help > About of your file manager (“Files” app) to see.
In nemo, go to File > Connect to server, enter your remote machine’s details (SSH’s default port is 22), and then open the files just like any file on your local machine, with whatever editor you prefer. You can even close Nemo and continue working in your editor.
From the address bar, it seems to be using the sftp protcol.
Just be aware that if your remote host has an inactivity timeout for the SSH connection, this will also prevent you from saving changes in the editor after the timeout has dropped the connection…
Method 9
I quite often use something like the following
for i in hosta hostb hostc do ssh -t vim xx.txt done
so the trick is
ssh -t vim xx.txt
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0