3 votes

How can I make "whereis" automatically open the file on Nvim when it is the only result?

EDIT: SOLVED

It looks like it was much simple than I thought and someone solved it on Reddit already. I won't delete, just leave the link if someone is interested.

Runtime Environment

Issue

Sometimes I use "whereis" (aliased for "wh", but it doesn't make any difference...) for my own scripts.

I usually copy their paths manually (using tmux) and paste to the command line resulting in something like this:

nvim /home/my_username/my_scripts_folder/my_script

Could I make that into a single command?

Thanks in advance!

5 comments

  1. [5]
    ItchyOuch
    Link
    I'll have to edit this (on mobile) and confirm it but something like: Inside .bashrc alias wh=~/bin/nvim-whereis inside nvim-whereis #/use/bin/env bash Output=$(Whereis "$@") [[ $(Echo $output |...

    I'll have to edit this (on mobile) and confirm it but something like:

    Inside .bashrc
    alias wh=~/bin/nvim-whereis

    inside nvim-whereis

    #/use/bin/env bash

    Output=$(Whereis "$@")

    [[ $(Echo $output | cut -d: -f2 | xargs -n1 | wc -l) -eq 1 ]] && nvim $(echo $output | cut -d: -f2) || echo $(output)

    99% sure this won't work as is. Might be capturing random white space and what not, but maybe this gives you an idea of what you can do.

    The alias in the Bash just sets up a shortcut. You could just add ~/bin to you path, then you don't need the alias and just rename the script to something shorter

    1. mrbig
      (edited )
      Link Parent
      Have you seen the answer in the SOLVED link? I now have a script with the following content, which seems to be doing the job: #!/usr/bin/env bash script="$*" nvim "$(whereis "$script" | cut -d' '...

      Have you seen the answer in the SOLVED link? I now have a script with the following content, which seems to be doing the job:

      #!/usr/bin/env bash
      
      script="$*"
      nvim "$(whereis "$script" | cut -d' ' -f 2)"
      
    2. [3]
      mrbig
      Link Parent
      I just realized I forgot to put the link. Fixed now. Anyway: here it is.

      I just realized I forgot to put the link. Fixed now. Anyway: here it is.

      1. [2]
        ItchyOuch
        Link Parent
        Oh nice, way cleaner.

        Oh nice, way cleaner.

        1 vote
        1. mrbig
          Link Parent
          Someone else suggested an even cleaner solution: #!/usr/bin/env bash script="$*" nvim "$(which "$script")"

          Someone else suggested an even cleaner solution:

          #!/usr/bin/env bash
          
          script="$*"
          nvim "$(which "$script")"
          
          1 vote