The Go Command

Alexey Komissarouk requested a modification to the cl command that opens the argument in the default text editor if it’s a file. The go command will open the file in the editor defined by the EDITOR environment variable.
Place this in your .bashrc, .bash_profile or analogous file:

go()
{
if [ -f "$1" ]
then
    $EDITOR "$1"
else
    cd "$1" && ls
fi
}

If you have not defined the default editor, perhaps you’d like to try emacs, my chosen editor. Add this to the .bashrc or .bash_profile as well:

export EDITOR=emacs
Advertisement
This entry was posted in Shell Scripting. Bookmark the permalink.

11 Responses to The Go Command

  1. Lauren F says:

    Great command! Only problem is typing ‘go’ with no arguments tries to open a blank file, rather than cd/ls on the home directory. Switching the order to something like

    go()
    {
    if [ -d $1 ]
    then
    cd $1 && ls
    else
    $EDITOR $1
    fi
    }
    makes it change to and list the home directory when given no arguments.

    -Lauren

    • Zachary Wasserman says:

      Thank you for bringing this up. I actually realized that just by quoting the $1 argument in the test command you can get the desired functionality. It will look like this:

      
      go()
      {
      if [ -f "$1" ]
      then
          $EDITOR $1
      else
          cd $1 && ls
      fi
      }
      

      I’m going to make this modification above, as this is the functionality I intended.

  2. Brian Krent says:

    I have a further modification upon Lauren’s `go`, where a blank file is not opened if the file does not already exist.

    For many, I can see where Lauren’s version has the desired behavior, but for my purposes, I’d rather be notified if I tried editing something that doesn’t exist. If I want to create a new file (not just edit an already existing file), I prefer to do so explicitly with my text editor.

    go()    {
            if [ -d $1 ]; then
                    cd $1 && ls -AF
            elif [ -f $1 ]; then
                    $EDITOR $1
            else
                    echo "$1 not found."
            fi
            }
    • Zachary Wasserman says:

      This is certainly a sensible behavior, Brian. Adding the quotes to the test command as I describe in my reply to Lauren will give you the error message from the cd command when you try to cd something that does not exist.

    • Brian Krent says:

      zacharywasserman:
      Good catch with the quote marks around “$1”. I guess (at first glance) your updated version with the quotes now behaves as mine does; except without the extra elif silliness.

  3. Brian Krent says:

    I type too slow! JINX! ;P~

  4. Brian Krent says:
    go() { if [ -f "$1" ]; then $EDITOR "$1"; else cd $1 && ls -AF; fi }

    Revision: Added quotes around the path to the EDITOR to handle for cases were spaces exist in the filename.

    • Brian Krent says:

      Apparently, I’m still not paying attention… final fix:

      go() { if [ -f "$1" ]; then $EDITOR "$1"; else cd "$1" && ls -AF; fi }
  5. Pat Regan says:

    This is a pretty awesome idea. I’ve stolen the idea and I’ve tacked on another else to take also take advantage of autojump.

    Here’s what I’m using now:


    go() {
    if [ -f "$1" ]; then
    $EDITOR "$1";
    elif [ -d "$1" ]; then
    cd "$1"
    elif [ "" = "$1" ]; then
    cd
    else
    j "$1"
    fi
    }

    I’m tempted to go one step further and use `xdg-open` as well, but I don’t really want that to get in the way of editing.

  6. Tom says:

    Hi,

    The function can also be improved by :
    – asking to open the file with sudo if the file is write protected
    – asking to create the file if it does not exist

    My “go” function is called “o” (as open) and is available here : https://github.com/tomsquest/dotfiles/blob/master/zsh/functions/o

    • Pat Regan says:

      Thanks for the ideas, Tom!

      I really, really like the sudo idea, and I understand why you would have it ask first before starting the edit.

      I’m not sure I like being asked for confirmation, though. By default on my few Centos servers, root has aliases that make cp, mv, and rm interactive. It drives me up a tree. 🙂

      Your second suggestion has definitely given me some food for thought. On one hand, I’m not sure I want a command that opens things to start creating things (or offering to create things) when I typo. On the other hand, though, I can certainly see it being pretty handy.

      I’ll be keeping an up to date copy of my “go” command on my blog:

      http://blog.patshead.com/2011/05/my-take-on-the-go-command.html

      I also plan to keep it working in both zsh and bash.

      Pat

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s