zed icon indicating copy to clipboard operation
zed copied to clipboard

Vim mode: add :s/search/replace command

Open dancojocaru2000 opened this issue 1 year ago β€’ 14 comments

Check for existing issues

  • [X] 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 avatar Mar 16 '24 02:03 dancojocaru2000

:+1: 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 avatar Mar 18 '24 16:03 ConradIrwin

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.

elyobo avatar Mar 28 '24 01:03 elyobo

This is now in 0.134.0-pre.

JosephTLyons avatar May 01 '24 19:05 JosephTLyons

Awesome, thanks!

elyobo avatar May 01 '24 21:05 elyobo

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.

stepan-tikunov avatar May 19 '24 19:05 stepan-tikunov

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

ConradIrwin avatar May 20 '24 19:05 ConradIrwin

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

stepan-tikunov avatar May 20 '24 19:05 stepan-tikunov

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

πŸ‘ Thanks!

I'm in a bit of a pickle with what to do about '< and '> - vim always adds them to the command palette from visual mode and then complains if you do something "wrong" (like :w).

It seems like there are three options:

  • vim-style: when typing : it prefills the '</'> marks, and if you type a command that doesn't support a range it errors
  • zed-style: when typing : with a visual selection, it doesn't give you a visual indication, and if the command respects the active selection then it just works
  • hybrid: we prefill the marks, and if you type a command that doesn't support them, we just ignore that?

Any preferences?

On Mon, May 20, 2024 at 1:24 PM, Stepan Tikunov @.***> wrote:

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

β€” Reply to this email directly, view it on GitHub https://github.com/zed-industries/zed/issues/9428#issuecomment-2121060658, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAXAQGIH5DFQMRO4KX5HWLZDJEVJAVCNFSM6AAAAABEY6F4WWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMRRGA3DANRVHA . You are receiving this because you modified the open/close state.Message ID: @.***>

ConradIrwin avatar May 20 '24 19:05 ConradIrwin

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

stepan-tikunov avatar May 20 '24 19:05 stepan-tikunov

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.

dancojocaru2000 avatar May 20 '24 20:05 dancojocaru2000

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 🀷).

sstadick avatar May 21 '24 15:05 sstadick

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 avatar Jul 02 '24 18:07 rokob

I took a stab at the issue I mentioned: https://github.com/zed-industries/zed/pull/13920

rokob avatar Jul 08 '24 00:07 rokob

+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!

smit2909 avatar Jul 14 '24 06:07 smit2909

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.

elyobo avatar Jul 15 '24 03:07 elyobo

This should be landing in today's preview release.

JosephTLyons avatar Aug 14 '24 17:08 JosephTLyons