Search code examples
vimgrep

Is there a shortcut to open vim and then jump to line ”num“ as ":num"?


I used to search words in my code tree by grep -rn. And the output is just as below.

"{filename}:{num}:{line content}"

Sometimes I will to open the file with vim, and jump to the line num as below.

vim {filename} +{num}

But I would like to do those steps as below. Is there any shortcut?

vim {filename}:{num}

Solution

  • You could create a shell function to do this, say, vimn:

    vimn () {
      case $1 in
      (*:[1-9]*) vim "${1%:*}" +"${1##*:}";;
      (*)        vim "$@"
      esac
    }
    

    This assumes a Bourne-type shell (sh, bash, ksh, zsh, ...). There are ways to make this work for a function named vim as well, but I prefer to avoid overloading command names.

    EDIT: make this work for vimn hello:world:42 -> open hello:world at line 42.