DEV Community

Cover image for Open all conflicted files from Command Line
Lucas Perez
Lucas Perez

Posted on

Open all conflicted files from Command Line

A few days ago I learned more about the xargs command.

For a brief explanation, xargs will receive a chunk of text and transform it in a list of arguments and pass it to a command that you can specify.

For instance, if you write in the terminal:

echo -e "file1\nfile2"
Enter fullscreen mode Exit fullscreen mode

You will see this result:

file1
file2
Enter fullscreen mode Exit fullscreen mode

We now could pipe this into xargs and do something with it, say, create files with those names:

echo -e "file1\nfile2" | xargs touch
Enter fullscreen mode Exit fullscreen mode

We just created two files whose names are file1 and file2.

There are many things you could do with xargs, write xargs -h to see more (the -I flag is specially interesting).

But what happened today was that I decided I wanted to open all files that had a conflict after an attempted git rebase -i command.

To do that, we first have to find a way to list all the files with a conflict. I'm pretty sure there is nothing git can't do, and of course what we want is very possible.

The command git diff can receive the flag --name-only, which will give you just the names of the files that git diff would have looked at.

user@pc:~$ git diff --name-only
app/this-guy
config/this-other-guy
user@pc:~$
Enter fullscreen mode Exit fullscreen mode

This is just what we wanted! We can now easily pipe it into xargs and call a code editor, for example vim:

git diff --name-only | xargs vim
Enter fullscreen mode Exit fullscreen mode

Xargs can be very useful, this is just one use case. Maybe you want to append a specific extension to a bunch of files? You could use find, ls and grep, just to pass them to xargs with a mv command, for instance.

I hope someone else also learns a little with my little "playing around in terminal" thing.

Thanks for reading and have a nice day (:

Top comments (0)