save on gedit remotely - linux

On a machine I don't have physically access to, I left gedit open with a text file I forgot to save. I can ssh to that machine. The OS running there is Ubuntu.
Is there any way I can save that file remotely?

Look at xdotool, which can programatically activate/move/resize windows, and simulate keyboard input and mouse activity.
xdotool search --classname gedit key ctrl+s
This will search for all of gedit's windows and press ctrl+s in them.

Yup! I can think of two straightforward ways.
If you have SSH access and root privileges, you can tunnel in and install and configure a remote desktop viewer (or use the default VNC, vinagre). You can then connect to your desktop, find the gedit window and literally press "save."
You can use X forwarding over SSH to forward the gedit window to your local machine, where you can also just press "save." Note that you'll have to change the X display of the gedit instance so it's forwarded. This may be slightly tricky, but you can give it a try. You'll find many guides to X forwarding with a simple search.
There's definitely a hackish way to take the contents gedit is holding in memory and write them to file, but I think using a VNC client is a much, much simpler option.

Related

Edit files in server using text editor

I was wondering, is there a way I can edit files inside a live linux server using a text editor? I am able to edit using the nano command but that is not very friendly. Can I achieve the same using an editor.
vi is an editor. If you mean graphical editor, then it depends. At first glimpse, you have two options: A) Open the file using some file share (nfs, samba, ftp?) on your client computer using your favourite editor, or B) Use some ssh client which is capable of "tunneling" an X connection (putty?) and use an editor running on the server machine, displaying GUI on your client (which acts as an X host).
You can install a better editor, like vim or emacs on your Linux server.
You could also install X-client editors like gedit (and many others) and use ssh -X to access your server. You need some X11 server in front of you (on your laptop).

Copy and paste content from one file to another file in VIM from remote machine

I have found lots of questions about coping content of a file in Vim from one file to another and of course there are various ways to do it (1-2). I'm working on a remote machine and I'm going to copy large amount of data from a file in Vim to my laptop. None of the methods I have found yet works for coping from a remote machine. I have to use scp for any thing that I need from the remote machine. Do you have any idea?
I can't use "*100yy to copy content and remote machine does not have
any graphical editor. – Abolfazl
Naturally, if the editor has no notion of a GUI or a window system, it cannot use the clipboard, let alone that of your networked laptop. One option is to use the copy function of the local terminal program where you enter ssh, but of course that is practically limited by the terminal size.
If your laptop runs Windows, I recommend using WinSCP instead of ssh and the remote vim - you can configure WinSCP to edit the remote file with your local gvim, with all its features.
In your terminal:
$ man ssh
/-X

VIM support to upload via SFTP

I am used to Sublime's plugin that let's you do a mapping between your local project and a remote project. By doing so, when you are done editing a file, you can do a right click and "Upload file" action which automatically updates the file in the remote server.
I am wondering what would be the best way to do this while programming with VIM. I am looking for an easy way to be able to map a local project to a remote one and be able to upload the file easily.
Thanks
If all you want is to edit files on a remote server,
try vim's netrw feature:
vim scp://you#yourserver//path/to/directory/
Edit a file directly:
vim scp://you#yourserver//path/to/directory/somefile.txt
Or if you are already in vim, hit ESC, then type:
:e scp://you#yourserver//path/to/directory/
This assumes that:
you have ssh installed (usually the case for Linux or OSX; if you are on Windows then it depends).
you have ssh access to the server via some convenient method (sshagent and keys)
Most serious FTP clients let you edit remote files locally. Filezilla, gFTP, Transmit, Cyberduck, YummyFTP… they all have their own "Edit in…" button. GUI FTP clients may not be sexy hacker tools but they work very well.
Some of those serious FTP clients have synchronisation features or folder watchers. It might be worth your time to look around for such a feature.
Of course, you can also use Vim itself to edit a remote file via FTP or SFTP:
$ vim sftp://user#machine/filename
$ vim ftp://user#machne/filename
and list remote files:
$ vim sftp://user#machine/directory/
See :help netrw for more info.
Did you consider using a VCS like Subversion, Git or Mercurial?

Automated Context Switching Between Projects via SSH and Multiple Terminals

I'm looking for some guidance on an approach to automate the process of quickly switching between projects.
Putty Windows (2 of them):
Vim with project.session open (multiple tabs)
IPython open to the working directory of the project
What I want to automate in its respective windows:
Save all files, save session, close vim, change working directory of vim to next project, open related project session.
Close ipython (as I often restart it during development and want a fresh instance), change directory to next project'ss location, open a new ipython
A magic solution would be a single command that would change the state of both putty windows. However, since I have no idea how that would be done my current approach would be:
Attempt to figure out how to bind all that Vim stuff to a key press or a custom vim script that is used like: :SwitchProj "projectName"
Write a bash script or find a bash command that will allow me to type as little as possible to perform the directory switch and open new ipython interpreter.
As I have very little experience on Linux and SSH I would be interested to know how other's have solved this problem or would approach it!
I believe that the Linux screen command should satisfy you. Google gives you many tutorials, like this one.
I sadly confess that I am not very familiar with screen. Learning it is in my todo list
If you use the Gnu Emacs editor, you can also open many shell buffers inside (and other interactive buffers, like gdb sessions, compilations, grep search) and edit many files. I do that very often. You can use emacs thru a tty interface, such as provided by ssh.
You could also use ssh with e.g. -X to also redirect X11 windowing. For you, that means that you'll need to run an X11 server on your local Windows machine.

Open MacVim via Terminal when SSH'ed to a server

I'm wondering if there is a way to have MacVim become my default editor when I've SSH'ed into a server. Is there any way for the server's .vimrc file to launch a program on my Mac?
I ask because occasionally I'll be getting some help from our programmer on a project, and if he logs into my account from his computer and opens vim using my configuration the color scheme is usually pretty harsh since he isn't using a terminal that supports 256 colors. Would it be easier to have my .vimrc file set up so if it detects a gui (e.g., I'm using iTerm instead of Terminal.app) it loads the colorscheme I prefer?
Thanks for any advice!
Here's a way to use MacVim to edit files on your server, but it requires you to start MacVim on your local computer first.
First make sure you have netrw installed. Check :help netrw to confirm that it is.
Then after you start MacVim, you can type something like this:
:e scp://username#yourserverdomain.com/.bash_profile
And you should see the file open up in your MacVim vim session. Edit it and save.
Having ssh keys set up to automatically authenticate you into the server will help.

Resources