Find and replace in Visual Studio code

100,654

Solution 1

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?

Solution 2

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

Solution 3

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.

Solution 4

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)

Solution 5

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.

Share:
100,654
Adam
Author by

Adam

Updated on June 22, 2021

Comments

  • Adam
    Adam almost 3 years

    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.

  • Adam
    Adam almost 7 years
    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.
  • Mark
    Mark almost 7 years
    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.
  • Adam
    Adam almost 7 years
    Issue has been opened: github.com/Microsoft/vscode/issues/27083. Will post an answer if they come out with a fix.
  • Robert Koritnik
    Robert Koritnik over 6 years
    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.
  • Marcus Mangelsdorf
    Marcus Mangelsdorf almost 6 years
    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.
  • Mark
    Mark over 5 years
    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.
  • ja_him
    ja_him over 5 years
    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
  • Matt
    Matt over 5 years
    Alt+L commentary over mouse click makes it different (read: better)
  • Julian
    Julian almost 5 years
    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
  • Suragch
    Suragch almost 5 years
    @Julian, nice one! I don't have sublime keymap enabled and it still works. I updated the answer. Thank you.
  • Julian
    Julian almost 5 years
    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
  • TheJavaGuy-Ivan Milosavljević
    TheJavaGuy-Ivan Milosavljević almost 4 years
    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.
  • otayeby
    otayeby almost 4 years
    Command + Option + F enabled it for the first use, afterwards Command + F will be enough to get the find/replace bar.
  • Johnny Wong
    Johnny Wong almost 4 years
    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: marketplace.visualstudio.com/…
  • Philippe Fanaro
    Philippe Fanaro over 3 years
    Having the main workflow of the answer based on "click"s should disqualify this answer.
  • Julian Dm
    Julian Dm about 3 years
    If you do it by ENTER, how do you skip ?
  • Julian Dm
    Julian Dm about 3 years
    If you press CTRL + d once to often: how do you undo the last selection?
  • Philippe Fanaro
    Philippe Fanaro about 3 years
    Ctrl + Shift + d
  • Julian Dm
    Julian Dm about 3 years
    that opened my debug window on factory settings. I found the command -> CTRL + U
  • Philippe Fanaro
    Philippe Fanaro about 3 years
    You can edit your shortcuts in VS Code if you want. I don't know why yours was mapped to that, mine was working with Ctrl + Shift + d since the beginning as far as I remember, and it seems to be a much more natural shortcut.
  • jlsecrest
    jlsecrest over 2 years
    This is my new favorite thing! Thanks!
  • wisbucky
    wisbucky over 2 years
    btw, the icon on the right with the 4 lines is the Toggle Find in Selection
  • Jack Kelly
    Jack Kelly about 2 years
    The setting editor.find.autoFindInSelection now expects a string, not a boolean. Valid values are: "never", "always", "multiline"
  • Jack Kelly
    Jack Kelly about 2 years
    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).