1

快速提问。我喜欢emacs。不过我讨厌打字,所以我喜欢用它e来调用 emacs。

以前我将 .bash_profile (OS X) 配置为:alias e="emacs .". 但是,当我只想编辑一个文件时,我已经厌倦了仍然必须键入 emacs {file}。

所以我试图通过一些谷歌搜索来解决这个问题,但 bash 抱怨[]

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

我想用这个来做:e something.c或者,只是e.

4

2 回答 2

2

尝试

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

我认为 bash 对空间非常奇特。我什至很惊讶你可以在函数名和 () 之间省略一个空格。(此外,使用 $@ 应该打开您传递的所有文件。)

ETA:更好地检查参数的数量,以防e "" foo.txt...

于 2010-12-19T17:21:48.760 回答
1
#!/bin/bash                       

###smart emacs, open file, or if none, dir
e()
{
    if [[ -z "$1" ]]; then # if "$1" is empty
        emacs .
    else
        emacs "$1"
    fi
}
于 2010-12-19T17:21:20.443 回答