Sublime Text:选择变量的所有实例并编辑变量名称

本文翻译自:Sublime Text: Select all instances of a variable and edit variable name

I'm new to Sublime, and I just discovered that 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: 我是Sublime的新手,我刚刚发现,如果我在代码中选择一个变量 (不仅仅是任何字符串),该变量的所有其他实例都会在它们周围得到一个笔划(白色轮廓):

在此输入图像描述

Is there a keyboard shortcut that will let me select all of those instances of the variable and edit them all at once? 是否有键盘快捷键可以让我选择变量的所有实例并一次编辑它们?


Things I've tried: 我试过的事情:

Using D , K , and U lets me select them one-by-one, but I have to manually exclude the non-variable string matches: 使用⌘d,⌘K⌘ü让我一个接一个地选择它们,但我必须手动排除非可变字符串匹配:

在此输入图像描述

And using Ctrl G simply selects all the string matches: 并用Ctrl⌘ 简单地选择所有的字符串匹配:

在此输入图像描述

Clearly, Sublime is able to differentiate between variable- and string-matches. 显然,Sublime能够区分变量匹配和字符串匹配。 Is there no way to select just the variable matches? 有没有办法只选择变量匹配?


#1楼

参考:https://stackoom.com/question/18g41/Sublime-Text-选择变量的所有实例并编辑变量名称


#2楼

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. 我完全同意它应该是可能的,因为当你第一次突出它时,程序可以区分foobuffoon ,但似乎没有人知道这样做的方法。


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⌘ -选择当前单词的所有出现(Alt键 F3在Windows / Linux的)

D - selects the next instance of the current word ( Ctrl D ) ⌘d -选择当前字的下一个实例(Ctrl键 d)

  • K , D - skips the current instance and goes on to select the next one ( Ctrl K , Ctrl D ) ⌘K,⌘d -跳过当前实例并继续选择下一个( 按Ctrl K,Ctrl d)
  • U - "soft undo", moves back to the previous selection ( Ctrl U ) ⌘ü - “软撤消”,移回到先前的选择(Ctrl键 U)

E , H - uses the current selection as the "Find" field in Find and Replace ( Ctrl E , Ctrl H ) ⌘E,⌘^ h -使用当前的选择作为“查找”字段中查找和替换( 按Ctrl E, 按Ctrl 1H)


#3楼

1. PUT THE CURSOR IN THE VARIABLE 1.将光标放在变量中

⚠️ the key is to start with an empty selection ⚠️ – don't highlight; ⚠️关键是从一个空的选择开始⚠️ - 不要突出显示; just move your cursor there. 只需光标移动到那里。

文本光标在变量上

2. PRESS D as needed. 2.按 ⌘d根据需要。 Not on a Mac? 不在Mac上? Ctrl D Ctrl D

突出显示更多变量实例

🤔 didn't work? 🤔没用 Try again, making sure you started with an empty selection . 再试一次,确保以空选择开始。

🥕 more commands: 🥕 更多命令:

Find All: Ctrl G Selects all occurences at once. 查找所有 :Ctrl⌘ 选择所有出现一次。 Not on a Mac? 不在Mac上? Alt F3 Alt F3

Undo Selection: U Steps backwards. 撤消选择:⌘ü向后退。 Not on a Mac? 不在Mac上? Ctrl U Ctrl U.

Quick Skip Next: K D Skips the next occurence. 快速跳过,下一步:⌘ķ⌘d跳到下一个occurence。 Not on a Mac? 不在Mac上? Ctrl K Ctrl D Ctrl K Ctrl D.

📙 Sublime Docs 📙 崇高文件


h/t adzenith . h / t adzenith


#4楼

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 KCtrl DCtrl D ...

That should select as many instances of the word as you like, then you can just type the replacement. 这应该选择任意数量的单词实例,然后你可以输入替换。


#5楼

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


#6楼

魔术是,你必须从一个空的选择开始,所以把你的光标放在想要多选的单词/字符的前面,然后按Ctrl + D

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章