Find and replace in Visual Studio code in a selection

Viewed 125996

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.

10 Answers

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

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)

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.

On Mac:

  • Select the text
  • Press command + shift + L

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

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.

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!

In 2022, there's a bug to be wary of

There is a silent bug (I'll add more about this as I learn more about it). But sometimes find and replace within selection doesn't find the values, even if you can see them with your own eyes. This is dangerous because you could think you've replaced them all but it really hasn't.

So do these two things:

  1. a visual check after doing a find and replace (to make sure it worked)
  2. if vscode completely ignores you (and doesn't do the find and replace within selection after you've followed the above instructions), close the find and replace box by pressing the "x" in the corner, and retry the sequence of steps (it worked for me after closing and retrying).

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".

Related