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

Telescope-style search box #8279

Open
1 task done
SalamanderXing opened this issue Feb 23, 2024 · 26 comments
Open
1 task done

Telescope-style search box #8279

SalamanderXing opened this issue Feb 23, 2024 · 26 comments
Labels
enhancement [core label] search / project search Feedback for in-file search, multi-buffer global search, etc

Comments

@SalamanderXing
Copy link

Check for existing issues

  • Completed

Describe the feature

In (neo)vim I'm able to search for file names and contents using telescope. This has the following advantages:

  • runs search as I type
  • I don't have to leave the current tab so it's faster
  • Since the preview of a file and the list of files are separated I can just very quickly scroll through the list of files only using my keyboard

If applicable, add mockups / screenshots to help present your vision of the feature

Screenshot 2024-02-23 at 15 57 27
@SalamanderXing SalamanderXing added admin read Pending admin review enhancement [core label] triage Maintainer needs to classify the issue labels Feb 23, 2024
@JosephTLyons JosephTLyons added search / project search Feedback for in-file search, multi-buffer global search, etc and removed triage Maintainer needs to classify the issue admin read Pending admin review labels Feb 26, 2024
@ryanwinchester
Copy link

It's the main thing I'll miss from neovim

Screen.Recording.2024-04-19.at.7.53.25.PM.mov

@ryanwinchester
Copy link

And the grep version

Screen.Recording.2024-04-19.at.8.00.28.PM.mov

@NachoNievaG
Copy link

Agree completely.
Also, the live grep requires ripgrep (rust based) as a dependency, which using it in Zed sounds suitable. (talking as an outsider regarding integrations tho)

@lungarella-raffaele
Copy link

Would be the feature that makes me move from vs-code

@martincartledge
Copy link

Please make this happen!

@joachimhorshauge
Copy link

This would be amazing

@ElhamAryanpur
Copy link

absolutely, I'm on board too

@zaevimn
Copy link

zaevimn commented Jul 16, 2024

I also want this to happen, It'll speed the coding.

@costinsin
Copy link

This would be very nice!

@vjdhanota
Copy link

In case it helps anyone else, I was able to set up a task that uses rg, fzf and bat for a better find in files experience:

  {
    "label": "search:project",
    "command": "QUERY=${ZED_SELECTED_TEXT:-}; FILE=$(rg --column --hidden --line-number --no-heading --color=always --smart-case --colors match:fg:green --colors path:fg:white --colors path:style:nobold --glob '!**/.git/' '--glob' '!**/node_modules'  \"$QUERY\" . | fzf --ansi --delimiter : --preview 'bat --style=numbers,changes,header --color=always --highlight-line {2} {1}' --preview-window 'up:60%:+{2}+3/3' --layout=reverse --query \"$QUERY\" --print-query | tail -1); if [ -n \"$FILE\" ]; then LINE=$(echo $FILE | cut -d':' -f2); COL=$(echo $FILE | cut -d':' -f3); FILE=$(echo $FILE | cut -d':' -f1); zed $FILE:$LINE:$COL; fi",
    "env": {},
    "cwd": "${ZED_WORKTREE_ROOT}",
    "allow_concurrent_runs": false,
    "use_new_terminal": false,
    "reveal": "always",
    "hide": "on_success"
  },

The preview window is specific to my layout but should be pretty easy to update (--preview-window 'up:60%:+{2}+3/3' --layout=reverse). There is probably a cleaner way to set this up but it has worked for me so far.

Something built-in like telescope would be amazing

@j-o-sh
Copy link

j-o-sh commented Sep 11, 2024

This 🙌

I'm soooo in favor. This is the main (and atm. basically the only) missing feature that keeps me from switching to Zed from Neovim. - I don't care for tabs or clunky file trees that much but am totally content using Telescope for this.

Things I'd need from that:

  • Fuzzy find files in the root dir (basic Telescope)
  • Grep over files in root dir
  • Find files in the open buffers

Bonus:

  • Grep over files in the open buffers
  • Find (and Grep in) files that have been modified in the current branch of the repo

@Koshkaj
Copy link

Koshkaj commented Sep 12, 2024

If this gets added to the editor along with errorlens. consider me as a loyal lifelong Zed user.

@eduDorus
Copy link

eduDorus commented Oct 9, 2024

This would be a very nice addition! Keep up the good work

@dkuku
Copy link

dkuku commented Oct 13, 2024

Every time I try to switch from nvim I end up just running nvim in the editor terminal to edit files this way :D
For me this is a killer feature.

@calebeaires
Copy link

This is the final piece to make me migrate from webstorm to zed

@Rits1272
Copy link

Planning to move from neo vim to zed. Wish this gets implemented in zed

@bauffman
Copy link

bauffman commented Nov 4, 2024

I've been trying Zed for a few weeks now and the only feature that keeps me from making the switch is Telescope. Hope this gets released and keep up the good work!

@GengCen-Qin
Copy link

In case it helps anyone else, I was able to set up a task that uses rg, fzf and bat for a better find in files experience:

  {
    "label": "search:project",
    "command": "QUERY=${ZED_SELECTED_TEXT:-}; FILE=$(rg --column --hidden --line-number --no-heading --color=always --smart-case --colors match:fg:green --colors path:fg:white --colors path:style:nobold --glob '!**/.git/' '--glob' '!**/node_modules'  \"$QUERY\" . | fzf --ansi --delimiter : --preview 'bat --style=numbers,changes,header --color=always --highlight-line {2} {1}' --preview-window 'up:60%:+{2}+3/3' --layout=reverse --query \"$QUERY\" --print-query | tail -1); if [ -n \"$FILE\" ]; then LINE=$(echo $FILE | cut -d':' -f2); COL=$(echo $FILE | cut -d':' -f3); FILE=$(echo $FILE | cut -d':' -f1); zed $FILE:$LINE:$COL; fi",
    "env": {},
    "cwd": "${ZED_WORKTREE_ROOT}",
    "allow_concurrent_runs": false,
    "use_new_terminal": false,
    "reveal": "always",
    "hide": "on_success"
  },

The preview window is specific to my layout but should be pretty easy to update (--preview-window 'up:60%:+{2}+3/3' --layout=reverse). There is probably a cleaner way to set this up but it has worked for me so far.

Something built-in like telescope would be amazing

Thank you for providing the code solution. I have adjusted it to make it look even better. Everyone is welcome to give it a try.

2024-11-23.12.59.50.mp4

my fish config:

function search_and_edit
    set -l selected_file (rg --column --hidden --line-number --no-heading --color=always --smart-case --glob '!**/.git/' --glob '!**/node_modules/' . | fzf --ansi --delimiter : --preview 'bat --style=numbers,changes,header --color=always --highlight-line {2} {1}' --preview-window 'up:60%:+{2}+3/3' --layout=reverse)

    if test -n "$selected_file"
        set -l file (echo $selected_file | cut -d':' -f1)
        set -l line (echo $selected_file | cut -d':' -f2)
        set -l col (echo $selected_file | cut -d':' -f3)

        zed $file:$line:$col
    end
end

my keymap

{
    "context": "Editor && vim_mode == normal && !VimWaiting && !menu",
    "bindings": {
      "space f": [
        "workspace::SendKeystrokes",
        ": new center terminal enter search_and_edit space && space exit enter"
      ]
    }
  }

@JaLnYn
Copy link

JaLnYn commented Nov 28, 2024

Hey guys, I was looking at this issue too. I have an implementation of this search style in rust on my websocket ide project here on line 177. I use the crate nucleo.

I will spend some time looking into the code to see if I can easily move the coded over.

If someone can point to a starting point, that would be great.

@JaLnYn
Copy link

JaLnYn commented Nov 29, 2024

I submitted a pr for realtime search

#21348

but it's kinda slow because their current search seems to lack optimizations. Waiting on their advice on how to proceed.

@LogicKahanHai
Copy link

Hey @GengCen-Qin Can you please help me out.. I configured fish and added the function as well and it works in the terminal when I put it manually. I have also added the keymap as you provided. But, when i press space-f it just opens the full-screen terminal tab for me and then stops there. I am unable to find any solution to this. Would appreciate your help. Thanks :)

@vjdhanota
Copy link

vjdhanota commented Dec 23, 2024

The "workspace::SendKeystrokes" workaround keybinding to open a center terminal no longer works as of a recent release. Luckily, the latest preview release added support for running tasks in a center pane (Zed Preview 0.167.1).

I've updated and enhanced my config since my last post. I've added file search and historical search support. Sharing it here for others:

In my ~/.zshrc

function zed_file_search() {
  local FILE
  FILE=$(
    rg --files --hidden --color=always \
      --glob '!**/.git/' \
      --glob '!**/node_modules' \
    | fzf --ansi --preview 'bat --decorations=always --color=always {} --style=full --line-range :50' \
          --preview-window 'up:60%:wrap' \
          --layout=reverse
  )

  if [ -n "$FILE" ]; then
    zed "$FILE"
  fi
}

function zed_search() {
  local query="${1:-}"
  local OUT

  # Run fzf in multi-selection mode with dynamic reload based on the query.
  # The first line of output is the final query, followed by one or more selected results.
  OUT=$(
    fzf --ansi --cycle --exact --delimiter ':' --nth 4.. \
      --preview 'bat --decorations=always --color=always {1} --highlight-line {2} --style=full' \
      --preview-window 'up:60%:+{2}+3/3' \
      --layout=reverse \
      --query "$query" \
      --phony \
      --print-query \
      --multi \
      --bind 'start:reload:rg --column --hidden --line-number --no-heading --color=always --smart-case \
        --colors match:fg:green --colors path:fg:white --colors path:style:nobold \
        --glob "!**/.git/" --glob "!**/node_modules" {q} .' \
      --bind 'change:reload:rg --column --hidden --line-number --no-heading --color=always --smart-case \
        --colors match:fg:green --colors path:fg:white --colors path:style:nobold \
        --glob "!**/.git/" --glob "!**/node_modules" {q} .'
  )

  # If nothing was selected, do nothing
  if [ -z "$OUT" ]; then
    return
  fi

  # The first line of OUT is the query used, append it to history
  local FINAL_QUERY
  FINAL_QUERY=$(echo "$OUT" | head -n1)

  if [ -n "$FINAL_QUERY" ]; then
    echo "$FINAL_QUERY" >> ~/.zed_search_history
  fi

  # Now extract the selected file lines (everything after the first line)
  # Each line is in the format: filepath:line:col:...
  echo "$OUT" | tail -n +2 | while IFS= read -r FILE_LINE; do
    if [ -n "$FILE_LINE" ]; then
      local LINE COL PATH
      # Not sure why, but I have to use absolute paths to `cut` and `zed` here
      LINE=$(echo "$FILE_LINE" | /usr/bin/cut -d':' -f2)
      COL=$(echo "$FILE_LINE" | /usr/bin/cut -d':' -f3)
      PATH=$(echo "$FILE_LINE" | /usr/bin/cut -d':' -f1)
      /usr/local/bin/zed "$PATH:$LINE:$COL"
    fi
  done
}

function zed_search_history() {
  # FZF to choose a past query
  local HIST_QUERY=$(fzf --prompt="History> " < ~/.zed_search_history)
  if [ -n "$HIST_QUERY" ]; then
    # Run zed_search again with the chosen query
    zed_search "$HIST_QUERY"
  fi
}

My global tasks.json:

  {
    "label": "search:files",
    "command": "zed_file_search",
    "env": {},
    "cwd": "${ZED_WORKTREE_ROOT}",
    "allow_concurrent_runs": false,
    "use_new_terminal": false,
    "reveal": "always",
    "hide": "always",
    "show_command": true,
    "show_summary": true,
    "reveal_target": "center"
  },
  {
    "label": "search:project",
    "command": "zed_search ${ZED_SELECTED_TEXT:-}",
    "env": {},
    "cwd": "${ZED_WORKTREE_ROOT}",
    "allow_concurrent_runs": false,
    "use_new_terminal": false,
    "reveal": "always",
    "hide": "always",
    "show_command": true,
    "show_summary": true,
    "reveal_target": "center"
  },
  {
    "label": "search:history",
    "command": "zed_search_history",
    "env": {},
    "cwd": "${ZED_WORKTREE_ROOT}",
    "allow_concurrent_runs": false,
    "use_new_terminal": false,
    "reveal": "always",
    "hide": "always",
    "show_command": true,
    "show_summary": true,
    "reveal_target": "center"
  },

I then have some keybindings that fire these tasks. Here is a quick demo of how it works/looks:

Kapture.2024-12-23.at.09.41.53.mp4

I recently came across this TUI tool for general-purpose fuzzy finding inspired by Telescope - https://github.com/alexpasmantier/television. Integrating this as a center terminal task might be a better approach but I haven't gotten around to it yet

@baldwindavid
Copy link
Contributor

baldwindavid commented Dec 25, 2024

@vjdhanota - Nice work on that and good find for television. Just a naive setup leveraging television works well enough for me. I'm sure this could be improved, but doesn't get much simpler.

tasks.json

  {
    "label": "File Finder",
    "command": "zed \"$(tv files)\"",
    "hide": "always",
    "allow_concurrent_runs": true,
    "use_new_terminal": true
  },
  {
    "label": "Find in Files",
    "command": "zed \"$(tv text)\"",
    "hide": "always",
    "allow_concurrent_runs": true,
    "use_new_terminal": true
  },

keymap.json

"cmd-p": [
  "task::Spawn",
  { "task_name": "File Finder", "reveal_target": "center" }
],
"cmd-f": [
  "task::Spawn",
  { "task_name": "Find in Files", "reveal_target": "center" }
],

@LogicKahanHai
Copy link

LogicKahanHai commented Dec 25, 2024

Hi @baldwindavid
I tried your method. Amazing! Thank you so much for this! Just one small question, how does the reveal_target: center part of it work (or affect the whole thing)? I took a look at #9445 and #22004 and from there what I understood was that adding this would run the task in the center terminal (like the one that opens when you run : new center terminal) but it does not do that in my system. Did I miss something in between? I really want it to run in the center terminal.

Thanks for your help! Merry Christmas!

@baldwindavid
Copy link
Contributor

@LogicKahanHai The feature to run a task in the center terminal is currently only in the preview release. The next stable release will include it.

@baldwindavid
Copy link
Contributor

Posted a discussion of the drop-in replacement leveraging television with a video in #22581. This is effectively a solved issue for me at this point though I know popping into a TUI is not everyone's bag.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement [core label] search / project search Feedback for in-file search, multi-buffer global search, etc
Projects
None yet
Development

No branches or pull requests