Find and replace in Visual Studio code
Solution 1
I was able to get it to work but the workflow is poor:
- control + H to open Find/Replace
- Select your line of text
- Click the "Find in selection" icon to the right Alt L or ⎇ ⌘ L on macOS)
- Enter your find and replace characters in their inputs
- 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:
Alternatively, you can press Command + F to open Find and then click the little triangle on the left to show the Replace field:
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:
- Select text region with mouse or keyboard.
- Ctrl+H to toggle find and replace
- Alt+L to toggle find in selection
- 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:
- Use shift and arrows to highlight the term you want to match.
- Use Ctrl + d to highlight the next occurrence of the term.
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.
Adam
Updated on June 22, 2021Comments
-
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 with0
s. 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 thetest_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 almost 7 yearsI 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 almost 7 yearsOpening 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 almost 7 yearsIssue has been opened: github.com/Microsoft/vscode/issues/27083. Will post an answer if they come out with a fix.
-
Robert Koritnik over 6 yearsThis 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 almost 6 yearsChanging 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 over 5 yearsHow 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 over 5 yearsthe 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 over 5 yearsAlt+L commentary over mouse click makes it different (read: better)
-
Julian almost 5 yearsI 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 almost 5 years@Julian, nice one! I don't have sublime keymap enabled and it still works. I updated the answer. Thank you.
-
Julian almost 5 yearsthanks! 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ć almost 4 yearsYes! This is the natural flow - first select text, then open
Find & Replace
, then selectFind in selection
, then type term to find and replacement term and finallyReplace all
. -
otayeby almost 4 yearsCommand + Option + F enabled it for the first use, afterwards Command + F will be enough to get the find/replace bar.
-
Johnny Wong almost 4 yearsPerforming 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 over 3 yearsHaving the main workflow of the answer based on "click"s should disqualify this answer.
-
Julian Dm about 3 yearsIf you do it by ENTER, how do you skip ?
-
Julian Dm about 3 yearsIf you press CTRL + d once to often: how do you undo the last selection?
-
Philippe Fanaro about 3 years
Ctrl
+Shift
+d
-
Julian Dm about 3 yearsthat opened my debug window on factory settings. I found the command -> CTRL + U
-
Philippe Fanaro about 3 yearsYou 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 over 2 yearsThis is my new favorite thing! Thanks!
-
wisbucky over 2 yearsbtw, the icon on the right with the 4 lines is the
Toggle Find in Selection
-
Jack Kelly about 2 yearsThe setting
editor.find.autoFindInSelection
now expects a string, not a boolean. Valid values are: "never", "always", "multiline" -
Jack Kelly about 2 yearsAlso, auto find in selection does not work for me when editing a Python Jupyter Notebook
.ipynb
(although it works well when editing.py
files).