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

Question: Get the selected line **number**? #1034

Closed
4 of 15 tasks
infokiller opened this issue Aug 31, 2017 · 6 comments
Closed
4 of 15 tasks

Question: Get the selected line **number**? #1034

infokiller opened this issue Aug 31, 2017 · 6 comments

Comments

@infokiller
Copy link
Contributor

  • Category
    • fzf binary
    • fzf-tmux script
    • Key bindings
    • Completion
    • Vim
    • Neovim
    • Etc.
  • OS
    • Linux
    • Mac OS X
    • Windows
    • Windows Subsystem for Linux
    • Etc.
  • Shell
    • bash
    • zsh
    • fish

Hey, is there an option in fzf for outputting the line number that I'm missing?

If not, what's the best way to get the selected line number, as opposed to the actual line content?
Right now I have a script which does this in an hacky way by grepping for the selected line, but this won't always work (for example, if there are duplicate lines in the stream).

Thanks!

@infokiller infokiller changed the title Questions: best way to get the selected line **number**? Question: best way to get the selected line **number**? Aug 31, 2017
@infokiller infokiller changed the title Question: best way to get the selected line **number**? Question: Get the selected line **number**? Aug 31, 2017
@junegunn
Copy link
Owner

Have you looked at #1014?

@infokiller
Copy link
Contributor Author

Thanks a lot @junegunn that looks perfect!
Didn't know about the --with-nth option which is what I needed to implement this.

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented Feb 28, 2022

fzf show line numbers

To have fzf show the matching line number of a text file you are searching in, then do this:

grep -n '' "path/to/file.txt" | fzf

(this won't show line numbers)

cat "path/to/file.txt" | fzf

I like to also add -m --reverse to fzf:

grep -n '' "path/to/file.txt" | fzf -m --reverse

@infokiller
Copy link
Contributor Author

@ElectricRCAircraftGuy I'm not sure what comment are you replying to, since no one mentioned using cat for this in this issue. Anyway, the solution posted here seems better: it can hide line numbers and it's also faster than grep -n.

@ElectricRCAircraftGuy
Copy link

ElectricRCAircraftGuy commented Feb 28, 2022

@infokiller , oh, sorry, I'm just documenting a generic answer to the title of the question: "Get the selected line number?", which I am demonstrating how to do when passing regular text files to fzf to fuzzy search within the file, which is something I previously did with cat. I wasn't able to find that anywhere, but google searches led me here so I just jotted it down for future readers who stumble here looking for what I was looking for. find doesn't meet my need as it finds file paths, but I want to fuzzy search file content.

Anyway, what I've got above does just that for anyone else who is looking for it.

@Agent-E11
Copy link

Agent-E11 commented Mar 9, 2024

@ElectricRCAircraftGuy It looks like you can alter the the answer infokiller linked to to get what you want:

nl -ba file.txt | fzf | awk '{ print $1 }'

This will add line numbers to the contents of file.txt (-ba means to also include empty lines), and pipe that to fzf to search through. Then print out the line number of the selected line using awk.

Personally, I think this looks cleaner than the grep -n answer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants