Vscode: Feature Request: Preserve Case - Find / Replace

Created on 26 Jul 2016  ·  39Comments  ·  Source: microsoft/vscode

It'd be awesome if a preserve case feature was eventually added to the find/replace functionality!

ST3's implementation of it is a good example if you need one.

editor-find feature-request help wanted

Most helpful comment

While an extension can get the job done, being built into the Find/Replace window would be ideal, with an option right next to "Use Regular Expressions", that searches in the entire file.

This hasn't been active in a long while. Is this not planned anymore? This would truly be a life changer.

All 39 comments

This would be life-changing

Example: currently implementing an immutable red/black tree, and there is an if/else statement, with each branch containing a mirror of the other branch. To implement the second branch, I need to replace all instances of "left" with "right" and vice versa. This includes:

  • left/right (local variable)
  • Left/Right (part of a function name)
  • LEFT/RIGHT (a constant indicating branch direction)

A case-insensitive replacement operation would make this doable in one step. Instead, I have to first replace each case of _left_ with _foo_ (3 steps). Next I replace each case of _right_ with _left_ (3 more steps). Finally, I replace _foo_ with _right_, for a total of 9 steps. With case insensitive replacement, it'd be 3 steps total.

Let's track the setting persistence in #6158

@rebornix I think this is not a duplicated issue.
The issue #6158 is about 'saving' the setting.
This issue (from what I read) is about keeping the case for the replaced word (default in emacs)
eg:

foo-bar
FOO-BAR
Foo-Bar
// find: foo-bar
// replace: keep-case
keep-case
KEEP-CASE
Keep-Case

@ant31 The feature is/would be per-character, not per word, which means your example would resolve as:

// find: foo-bar
// replace: keep-case
keep-case
KEEP-CAse
Keep-case

Sorry for closing it too fast and Thank you for making it clear, it's a valid feature request.

Just wrote a simple extension that covers some use cases. Check out https://marketplace.visualstudio.com/items?itemName=vilicvane.sensitive-replace

this should work exactly like described in case when using Cmd + D , Add selection to Next Find

there are so many situations in using camel case / hungarian notation where it would be useful...

hope it gets built in ? :)
I'll take a look also how I can build it and assign Cmd + D to it

@vilic 's extension looks almost perfect, it just needs to be across an entire project 👍

While an extension can get the job done, being built into the Find/Replace window would be ideal, with an option right next to "Use Regular Expressions", that searches in the entire file.

This hasn't been active in a long while. Is this not planned anymore? This would truly be a life changer.

It would be really appreciated! PHPStorm has this feature for at least 2 years.

Unfortunatelly, the extension (even being good) does not get the job done in my case (pun not intended) because so many times I have to search/replace over all project's files, not only on a selected text.

Really need this. I'm having to do 3 find and replaces where a case preserve would make it only 1.

Find: foo
Replace: bar

FOO->BAR
Foo->Bar
foo->bar

The way Eclipse does this is through a regex replace token \C.

So I can search for foobar and replace it with \Cbazqux and it will work as expected.

Why the freak this is not implemented yet ?

This is an extremely useful feature, I love the implementation in WebStorm.

Why on earth is this not planned - it's a simple, self contained quality of life improvement that's been in every editor I've used in years. Easy to implement and test.

You need to flesh out your core editor before all these bells and whistles I keep seeing in release notes.

@barnc That comment isn't helpful at all. VSCode is free and open source, and the team is doing an amazing job with so many improvements, and disparaging their prioritization doesn't show much gratitude toward that.

If you've looked into the code and see that it's simple, I'm sure the team would gladly look at a pull request from you.

Just adding my support to this. It is just crazy that we have to open/use another tool to get things going properly with vscode? Obviously I appreciate the efforts of the developers, but it is these types of features that always get overlooked in open source projects. Which is a shame!

@quicksnap Whilst I appreciate your positive attitude, this is a 2 year old issue for a repeatedly requested feature that is simple to implement.

I don’t need to look into a code base to determine if adding a case sensitive replace to an existing find and replace function is easy or not. Furthermore, ‘why don’t you do it yourself’ does not qualify as an excuse for it not being even considered for development by the team.

@gmosx if you’re going to leave sarky single link comments I’d recommend making sure said links don’t 404, hth.

not qualify as an excuse for it

As far as I know, they don't owe you an excuse to NOT implement a feature.
Plus who are 'they'? It's an opensource project with 700+ contributors! maybe it's time for you to contribute too instead of whining?

It's not just any open source project. And not everyone can contribute. It's an important project of Microsoft connected with their effort do being nice to devs again and with their effort to spread TypeScript (among other things I guess).

But... Yet another comment with _why is this not implemented_ is not helpful for devs. Just add +1 to the initial comment and maybe some PM from Microsoft will finally make time to do this.

I just found out that Mac OS X has something called "Paste and Match Style" which defaults to Shift+Cmd+V. I think this can be used, if you can consider the capitalization of a word to be its "style".

Nearly 3 years. Is there any hope of this getting implemented?

Today was my day to really try vscode. I'm back to emacs with its case aware search and replace. This feature is really very important for refactoring. The current implementation can do harm your code.

Today was my day to really try vscode. I'm back to emacs with its case aware search and replace. This feature is really very important for refactoring. The current implementation can do harm your code.

Seems a tad overly dramatic no? If it really is such a game changer and so vital for refactoring, why not just use the extension provided further up the page?

@itsafire
You can do it somewhat with regex capturing groups
Search term: (benev)(a)(lence)
Replace with: ${1}o${3} (you can omit the {} but they're sometimes necessary)

obraz

it will match regardless of case so the result will be

obraz

I use it to fix typos..

But I guess It doesn't really allow this:
keep-case -> foo-bar
KEEP-CASE -> FOO-BAR

@leawp Neat, but that wouldn't work if you want to the change to be relative to the case of the search.

Here o is always lowercase. Changing BENEVALENCE would yield BENEVoLENCE. I usually confront this issue when changing a camel-case word like wanting the following:

  • lastTabIndex => lastItemIndex
  • tabIndex => itemIndex

Here, replacing tab/Tab with Item versus item would be relative to the search result's capitalization.

@clshortfuse yeah, I've realized it after reading comments, added a little explanation to mine.

One of those really simple features that forces me to keep another text editor around

With #60311 merged, you will be able to preserve case when doing single replacement in Find Widget from tomorrow's Insiders. It's still in its early stage so currently we only support following patterns

  • all lower case
  • ALL UPPER CASE
  • Title Case

Considering we already have the infrastructure there, it would be straightforward to add more, all you need to do is

  1. Detect the pattern for the find result, add an entry in https://github.com/microsoft/vscode/blob/master/src/vs/editor/contrib/find/replacePattern.ts#L59
  2. Convert the replace term to the new pattern
  3. Write test cases in https://github.com/microsoft/vscode/blob/master/src/vs/editor/contrib/find/test/replacePattern.test.ts#L157

Added Help Wanted label and feel free to contribute more patterns for this feature. Simply follow above 3 steps.

Amazing, I'm so excited!!!

Not working as expected; I'll have a PR done in a bit

Insiders now works as expected :D Thanks for merging!

Although we have preserve (sensitive) case in normal find and replace we cannot do it when using replace all across multiple files. That would be useful.

@uglyeoin it's being tracked in https://github.com/microsoft/vscode/issues/78397

I think this issue can be closed and new issues be created / referenced here for any related functionality that isn't in yet. Thanks to all who contributed 🙂

Thanks all for the patience and contribution to this feature. If you are interested in contributing more patterns to Preserve Case, you can follow https://github.com/microsoft/vscode/issues/9798#issuecomment-515153288 and if you run into issues while playing with this feature, please file new issues in this repository.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mrkiley picture mrkiley  ·  3Comments

omidgolparvar picture omidgolparvar  ·  3Comments

VitorLuizC picture VitorLuizC  ·  3Comments

biij5698 picture biij5698  ·  3Comments

trstringer picture trstringer  ·  3Comments