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

Vim mode: add :s/search/replace command #9428

Closed
1 task done
dancojocaru2000 opened this issue Mar 16, 2024 · 16 comments · Fixed by #15985
Closed
1 task done

Vim mode: add :s/search/replace command #9428

dancojocaru2000 opened this issue Mar 16, 2024 · 16 comments · Fixed by #15985
Labels
enhancement [core label] vim

Comments

@dancojocaru2000
Copy link

Check for existing issues

  • Completed

Describe the feature

I find needing to use the GUI for find&replace to be quite unwieldy. It would be quite useful to have Vim's :s implemented. The features (some missed by other projects implementing Vim emulation) should be:

  • :s/search/replace - replaces one occurrence of "search" with "replace"
  • :s/search/replace/g - replaces all occurrences of "search" with "replace"
  • :s=a/b=c/d - replaces "a/b" with "c/d" (using = or another character as a different separator to allow the search to contain the / character)
  • When making a selection in visual mode, the command should only apply to the selected text

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

No response

@dancojocaru2000 dancojocaru2000 added admin read Pending admin review enhancement [core label] triage Maintainer needs to classify the issue labels Mar 16, 2024
@ConradIrwin
Copy link
Member

ConradIrwin commented Mar 18, 2024

👍 We currently support :%s/../.., (and you can use any character instead of /) but you have to use % and /g is always provided. It would be lovely to parse this more similarly to vim.

TODO:

  • Implement /g (only replace the first match on a line by default)
  • Implement range parsing (allowing no range for current line, and then X,Y for a line range)

If you'd like to pair with me on this, you can book time here: https://calendly.com/conradirwin/pairing

@ConradIrwin ConradIrwin added vim and removed triage Maintainer needs to classify the issue admin read Pending admin review labels Mar 18, 2024
@elyobo
Copy link

elyobo commented Mar 28, 2024

Global search and replace is handy but current line and fixed number of lines replacement is extremely useful too (the range parsing @ConradIrwin mentions) 👍

Edit: line parsing in general would be handy. Keeps throwing me off, e.g. for indentation like 3>> to indent the next three lines. Seems to work intermittently, e.g. 3dd works to delete the next three lines.

ConradIrwin pushed a commit that referenced this issue Apr 30, 2024
Release Notes:

- Added support for line ranges in vim replace commands #9428


- not supporting anything other than bare line numbers right now
- ~need to figure out how to show range in question in search bar~
@ConradIrwin implemented showing a highlight of the selected range for a
short direction instead
- ~tests lol~
@JosephTLyons
Copy link
Collaborator

This is now in 0.134.0-pre.

@elyobo
Copy link

elyobo commented May 1, 2024

Awesome, thanks!

@stepan-tikunov
Copy link

stepan-tikunov commented May 19, 2024

Is the s/search/replace still not supported or is it still only in preview release? The only thing that worked for me is %s/search/replace which is not what I always want.

@ConradIrwin
Copy link
Member

Currently we support % and (as of recently) 1,2s to give a line number range. What range syntax were you trying to use?

@stepan-tikunov
Copy link

stepan-tikunov commented May 20, 2024

I was trying to substitute the text that I selected in visual mode

@ConradIrwin
Copy link
Member

ConradIrwin commented May 20, 2024 via email

@stepan-tikunov
Copy link

stepan-tikunov commented May 20, 2024

The first option seems the least preferable if our goal isn't to 100% replicate vim's behavior. There is no real functional difference between the zed-style and the hybrid options. But it seems that the zed-style option looks more intuitive from user's perspective as opposed to the hybrid way because for commands that don't rely on visual selection, those prefilled marks might confuse someone

@dancojocaru2000
Copy link
Author

I would very much prefer an indication that Zed will only perform the replace in the selection and won't mess up the entire file.

@sstadick
Copy link

sstadick commented May 21, 2024

I don't think the any of the original features here are implemented? The global find replace works, but none of the following works?

  • :s/search/replace - replaces one occurrence of "search" with "replace"
  • :s/search/replace/g - replaces all occurrences of "search" with "replace"
  • :s=a/b=c/d - replaces "a/b" with "c/d" (using = or another character as a different separator to allow the search to contain the / character)
  • When making a selection in visual mode, the command should only apply to the selected text

I'm not sure this should be closed yet, especially since it was a top voted issue. It's actually the single issue stopping me from using Zed at the moment (maybe I'm alone in how often I use Vim search replace on visual selections though 🤷).

@ConradIrwin ConradIrwin reopened this May 21, 2024
@rokob
Copy link
Contributor

rokob commented Jul 2, 2024

Also something with the range syntax appears to be broken as well.

If I have the text

1
2
3
4
5

and I execute the command:

:2,3s/^/x/

I get the result

1
x2
x3
x4
5

If I do the same in vim I get the expected result:

1
x2
x3
4
5

@rokob
Copy link
Contributor

rokob commented Jul 8, 2024

I took a stab at the issue I mentioned: #13920

ConradIrwin pushed a commit that referenced this issue Jul 9, 2024
This is related to #9428 

I noticed that doing a search and replace for the beginning of a line
`^` results in the trailing line being included in the search. This
seems to be because of the way the range is generated for generating
matches being the up to the start of the trailing line rather than up to
the end of the last line.

I added a test and took a stab at fixing it but it is a bit yolo as this
is the first time I've seen this codebase.
@smit2909
Copy link

smit2909 commented Jul 14, 2024

+1 on this issue, it was very jarring to see the %s replaced every instance in file rather than just the selected lines in visual mode. This feels like a must have for a vim mode. Entering line ranges manually isn’t something I do ever in vim. Hoping this gets fixed soon!

@elyobo
Copy link

elyobo commented Jul 15, 2024

I never use visual mode, but I enter line ranges all the time - however it doesn't work anyway for many things, e.g. search and replace does not work on a single line or on a line range. Honestly the most frustrating thing in zed and why I sometimes jump back into real vim.

ConradIrwin added a commit that referenced this issue Aug 8, 2024
The most requested feature here is "search and replace in visual mode",
but as a happy side effect we can now support things like :2,12j to join
those lines, and much much more.



Release Notes:

- vim: Added support for range syntax in command
([#9428](#9428)).
- vim: Prefill command with `:'<,'>` from visual mode
([#13535](#13535)).
@JosephTLyons
Copy link
Collaborator

This should be landing in today's preview release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement [core label] vim
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants