Editing Remote Files on lftp

Well, after that happy day when I finally set fireFTP to open remote files on vim for editing, I’ve decided that I won’t use fireFTP any more. The reason for this is basically that I want to migrate to a command-line-based life. There are things I won’t be able to do through the cl, but that is OK. All I want is to be more efficient, and the command line is one way to really be more efficient. Those tasks that I cannot do via the command line I will do them in the most efficient way I know. For example, I installed pentadactyl on firefox, and it really speeds up any kind of activity you do on firefox.

Anyway, I stated using lftp because it is a really nice ftp client. It come pre-installed on Ubuntu, or at least I don’t remember installing it, and it is really powerful. However, it does have some disadvantages, and one of them is that you cannot edit remote files.

The way you would do it is downloading the file, editing it locally, and then uploading it to the server, but that is just a really ugly way to do things. I decided I would look for a solutions. A quick search led me to this page:
but that is not what I wanted. This script allows you to open any remote file on vim, edit it, and when you close vim, the file gets automatically updated on the remote server. This is not really a good way to do it, because many times you want to make changes, reload the page, make some more changes, and reload again. If every time you make a change you have to close vim for the script to upload the file, you will end up wasting a lot of time.

The ideal way to do it would be to upload the file as soon as it has been edited and saved even if vim is still open. Well, I just spend more than 8 hours writing a script to do just that.

As a side note, I didn’t even feel the time passing. Rarely do I spend so much time working on something straight.

I found many challenges writing this script, especially because I don’t really know shell scripting. The first problem I found is documented on http://superuser.com/questions/374406/why-do-i-get-not-found-when-running-a-script
The other challenges I faced are related to shell commands and the fact that shell commands run one after the other. That is, if you type 2 commands, the first one has to finish execution in order for the second to start executing. You can run commands asynchronously, but that is something I did not know when I started this, or at least I didn’t exactly know how to do it.

All I knew about running command asynchronously was using the & at the end of a command. I use it sometimes when I want to launch a program, but I don’t want it to block the current terminal window.

I started by modifying the script presented on maven.pl. After so many modifications I got to this:

save() {
#echo shell echo $COPROC_PID
        if [[ /tmp/$tempid$2 -nt /tmp/$tempid$2.check ]]; then 
                echo  shell echo "Uploading file " $2
                cp /tmp/$tempid$2 /tmp/$tempid$2.check;
                echo put /tmp/$tempid$2 -o $2;
        sleep 2
        if [[ $COPROC_PID != '' ]]; then
                save $tempid $2
echo get $2 -o /tmp/$tempid$2
cp /tmp/$tempid$2 /tmp/$tempid$2.check;
coproc terminator -x $1 /tmp/$tempid$2
save $tempid $2 $COPROC
echo put -E /tmp/$tempid$2 -o $2
rm $file2

just create a file under ~/.lftp/ named edit.sh, and write that code in it.
Then create another file on the same location and name it rc and add the following:

alias edit source -e ~/.lftp/edit.sh

Now you can open your files and edit them. They will be uploaded when the script detects that they have changed.

There are still many things that can be done, like adding the possibility of editing more than one file at the same time, but for now I’m happy with it the way it is.

The usage is simple:

start lftp:

$ lftp user@host.com

enter your password and navigate the folder where the file you want to modify is located. The type:

edit vim file.php

vim is the program you want to use to edit the file. You could use gimp for images, vim for text files, or gedit for text files, or any other text editor, or in fact any program that is suitable to edit the kind of file you want to edit.
file.php is the name of the file you want to edit.

The most useful resource while developing this was the man page for bash.

Some links that were useful, or maybe not, are recorded here just for reference:

Not all this links are completely related to the script, and not all of them were useful. Some of them I didn’t read in their entirety or I just red over the surface without really paying much attention to them. They are there just for future reference in case I want to go back to them later. Bookmarking them seemed too


2 thoughts on “Editing Remote Files on lftp

  1. Why not use ssh to run Vim directly on the server? That way you don’t have to transfer the entire file each time it is opened and saved.


    • Ssh would be the best solution, but not all hosting compnies offer it. Also most clients are familiar with ftp but not ssh, but yes ssh is a better solution.

Comments are closed.