[英]How to select all instances of a variable and edit variable name in Sublime
If I select a variable (not just any string) in my code, all other instances of that variable get a stroke (white outline) around them:如果我在我的代码中选择一个变量(不仅仅是任何字符串),该变量的所有其他实例都会在它们周围得到一个笔划(白色轮廓):
Is there a keyboard shortcut that will let me select all of those instances of the variable and edit them all at once?是否有键盘快捷键可以让我选择变量的所有实例并一次编辑它们?
⌘ D , ⌘ K , and ⌘ U lets me select them one-by-one, but I have to manually exclude the non-variable string matches: ⌘ D 、 ⌘ K和⌘ U让我可以一一选择它们,但我必须手动排除非可变字符串匹配项:
And using Ctrl ⌘ G simply selects all the string matches:使用Ctrl ⌘ G只需选择所有匹配的字符串:
Clearly, Sublime is able to differentiate between variable and string matches.显然,Sublime 能够区分变量匹配和字符串匹配。 Is there no way to select just the variable matches?
有没有办法只选择变量匹配?
Put the cursor in the variable.将光标放在变量中。
Note: the key is to start with an empty selection.注意:关键是从一个空的选择开始。 Don't highlight;
不要突出显示; just put your cursor there.
只需将光标放在那里。
Didn't work?没用? Try again, making sure to start with nothing selected .
再试一次,确保从没有选择任何内容开始。
More commands:更多命令:
Find All: Ctrl ⌘ G selects all occurences at once.
查找全部: Ctrl ⌘ G一次选择所有出现。 Not on a Mac?
不在 Mac 上? Alt F3
Alt F3
Undo Selection: ⌘ U steps backwards.
撤消选择: ⌘ U向后退一步。 Not on a Mac?
不在 Mac 上? Ctrl U
Ctrl U
Quick Skip Next: ⌘ K ⌘ D skips the next occurence.
快速跳过下一个: ⌘ K ⌘ D跳过下一个出现。 Not on a Mac?
不在 Mac 上? Ctrl K Ctrl D
Ctrl K Ctrl D
I know the question is about Macs, but I got here searching the answer for Ubuntu, so I guess my answer could be useful to someone.我知道这个问题是关于 Mac 的,但我来到这里是为了寻找 Ubuntu 的答案,所以我想我的答案可能对某人有用。
Easy way to do it: Alt F3 .简单的方法: Alt F3 。
Despite much effort, I have not found a built-in or plugin-assisted way to do what you're trying to do.尽管付出了很多努力,但我还没有找到一种内置或插件辅助的方式来完成您想要做的事情。 I completely agree that it should be possible, as the program can distinguish
foo
from buffoon
when you first highlight it, but no one seems to know a way of doing it.我完全同意这应该是可能的,因为当您第一次突出显示时,程序可以将
foo
与buffoon
区分开来,但似乎没有人知道这样做的方法。
However, here are some useful key combos for selecting words in Sublime Text 2:但是,这里有一些有用的组合键,用于在 Sublime Text 2 中选择单词:
Ctrl ⌘ G - selects all occurrences of the current word ( Alt F3 on Windows/Linux) Ctrl ⌘ G - 选择所有出现的当前单词(Windows/Linux 上的Alt F3 )
⌘ D - selects the next instance of the current word ( Ctrl D ) ⌘ D - 选择当前单词的下一个实例( Ctrl D )
⌘ E , ⌘ H - uses the current selection as the "Find" field in Find and Replace ( Ctrl E , Ctrl H ) ⌘ E , ⌘ H - 使用当前选择作为查找和替换( Ctrl E , Ctrl H )中的“查找”字段
This worked for me.这对我有用。 Put your cursor at the beginning of the word you want to replace, then
将光标放在要替换的单词的开头,然后
Ctrl K , Ctrl D , Ctrl D ... Ctrl K , Ctrl D , Ctrl D ...
That should select as many instances of the word as you like, then you can just type the replacement.这应该选择尽可能多的单词实例,然后您只需键入替换即可。
神奇的是,你必须从一个空的选择开始,所以把你的光标放在你想要多选的单词/字符前面,然后按Ctrl + D 。
To me, this is the biggest mistake in Sublime.对我来说,这是 Sublime 中最大的错误。 Alt + F3 is hard to reach/remember, and Ctrl + Shift + G makes no sense considering Ctrl + D is "add next instance to selection".
Alt + F3很难到达/记住,考虑到Ctrl + D是“将下一个实例添加到选择”, Ctrl + Shift + G没有任何意义。
Add this to your User Key Bindings (Preferences > Key Bindings):将此添加到您的用户键绑定(首选项 > 键绑定):
{ "keys": ["ctrl+shift+d"], "command": "find_all_under" },
Now you can highlight something, press Ctrl + Shift + D , and it will add every other instance in the file to the selection.现在您可以突出显示某些内容,按Ctrl + Shift + D ,它会将文件中的所有其他实例添加到选择中。
As user1767754 said, the key here is to not make any selection initially.正如 user1767754 所说,这里的关键是最初不要进行任何选择。
Just place the cursor inside the variable name, don't double click to select it.只需将光标放在变量名称内,不要双击选择它。 For single character variables, place the cursor at the front or end of the variable to not make any selection initially.
对于单字符变量,将光标放在变量的前端或末尾,以便最初不进行任何选择。
Now keep hitting Cmd + D for next variable selection or Ctrl + Cmd + G for selecting all variables at once.现在继续点击Cmd + D进行下一个变量选择或Ctrl + Cmd + G一次选择所有变量。 It will magically select only the variables.
它会神奇地只选择变量。
@watsonic 提到,在 macOS 上的 Sublime Text 3 中,从一个空的 selection 开始,只需⌃ ⌘ G (Windows 上的Alt F3 )就可以了,而不是 Sublime Text 2 中的⌘ D + ⌃ ⌘ G。
此时,2020 年 10 月 17 日,如果您选择一个文本元素并按 CTRL+SHIFT+ALT+M,它将突出显示代码块中的每个实例。
以防万一其他人在寻找跨多个文件替换字符串的方法时偶然发现这个问题,它是Command + Shift + F
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.