View differences since last write with vim

Sometimes it happen to me to start a vim session and forget it somewhere, behind a lot of other terminals. Some hours later, when it’s time to go home and I want to close all programs, I find this vim session again and I want to close it, but I don’t remember if I wrote something since last time I saved it and I don’t want to just save it without knowing what I’m saving or if it’s not necessary. So I first try to quit:

​:q

And since I usually wrote something, I can not just quit without saving (:wq) or forcing (:q!) and I get following error message:

E37: No write since last change (add ! to override)

Vim's "no write since last change" error message

To solve my problem, I just need to know what I wrote since last time I saved the document. This can easily be done with following command:

:w ! diff % -

Vim with diff's output

Explanation: vim writes the document (:w) to the standard output and pipes (!) it to the diff command, which needs two arguments (actually two files to be compared). The first argument (%) is the original file name and the second one (-) means “standard input” (i.e. kind of “what I’m getting from the pipe”).

I also found other better solutions to do this, which for instance have a better vim-integration, colours, etc., but they are all more complicated and not so easy to remember. So… this is my quick first choice.

My Instagram Pictures

Importing CSV Data from a Sunny WebBox with FTP

Last month we let install a photovoltaic (PV) system of 8.4 kWp with a special module (the Sunny WebBox of the company SMA Solar Technology) that sends all measurements to a portal, which is easily accessible with an iPhone, iPad an of course a normal internet browser. This was in fact for me the main reason to buy this optional module. But only after the installation of the PV-system I discovered that the portal (the place where my data is send) is located in another country, which is not what I wanted. Fortunately I also discovered that the Sunny WebBox, if configured accordingly, can be accessed using FTP (File Transfer Protocol) to transfer files, so that I don’t need anymore to send my data to someone I don’t know ;-)

As “programmer” as I am, I had the idea to use this access to download all the data automatically (at least once a day) and do some statistics by myself. And so I did. I wrote the following small python script which synchronise (in one way) the data on the FTP-Server of the Sunny WebBox with a local directory on my computer.

This script has been tested only wit OS X 10.7+, but should also works with any Linux/Unix distribution. The only requisite is actually the python module ftplib. There are also some limitations (see comment in the script header) which don’t compromise the good functioning in this specific case.

#!/usr/bin/env python

# Author: Michele Marcionelli <michele (at) marcionelli.ch>
#
# Note: this script synchronize a ftp directory with a local directory
# with following limitations:
#   - only filesizes are compared
#   - only filenames without spaces are supported
#
# Last change: 28-Nov-2013

FTP_HOST = 'host_or_ip_of_synny_webbox'
FTP_USER = 'User'
FTP_PASS = '0000'

def ftp_download(path):
    listing = []
    ftp.dir(path, listing.append)
    for entry in listing:
        x = re.compile(" +").split(entry)
        if x[2] == '<DIR>':
            ftp_download(path + '/' + x[3])
        else:
            remotesize = int(x[2])
            datetime = time.mktime(
                time.strptime(x[0] + ' ' + x[1], '%m-%d-%y %H:%M'))
            filename = x[3]
            # check if local copy exists and have same size
            try:
                localsize = os.path.getsize(path + '/' + filename)
            except:
                localsize = 0

            if localsize != remotesize:
                print 'downloading ' + filename
                if not os.path.isdir(path):
                    os.makedirs(path)
                ftp.retrbinary(
                    'RETR ' + path + '/' + filename,
                    open(path + '/' + filename, 'wb').write)
                os.utime(path + '/' + filename, (datetime, datetime))

ftp = FTP(FTP_HOST)
ftp.login(FTP_USER, FTP_PASS)
ftp_download('CSV')

Feel free to copy and modify the script and leave comments.

Statistics of our Photovoltaic System

In November 2013 we finally installed a 8.405 kWp Photovoltaic System and in these days I managed to do some statistics. At the moment these aren’t very interesting due to the lack of data… but wait some few months and you will see.

The system consists of 41 Modules Hareon Solar (HR-205-24/Aa 2BB) of 205 Wp each, a Sunny Tripower inverter and WebBox of SMA Solar Technology which send all data to to a portal, that allow remote monitoring and maintenance.