skip to Main Content

I want to be able to pipe a file and also a file and line number into code such that Visual Studio Code (VS Code) opens the file in the current window. I know the command line options for code to do so, but I am not able to figure out how to pipe the output of some scripting that gives the filename to code in a way that works.

For example, maybe I’d like to pipe the output of grep and open the file in VS Code. The following command will print out the filename and line number.

grep --with-filename --line-number --recursive --max-count 1 --include *.txt 'Test' . 
| cut --delimiter : --fields 1-2

will output something like ./filename.txt:47. Now, code‘s --goto option can handle this as expected if done like:

code --reuse-window --goto ./filename.txt:47

However, if I try to pipe into code, then VS Code just closes the current window and then opens a new blank window.

grep --with-filename --line-number --recursive --max-count 1 --include *.txt 'Test' . 
| cut --delimiter : --fields 1-2 
| code --reuse-window --goto

How do I make this work such that VS Code opens up the file at the line number in the current window? Am I doing something incorrectly with bash and the piping by incorrectly expecting this to just work? Or is this a code specific issue?

3

Answers


  1. Chosen as BEST ANSWER

    One way to do this is to use xargs.

    grep --with-filename --line-number --recursive --max-count 1 --include *.txt 'Test' . 
    | cut --delimiter : --fields 1-2 
    | xargs --replace code --reuse-window --goto {}
    

    The --replace will assume {} such that xargs will take the output of the pipe and place it where {} occurs in the following command.

    I am interested in other ways to do this as well.


  2. Aren’t you just confusing the standard input stream with the commandline? you want code --reuse-window --goto <output of command that produces thing> (I.e. output of command is used as part of commandline), but then you try to do <command that produces thing> | code --reuse-window --goto (I.e. piping output of command into the consumer’s standard input stream).

    You can substitute command outputs in commandlines in Bash by using `<command>` (enclose with backticks) or $(<command>) (also enclose with doublequotes if you want the output to be treated as a single commandline argument instead of being split by the field separator). Ex. code --reuse-window --goto "$(grep --with-filename --line-number --recursive --max-count 1 --include *.txt 'Test' . | cut --delimiter : --fields 1-2)".

    Login or Signup to reply.
  3. To be clear, this is not piping anything into code. You just want to use the output of a command as an argument to it.

    One thing to consider is how to deal with multiple files. If you want to open them all, I would use xargs with the -n 1 option; the command below winds up running the code command once per identified file, eventually opening all the files in tabs of the same window:

    grep -Hnrm 1 --include '*.txt' Test . |
      cut -d: -f1-2 |
      xargs -n 1 code --reuse-window --goto
    

    If you only want to open the first file, you could just interject a head:

    grep -Hnrm 1 --include '*.txt' Test . |
      cut -d: -f1-2 |
      head -n 1 |
      xargs code --reuse-window --goto
    

    But in that case you don’t really need xargs at all; you could use plain old command substitution:

    code --reuse-window --goto 
      "$(grep -Hnrm 1 --include '*.txt' Test . |
         cut -d: -f1-2 | head -n 1)"
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search