140

I have the following line in a file I'm editing in VSCode:

...............111.........111.............111..

I want to replace all .s with 0s. However, when I highlight the line and do a find/replace for .s, all the .s in the document are replaced, not just the ones in the line I've select, even when I toggle the "Find in selection" button. Is this a bug? In other editors, if I select a chunk of text and then do a find/replace, it will only find/replace matches within the selected block.

Below is a snippet that you should be able to reproduce the issue with. The ...............111.........111.............111.. line is inside the test_unicode function.

def test_simple2(self):
        """Simple CSV transduction test with empty fields, more complex idx, different pack_size.

        100011000001000 ->
        ..........111....................111..........11111..........111..
        """
        field_width_stream = pablo.BitStream(int('1000110001000001000', 2))
        idx_marker_stream = pablo.BitStream(int('11101', 2))
        pack_size = 4
        target_format = TransductionTarget.JSON
        csv_column_names = ["col1", "col2", "col3", "col4", "col5"]

        pdep_marker_stream = pablo.BitStream(generate_pdep_stream(field_width_stream,
                                                                  idx_marker_stream,
                                                                  pack_size, target_format,
                                                                  csv_column_names))
        self.assertEqual(pdep_marker_stream.value, 63050402300395548)

    def test_unicode(self):
        """Non-ascii column names.

        Using UTF8. Hard coded SON boilerplate byte size should remain the same, column name
        boilerplate bytes should expand.

        100010010000000 ->
        2 + 4 + 9     2 + 4 + 6     2 + 4 + 7
        ...............111.........111.............111..
        """
        field_width_stream = pablo.BitStream(int('100010001000', 2))
        idx_marker_stream = pablo.BitStream(1)
        pack_size = 64
        target_format = TransductionTarget.JSON
        csv_column_names = ["한국어", "中文", "English"]

        pdep_marker_stream = pablo.BitStream(generate_pdep_stream(field_width_stream,
                                                                  idx_marker_stream,
                                                                  pack_size, target_format,
                                                                  csv_column_names))
        self.assertEqual(pdep_marker_stream.value, 1879277596)

I'm using VSCode 1.12.2 in Ubuntu 16.04.

Mark
  • 143,421
  • 24
  • 428
  • 436
Adam
  • 8,752
  • 12
  • 54
  • 96

12 Answers12

186

I was able to get it to work but the workflow is poor:

  1. control + H to open Find/Replace
  2. Select your line of text
  3. Click the "Find in selection" icon to the right Alt L or L on macOS)
  4. Enter your find and replace characters in their inputs
  5. Click the Replace all icon

It works but you have to go through the workflow all over again for each new selection (except for CTR + H of course). BTW I have the exact same behavior in Sublime Text.

Could you go with a regExp to find your lines? Do they contain only .'s and 1's?

B25Dec
  • 2,301
  • 5
  • 31
  • 54
Mark
  • 143,421
  • 24
  • 428
  • 436
  • I could do it with regex in this case, but it seems like overkill. And in the general case, I think a more straight forward find/replace tool would be useful . I might open an issue on their git hub page. – Adam Jun 03 '17 at 15:25
  • 2
    Opening the issue is a good idea. I'm surprised that both VSCode and Sublime make you jump through these hoops for such a straightforward task. For example, you can not invert items 3 and 4 above in my answer - that seems prone to error and frustration. – Mark Jun 03 '17 at 17:15
  • 2
    Issue has been opened: https://github.com/Microsoft/vscode/issues/27083. Will post an answer if they come out with a fix. – Adam Jun 05 '17 at 17:54
  • 4
    the suggestion in that thread which works for me on the latest update is changing this setting (File > Preferences > Settings and then paste it in): "editor.find.autoFindInSelection": true – ja_him Oct 24 '18 at 08:32
  • 3
    Performing Replace In Selection with VSCode's default behavior is not quite convenient for me too. Therefore, I've just written a VSCode extension for that. Although later I found this post and think the settings `"editor.find.autoFindInSelection": "multiline"` may seem improve the situation somehow, my extension has several advantages beyond VSCode's Replace, and is especially good at replacing short search patterns (in regex). My extension is "Quick Replace In Selection", here: https://marketplace.visualstudio.com/items?itemName=johnnywong.quick-replace-in-selection – Johnny Wong Aug 08 '20 at 17:20
  • 4
    Why is this the only editor I've ever used where find/replace in selection is rocket science? I don't understand how the VS Code team managed to buck decades of precedent in favor of such counterintuitive behavior. Baffling. – Jamie Jackson Apr 01 '23 at 14:34
  • 2
    The process sucks big time. Having to click here and there just to make something so FOUNDAMENTAL to code editing work correctly while MS's Visual Studio have got it right for DECADES is a joke. This needs to be fixed ASAP. Ctrl+H with text being selected should apply to that text ONLY. Period. – dpant Apr 02 '23 at 13:23
63

This is a more general answer for other users who come here just wanting to use basic find and replace functionality.

On Mac you can press Command + Option + F to open Find and Replace:

enter image description here

Alternatively, you can press Command + F to open Find and then click the little triangle on the left to show the Replace field:

enter image description here

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
  • 2
    I have sublime keymap enabled so not sure if it works for all, but i was able to use CMD + OPT + f to open up find and replace immediately – Julian Jun 26 '19 at 19:28
  • @Julian, nice one! I don't have sublime keymap enabled and it still works. I updated the answer. Thank you. – Suragch Jun 26 '19 at 20:30
  • 2
    thanks! also, forgot to mention a couple other fun facts: If you CMD + ENTER that will set your find and replace for the entire file. Otherwise, you can step through each one with enter. You can also do a project-global find and replace by doing CMD + SHIFT + H – Julian Jun 26 '19 at 21:48
  • Command + Option + F enabled it for the first use, afterwards Command + F will be enough to get the find/replace bar. – otayeby Jul 16 '20 at 22:42
  • If you do it by ENTER, how do you skip ? – Julian Dm Apr 18 '21 at 12:40
  • 2
    btw, the icon on the right with the 4 lines is the `Toggle Find in Selection` – wisbucky Dec 01 '21 at 21:06
  • Yes, that icon on the right with a few horizontal lines is the key to do find&replacement in selection!!! – CyTex Sep 01 '22 at 07:45
37

From the VSCode devs:

We used to enable find in selection automatically when opening the find widget with a selection, but it was too easy to trigger accidentally and produced a lot of complaints. You probably want to set "editor.find.autoFindInSelection": true which will make it work the way you expect.

The VSCode GitHub issue has more details if anyone is interested.

EDIT: The autoFindInSelection option is available starting from VSCode 1.13. That version is currently in development (as of 6/7/2017), so this fix won't work until the new version is released.

Community
  • 1
  • 1
Adam
  • 8,752
  • 12
  • 54
  • 96
  • 8
    This will not solve the problem though... The only issue is that if you do consecutive F/R each time you select a new set of text **you need to refocus the find field** in order for it to change its *find context*. – Robert Koritnik Dec 14 '17 at 10:00
  • 2
    Changing the setting seemed to do the trick for me, although VS Code still marks _all appearances of the search term_ in the whole document, even if it will only replace the ones in the selection. – Marcus Mangelsdorf Jun 22 '18 at 09:54
  • 2
    The setting `editor.find.autoFindInSelection` now expects a string, not a boolean. Valid values are: "never", "always", "multiline" – Jack Kelly Mar 25 '22 at 11:28
  • 1
    Also, auto find in selection does not work for me when editing a Python Jupyter Notebook `.ipynb` (although it works well when editing `.py` files). – Jack Kelly Mar 25 '22 at 11:32
26

I found the following workflow to be fairly painless:

  1. Select text region with mouse or keyboard.
  2. Ctrl+H to toggle find and replace
  3. Alt+L to toggle find in selection
  4. Ctrl+Alt+Enter to replace all (or enter to replace individually)
Taylor Ellcee
  • 381
  • 4
  • 6
  • How is this different than the answer above? You just omitted actually inputting the find and replace text and swapped the order of the first two steps which doesn't matter. – Mark Sep 17 '18 at 13:54
  • Alt+L commentary over mouse click makes it different (read: better) – Matt Nov 15 '18 at 18:26
  • Yes! This is the natural flow - **first** select text, then open `Find & Replace`, then select `Find in selection`, then type term to find and replacement term and finally `Replace all`. – TheJavaGuy-Ivan Milosavljević May 25 '20 at 08:38
17

Since sometimes we might have similarly named things so you don't want to select everything, one of my favorites shortcut sequences is to select the next occurrence:

  1. Use shift and arrows to highlight the term you want to match.
  2. Use Ctrl + d to highlight the next occurrence of the term.

next occurrence selection

The Basic Editing in VS Code documentation page has some extremely useful variations on find and replace. One extremely useful shortcut is the Column (Box) Selection.

Philippe Fanaro
  • 6,148
  • 6
  • 38
  • 76
8

For mac

  1. Press command + option + f to bring up this menu:

enter image description here

  1. Press the little icon that has the arrow pointing at it above (3 horizontal bars)

  2. Select the text you want to do a find and replace in, and enter the 'find' and 'replace' fields

  3. Press this icon:

enter image description here

  1. That's all!
stevec
  • 41,291
  • 27
  • 223
  • 311
7

Just ran into this, my solution was to do

  1. command + N to create a new file
  2. paste my selection in there
  3. do my find and replace operations on that while file
  4. copy result back on top of my original selection
Rudy
  • 491
  • 5
  • 6
6

On Mac:

  • Select the text
  • Press command + shift + L
dang
  • 1,549
  • 1
  • 20
  • 25
5

Okay, this is really dumb, at first I felt really stupid when I finally found this, then I realized its just VS Code which has a bad interface.

The key is, there are TWO TOOLS here, Search/Replace (the pane on the left at the top of the Explorer) and Find/Replace (which is a dialogue which opens when you press CTRL-F)

THESE ARE NOT THE SAME TOOL!!

  • SEARCH-REPLACE is a tool written for project-wide searches and changes
  • FIND-REPLACE is a small dialogue best suited for more surgical editing.

i.e. you should use FIND-REPLACE!

find replace window image with find-in-selection highlighted

Also, its SUPER IMPORTANT to follow these steps in the right order, or it doesn't give the expected results.

  1. Press CTRL-F to open the find dialogue (usually opens in the top right)
  2. press the little arrow to the right of the find field which opens the replace input field
  3. ensure "find in selection" is turned off (i.e. not highlighted)
  4. type in the fields the strings you want to search/replace
  5. select the text you want to do a search/replace within.
  6. Now press "find in selection" (or type ALT-L)
  7. You should see only highlighting in the area you previously selected.
  8. Now you can click either "replace all" (CTRL-ALT-ENTER) or line-by-line "replace" (ENTER)

I hope this helps.

My suggestion to VSC developers, there should be a refresh button so that after you have selected the area of interest, and you already have your find and replace strings defined, you can select a new region and click "refresh find" instead of needing to repeat steps 3 to 8.

Leigh Boyd
  • 71
  • 1
  • 3
  • You can press CTRL+H to get the FIND-REPLACE dialog with replacement field already expanded. But yeah, this little feature violates several guidelines of UI design at once, and should be improved. Bookmarking this because I will surely need it again. – Zyl Dec 06 '22 at 14:41
  • I'm not going to be pressing anything for find /replace. Gotta be keyboard driven. If this is the closest we get bye bye vscode – WestCoastProjects May 11 '23 at 03:56
1

For those where it still does not work, there is one step omitted in all of the above answers: Uncheck "Find in selection" if it is checked (which it probably is when you are struggling with it and in despair googled the problem, and then found this SO entry). Only then select the lines and then re-check "Find in selection".

0

For Ubuntu, highlight the lines where you want to make changes, press alt-L and then ctrl-H. Then type the name you want to replace and replacement name in the top right dropdown.

Or some combination of these actions depending on whether the dropdown is already open or you're already in alt-L mode. I'm still learning but thought I'd share what's working.

mLstudent33
  • 1,033
  • 3
  • 14
  • 32
-3

I would recommend to use vim/gvim for such kind of things. VSCode doesn't have a convenient interface for an advanced editing actions(especially when you repeat the same steps) compare with gvim