Question

Quick question. I love emacs. I hate typing stuff though, so I like to use e to invoke emacs.

Previously I had my .bash_profile (OS X) configured as: alias e="emacs .". However, I was tired of still having to type emacs {file} when I just wanted to edit one file.

So I tried to whip this up, from some googling, but bash is complaining about the []:

###smart emacs, open file, or if none, dir 
e()
{
    if [$1]; then
        emacs $1
    else
        emacs .
    fi
}

I want to use this to do: e something.c or, just e.

Was it helpful?

Solution

#!/bin/bash                       

###smart emacs, open file, or if none, dir
e()
{
    if [[ -z "$1" ]]; then # if "$1" is empty
        emacs .
    else
        emacs "$1"
    fi
}

OTHER TIPS

Try

if [ $# -ge 1 ]; then
  emacs "$@"

I think bash is very peculiar about spaces. I'm even surprised you're allowed to omit a space between function name and (). (Also, using $@ should open all files you pass.)

ETA: better check against number of arguments, in case of e "" foo.txt...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top