1

I often have to do which command, and then open it in vim to actually see what's inside. I'd like to have an alias or function that does this, with tab completion. Here's what I've tried:

superwhich() {
    which "$1" | vim
}

superwhich2() {
    vim $(which $1)
}

Neither of these work. Is there something I'm missing?

  • 1
    The 1st is broken because it's passing the filename as input on stdin instead of command line argument to vim. The 2nd should work (except when the program name contains spaces). What errors do you get from it? – mosvy May 8 at 15:02
  • The second gives me 'Illegal variable name'. – user351959 May 8 at 15:03
  • Are you sure you running that in bash? There's no such error in bash. Please post some reproducible example. – mosvy May 8 at 15:06
  • 1
    Is your shell csh or tcsh? Then here's you superwhich alias: alias superwhich 'vim `which \!:1`' – mosvy May 8 at 15:14
  • With zsh: superwhich() vim -- =$1 (which also handles nicely the case where the command can't be found or if there's also an alias/function by that name) – Stéphane Chazelas May 8 at 15:19
2

csh and tcsh don't have functions.

The only way is to use aliases, and pass arguments to them via ! history substitutions:

alias superwhich 'vim "`which \!:1`"'

superwhich bzgrep

Or better (with error checking):

alias superwhich 'set q = `which \!:1`; if(-r "$q") vim "$q"'
0

You could define a script for that:

#! /bin/sh -
for cmd do
  if
    cmd_path=$(command -v -- "$cmd")
  then
    set -- "$@" "$cmd_path"
  else
    printf >&2 '"%s" not found\n' "$cmd"
  fi
  shift
done
[ "$#" -gt 0 ] && vim -- "$@"

Then that could be used from any shell.

Note that it won't work for sh builtins (like echo/[...).

With zsh, you'd just run:

vim =cmd

Or

vim cmd(:P)

to edit the file of the cmd command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy