How to copy in Terminal without mouse

294 Views Asked by At

I want to copy text in Terminal without using my mouse. For example to store a commit hash when doing a git log and use it for another command. Anyone has an idea how to move the cursor up and copy, maybe turning on something like a copy/visual mode or having a vim mode?

1

There are 1 best solutions below

1
Andrew McGlathery On

You can use git log --pretty=%H to get only the commit hash of the commit in question, then pipe it to pbcopy. The full command would be:

git log --pretty=%H | pbcopy

the --pretty=format option can do a whole lot, actually look at http://git-scm.com/docs/git-log and search for "pretty formats"

But in general, using a combination of utilities such as grep and cut, as well as the options in the command you are using, is probably the most portable way to go.