Editing file on ssh with a local GUI editor
Posted by 03263@reddit | linuxadmin | View on Reddit | 48 comments
I'm looking for something that would save a bit of time with editing files on an SSH connection
My envisioned workflow is something like:
$ ssh hostname
$ cd /var/log
$ !local-edit $SSH_HOST $PWD/huge.log
Where $SSH_HOST is the hostname used to connect, as configured in ~/.ssh/config, and the local-edit command spawns a script like this:
if ! mounted $1; then
gvfs-mount $1
fi
$VISUAL ssh://$2
It would save the work of opening my file manager to mount the ssh connection, navigating to the file path and then opening it in my editor.
Does anyone have a setup like this they could share, or know of a tool that accomplishes it?
AmusingVegetable@reddit
“Probably the only person”…
Man/woman/dog/whatever, are you aware that 99% of open source exists because someone scratched a personal itch, and then shared it?
(And yes, I’m aware that it sounds like a skin condition)
nawcom@reddit
sshfs. I used that when I wanted to use pycharm but didn't feel like setting up version control
boomertsfx@reddit
sshfs was great 5+years ago when Notepad++ was cool, but there’s so much more you can do with the proper remote-ssh plugin
03263@reddit (OP)
That's basically what I already do, just with gvfs instead of sshfs (same result - ssh mounted as filesystem). What I want is to just save the steps of opening the file manager, mounting the ssh filesystem, navigating to the file and then opening it. Jump right to opening it from my ssh prompt.
nawcom@reddit
So you want to access a remote file via a local file editor, but launch the local file editor via a remote shell? Not launch the local editor, locally? Even if you did pull it off with your own implementation, it would require multiple sessions. Why not just locally mount the specific path that you need to access? You mention multiple steps with gvfs. A fuse mount is a single mount command in cli. You can use fstab if you wanted to. I wasn't talking about launching some file manager do anything.
03263@reddit (OP)
Yes. Well, in a way that the remote server wouldn't know about.
I was hoping there was some easy way to escape from an ssh session temporarily. Like prefix a command with an escape character - imagine if
!lswould list files in the directory you were in before you typedssh $host.I would already have the ssh fs mounted in this case so there are two sessions open, there's just no easy way to say "launch my local editor and send it this sshfs file path as an argument" from inside an ssh session.
megared17@reddit
I just use a terminal text editor directly over ssh.
03263@reddit (OP)
I do as well but often I want to open the file locally if I'm going to make significant edits or need to format it nicely
I'm working on something to achieve it and I'll update if I get it working.
Will probably require specific SshEnv set for each host, not quite on-the-fly local editing but works for 99% of my cases connecting to servers I already have configured.
Overall_History6056@reddit
At that point one should use git plus configuration management tools.
03263@reddit (OP)
Not every file on a server is under version control. Like log files of course aren't.
Overall_History6056@reddit
Of course. We are not talking about editing log files, right?
03263@reddit (OP)
Not to save them but often I use regex to knock out irrelevant lines and clean up formatting, often to put in a ticket or email to someone. Or just because it's so noisy I need eliminate some noise to make sense of things.
Using a GUI editor is also good for this because copy and paste just works whereas in a terminal you can get lines that continue off screen if there's no line wrapping turned on, and inconsistent copy/paste results.
megared17@reddit
Shouldn't be editing an *active* log file in place.
But I wouldnt use an interactive editor to filter one anyway, I'd use grep, sed, etc.
03263@reddit (OP)
It gets old editing a long command piping grep and sed together to create exactly the right output. GUI editor is interactive, can try things and undo... just overall better feel.
megared17@reddit
You know you can just hit up arrow to recall the last command?
And if it gets too complex, stick it in a text file and edit it there.
I do this all the time when writing either one-time commands, or scripts I intend to use again.
03263@reddit (OP)
I do that sometimes, it just gets cumbersome even editing the command and pasting it in. Depending on the server, I've met some that mess up pasted text with extra symbols. I'm sure there's a fix for that, I just haven't spent enough time on any single one to bother diagnosing it.
FortuneIIIPick@reddit
Agreed, OP should adopt using git.
GoodNameGone@reddit
Or sed/awk.
megared17@reddit
Fwiw, I'm not a fan of GUI editors anyway.
BarServer@reddit
Either that, or transfer the file to the target server. Afterall that's what scp is for.
Or use some kind of version control like git. Allowing you a central place to keep all files and edit them, and then just check them out on the target server.
Currently it feels like you are trying to solve the problem from the wrong end?
MisterBazz@reddit
What is your actual end goal. I’m still not sure what you are trying to accomplish. I agree with another poster - this sounds like an XY problem.
03263@reddit (OP)
Quickly and seamlessly use sublime text to edit a file that exists on a remote server, where I have an ssh connection open in a terminal, and the tools available to mount it as an sftp virtual file system.
MisterBazz@reddit
if you are editing the same file, in the same PATH, with the same data on multiple servers, this would be a good use of Ansible. It would be way more efficient.
03263@reddit (OP)
Rarely would need the same edits across multiple servers, if I do I will prototype changes on one server then deploy to all later (actually using pyinfra right now which is kind of like Ansible but simpler and made for idiots like me)
MisterBazz@reddit
You could keep all of these files in a repo and have Ansible push the updates to every server all at once. That's the point of Ansible.
Pendaz@reddit
I use vscode ssh plug for exactly this. I have multiple VMs configured for each client I work with. With an ssh connection from my main dev machine into each vm I can work as if the code is local seamlessly
ancientweasel@reddit
I use this extensively and sshfs too.
dmurawsky@reddit
This is how I do it when I have large files, or multiple files that I need to iterate on. Bonus points that you can open a terminal on the remote host easily as well.
butter_lover@reddit
can you not just write a script to make the same change on a list of hostnames and schedule it?
arcimbo1do@reddit
Autofs + sshfs, then you can open /net// with whatever editor and run grep locally if you want
I never tried that because autofs runs as a system daemon and sshfs is a fuse filesystem that runs as a user but i think you can make it work.
03263@reddit (OP)
I could actually just run a local shell in the mounted sftp filesystem, but most operations are very slow in that case, like grepping through a lot of files has to download and read each one.
I'd probably get mixed up and end up running some command on my local machine instead of the remote if I did that too much.
jadijadi@reddit
I also sometimes enjoy using -X with ssh. This way you can run graphical / GUI based apps on the server and run them but see the "results" (I mean window / ...) on the client side. Its called X forwarding
derprondo@reddit
Another option I regularly use so it works from my local file system and my IDE is to just mount the remote dir with sshfs.
03263@reddit (OP)
If you ID a file you want to open in a terminal ssh session, how do you quickly get from seeing the file path in the terminal to opening it in your editor?
derprondo@reddit
If you're doing this at scale you should be controlling this with config management, though. I would only ever do this kind of thing in my home lab or when doing dev work against a real instance.
pobrika@reddit
I guarantee I can log on to a remote server and edit the file quicker in vi before you've even got your editor open.
I have tried using a gui such as visual editor but it's so slow and cumbersome it's just not worth the effort. Saying that it might make sense if working on a project with lots of files in a tree which is why I looked into visual in the first place.
03263@reddit (OP)
I often am. I'll use grep -r or ripgrep if available to find some piece of code and then know which file I want to open, but I do want it open in my GUI editor... currently I just have to copy the path without the filename, switch to my file manager, open the sftp mount for the server I'm on, then paste in the rest of the path to the URL bar to jump directly into the directory, then start typing the filename to get to the right file and open it.
Yeah using a cli editor would skip all that, but I can't really customize it or use any plugins, I'd be stuck with the default settings. So even if I did use vim, I'd want to be using my local copy of vim and my config, not the remote copy.
mvdw73@reddit
Doesn’t ssh -Y do what you want to do? Or has that been deprecated in Wayland??
steakhache@reddit
with waypipe should work
xorian@reddit
Emacs has had this for decades. It works great. I use it all the time.
Ontological_Gap@reddit
Better workflow too: C-x C-f /ssh:user@host:/path/to/file from your local system.
Tho the tramp-theme package has been broken for me for years ...
CatoDomine@reddit
You can edit a remote file with
vimOr if you already have vim open you can use this command
note that an absolute path is specified with double forward slash
//and a file in your home directory is specified with a single slash/aenae@reddit
You could probably do this like:
Do your things, navigate around etc. If you see a file you want to edit, you do your command, and what it does is basically:
A few things explained:
The only thing i did not test is an editor (preferably one that has ssh support) or a script to mount the sshfs.
michaelpaoli@reddit
Why would you want/need a GUI editor? $VISUAL generally is not a GUI editor, but "merely" a screen-oriented text editor (e.g. vi).
So, what exactly is the problem you're trying to solve?
XY Problem?
If you want to locally edit a file, where the file is accessed via ssh, there isn't a direct way of doing that. You can use sshfs or the like, but regardless how you do that with local editor, the file will be copied from remote, to local, and back again - at least once - even with sshfs. But then again, most editors, even if done totally local, generally the entire file will get read and rewritten - either to same inode, or replacing that at same pathname with a new/different inode (precise behavior on that will depend upon the editor, and possibly also its configuration and/or options). Likewise for most editors, they'll copy from original file, to buffer, edit there, then write that back, to either same inode, or same path, new/different inode.
And if feasible, generally better to use remote editor, rather than copy file back and forth, but for one or more reasons, that may not be feasible or desired.
So, to just edit the remote, directly, e.g.:
$ ssh -t host 'vi file'
And can generally use vi or whatever editor available one prefers, and use the pathname of file as desired, or potentially cd first, then edit the file.
But if you really want to use local editor, then why not just copy it back and forth - if you use sshfs or the like that's going to be happening anyway. So, e.g.:
$ scp -p host:file file && vi file && scp -p file host:file
And can generally use whatever editor you want in place of vi, and do whatever you want/need for pathname(s) on file. Could even leverage mktemp(1) and make use of rm(1) too, to securely make a non-conflicting temporary file that you edit (your local copy), and then remove it after it's successfully copied back.
Why not just:
$ ssh -t hostname 'cd /var/log && vi huge.log'
or something like that?
And if you want to use remote GUI editor with X, can use ssh's -X and -Y options, e.g.:
$ ssh -fXY hostname 'gedit file'
And using whatever ewey GUI editor you want in place of gedit, and whatever pathname for file.
And if you really want the remote mounted via sshfs or the like, then all you need is a wrapper to first ensure it's mounted ... and might even be able to set that to automount (e.g. can be easily done with NFS, might be feasible to do likewise with ssh).
rowman_urn@reddit
Yes, ssh -X if you really want to edit remotely
03263@reddit (OP)
Mine is:
I just used it as a generic term for GUI editor.
justinDavidow@reddit
This could be accomplished using NFS pretty trivially, though I don't know why anyone would want that.
https://gist.github.com/proudlygeek/5721498
The "Install NFS Client" section would need to be updated based on your local client.
Once mounted, the file path can simply be "open"'d like any local file.
skat_in_the_hat@reddit
Try learning vim. Saves a bunch of time.