DEV Community

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

Posted on

2 2

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 (:

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️