Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create alias to fast-edit a result #304

Closed
Hywan opened this issue Jan 6, 2017 · 8 comments
Closed

Create alias to fast-edit a result #304

Hywan opened this issue Jan 6, 2017 · 8 comments
Labels
question An issue that is lacking clarity on one or more points.

Comments

@Hywan
Copy link

Hywan commented Jan 6, 2017

Hello and thanks for this incredible work!

I am wondering if this would be hard to implement something similar to https://github.com/aykamko/tag? I can try if you want. The principle is simple: Generate one alias per results in a temporary file (so e1 will open an editor to the first result —file and line—, e2 to the second result etc.; e.g. alias e1="$EDITOR +line file"), source the alias (done by the shell, not by the command itself), that's it.

@BurntSushi
Copy link
Owner

I don't think mucking about with shell aliases is something that belongs in ripgrep. It sounds like a maintenance nightmare. Does it work on Windows? Which shells does it support?

I would, however, be amenable to making ripgrep's output format a bit more customizable. However, that will require some refactoring/design work that I'm not quite ready to tackle yet. That particular issue is tracked by #244.

@BurntSushi BurntSushi added the question An issue that is lacking clarity on one or more points. label Jan 6, 2017
@BurntSushi
Copy link
Owner

Note that it looks like ripgrep support will land in tag at some point: aykamko/tag#27

@lukaslueg
Copy link

lukaslueg commented Jan 6, 2017

You can do this using peco and a shell function.

Put this in your .zshrc / .bashrc / whatever:

rg2vim() { vim $(rg -n $1 | peco | awk -F\: '{print "+"$2,$1}') }

You can then just call rg2vim foobar. Peco will fire up and show you all files that contain foobar and the matching line obtained from rg. You can narrow it down within peco. Hit Enter and Vim will fire up at the exact matching line. Hold your breath in awe.

@lukaslueg
Copy link

This has the virtue of opening multiple selected files in individual tabs and skipping peco if there is only one match reported by rg. It does a horrible (read:none) job of escaping special characters though. Maybe someone can come up with something better

rg2vim() { vim -c "$(rg -n $1 | peco --select-1 | awk -F\: 'BEGIN {ORS="|"}; {print (NR==1 ? "e " : "tabe ") $1"|:"$2}')" }

@Hywan
Copy link
Author

Hywan commented Jan 6, 2017

Will try to integrate it into emacs. Have been running Vim for 7 years, I did the switch recently (with evil-mode, miam!).

@Hywan
Copy link
Author

Hywan commented Jan 6, 2017

peco is a nicer alternative to tag. Thanks for the tips!

@Hywan Hywan closed this as completed Jan 6, 2017
@partounian
Copy link
Contributor

partounian commented Dec 19, 2017

EDIT: Moving the chat to Luke's dotfiles. lukaslueg/dotfiles#1

Hey @lukaslueg it's been some time from this thread but I noticed this in your dotfiles

rg2vim() {
	vim -c "$(
	FL=0
	while IFS=: read -r fn ln line; do
		if [ "$FL" -eq "0" ]; then
			printf "e %q|" "$fn"
			FL=1
		else
			printf "tabnew %q|" "$fn"
		fi
		printf ":%d|" "$ln"
	done < <(rg -n "$1" | peco --select-1)
	)"
}

Is this the latest and greatest solution?

EDIT: Also any way to catch a Ctrl-C and prevent vim from opening?

@lukaslueg
Copy link

I never got around to such details. The "script" does not process Ctrl-C and just falls on it's nose if there is no rg or peco binary at all. All I do with it is fire rg and see if something useful comes out at all and then use rg2vim !$ to redo the search and load the desired matches into vim.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question An issue that is lacking clarity on one or more points.
Projects
None yet
Development

No branches or pull requests

4 participants