2008-12-18

Easy bash file backup command

If you want to backup a file, with timestamps (atime,ctime,mtime) preserved, you can run this command:

cp -pf --backup=t filename filename

Course, this is a pain to write, so an alias is in order:

alias bk='cp -pf --backup=t'

To use this alias, you can type

bk myimportantfile.txt myimportantfile.txt

But then why type it twice? Use a shell function instead:

bk () {
    [ "$1" == "-h" ] && \
        echo "Backs up one or more files." && \
        echo "usage: bk filename1 [filename2...]" && \
        return

    for f in $@ ; do
        local f="$1"; shift
        \cp -pf --backup=t "$f" "$f"
    done
}

Oh, and this script assumes

  • bash
  • gnu ls
  • gnu cp
  • no whitespaces in words.

2008-12-14

A consistent titlebar in emacs

If I know that I will be working for a while on a machine, I install emacs onto it. In the past, depending on the version of emacs and the build, I would have a different titlebar on my emacsen. Heres a bit of code which standardizes the titlebar, judging from certain environment variables.

;; Create a reasonable titlebar for emacs, which works
;; on both windows and unix.
;; Note: assumes HOSTNAME is exported.
(defun create_title_format (user host)
  "Creates a window title string"
  (interactive)
  (list (getenv user) "@" (getenv host) ":"
        '(:eval
          (if buffer-file-name
              (replace-regexp-in-string
               (getenv "HOME")
               "~"
               (buffer-file-name))
            (buffer-name))))
  )
;; Set window and icon title.
(if (eq system-type 'windows-nt)
    (setq frame-title-format
          (create_title_format "USERNAME" "COMPUTERNAME"))
  (setq frame-title-format
        (create_title_format "USER" "HOSTNAME")))

continuous ls in bash

Here's a bash function I use often at work, when I'm waiting for a file to be saved (or updated) in a directory. It runs ls -larth regularly in your terminal. It works under bash, and assumes that you've set the checkwinsize setting in bash.

shopt -s checkwinsize

wls () { 
  arg=$1;
  seconds=${arg:=5};
  while true; do
    clear;
    echo "$(date) (refresh rate: ${seconds}s)";
    i=$COLUMNS;
    while test $i -gt 1; do
      echo -n '-';
      i=$(( $i - 1 ));
    done;
    echo;
    ls -larth |\
      tail -n $(( $LINES - 3 )) |\
      cut -c-$COLUMNS |\
      grep -v ^total;
    sleep $seconds;
  done
}

It keeps track of the size of your terminal, so if there are more files than the number of lines in your xterm it will not display the oldest ones. Likewise, if there are files with filenames which are very long, they will also be truncated.

Oh, and finally, it takes a number as param, so it will wait N seconds between updates. Default sleep time is 5.

copying and pasting in emacs

I've used emacs for almost 15 years, and it's strange, but when I wanted to copy and paste, I've always cut the text first, then pasted it back, then finally pasted it where I wanted it to be.

It's only recently that I noticed that M-w copied the text to the clipboard. Silly, I know.

Here's some commands I use regularly when editing text:

key bindingdescriptionemacs command
C-spaceStart selection of region. Everything selected is automatically copied to clipboard.set-mark-command
C-p, C-M-pUp one line, one block back, respectivelyprevious-line, backward-list
C-n, C-M-nDown one line, one block forward, respectivelynext-line, forward-list
C-f, M-fForward one letter, forward one word, respectivelyforward-char, forward-word
C-b, M-bBack one letter, back one word, respectivelybackward-char, backward-word
C-wCut region to clipboardkill-region
M-wCopy region to clipboardkill-ring-save
C-yPaste most recent text from clipboardyank
M-yCycle through clipboard history (start with one C-y, then follow with multiple M-y)yank-pop

2008-12-13

Firefox plugin: Its all Text!

This is a firefox plugin I use all the time at work. For me its very useful- what does it do? Well, you know those textareas in web pages? Where you are supposed to write stuff, like um I dont know, blog posts, wiki entries, etc? They are a pain to write in. First off, they're too small, and then I miss all my shortcuts and macros from emacs.

Enter 'Its all Text!'. You install it, then you can tell it to make all editing come in a separate app. Since I use emacsclient (at work. At home I use gnuclient), I have an emacs server running on one of my desktops which the firefox plugin calls. An emacs window pops up, I get to type away, and when I hit C-c C-c. The text gets pushed into the textarea, and I'm done :)

This this much easier that bumbling about using g-client, but otoh its not a clean solution... Which to choose?

Ok, so here's how I got g-client to work

g-client is an emacs library which enables your emacs to access blogger, google calendar, etc. I tried it in the previous post, and it seemed to work quite well, albeit a bit unpolished.

First off, I downloaded g-client from googlecode.com. Once I got it down, I followed the instructions over at emacsspeak- that is, I unpacked it, ran make, then tried to load the library into emacs.

Beep! Nope, I got an error complaining that emacs couldn't find something called 'g-cus-load'. After some googling, I found that it was enough to create a file with that name (g-cus-load.el), which contains the following line:

(provide 'g-cus-load)

After this, I ran into my next problem- how to use it? The 'manual' over at emacsspeak is terse to say the least- not much of a manual. I'm a bit too tired to start reading new code, so instead I just tried running various commands starting with gblogger (M-x gblogger tab tab).

Question is, how to customize? The manual states that I'm supposed to use the customize interface, but all I could find was an empty G group under Applications. Strange.

Anyhow, I'm writing in emacs right now, and its quite nice.

test from emacs

Ok, here goes. I'm testing posting to blogger from my trusty ol editor. So far, so good. :)

Hmm. How to enter text to this blog...

Hmm. Why would I want to write blog entries in this crappy textarea when I could do it in emacs? Theres 'Its all text!', but I wonder if theres some libs for emacs out there for blogger? Its time to check the tubes for some google-emacs connectibity software...

Re-starting blog over on blogger

So... I've decided to re-start my little diary. Its been over a year since I stopped, but I realized that I really missed writing down my ideas and what not.

Why did I decide to do it on Blogger and not nanoblogger, or vee? Well, I'm lazy. Damn lazy. I just can't be bothered to fiddle around for hours just getting the environment right. Instead, I can focus on content, and perhaps a bit on layout.

Now, the question is if I'm going to try to migrate my old posts into Blogger, or if I'll skip it. Right now, I don't think I could be bothered. We'll see.

For now, I'll just supply a link to my old blog, over at blahonga.yanson.org.