简体   繁体   中英

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?


Things I've Tried:

D , K , and U lets me select them one-by-one, but I have to manually exclude the non-variable string matches:

在此处输入图片说明

And using Ctrl G simply selects all the string matches:

在此处输入图片说明

Clearly, Sublime is able to differentiate between variable and string matches. Is there no way to select just the variable matches?

  1. Put the cursor in the variable.

    Note: the key is to start with an empty selection. Don't highlight; just put your cursor there.

变量上的文本光标

  1. Press D as needed. Not on a Mac? Use Ctrl D .

突出显示变量的更多实例

Didn't work? Try again, making sure to start with nothing selected .

More commands:

Find All: Ctrl G selects all occurences at once. Not on a Mac? Alt F3

Undo Selection: U steps backwards. Not on a Mac? Ctrl U

Quick Skip Next: K D skips the next occurence. Not on a Mac? Ctrl K Ctrl D

Sublime Docs

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.

Easy way to do it: 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.


However, here are some useful key combos for selecting words in Sublime Text 2:

Ctrl G - selects all occurrences of the current word ( Alt F3 on Windows/Linux)

D - selects the next instance of the current word ( Ctrl D )

  • K , D - skips the current instance and goes on to select the next one ( Ctrl K , Ctrl D )
  • U - "soft undo", moves back to the previous selection ( Ctrl U )

E , H - uses the current selection as the "Find" field in Find and Replace ( 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 ...

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. Alt + F3 is hard to reach/remember, and Ctrl + Shift + G makes no sense considering Ctrl + D is "add next instance to selection".

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.

As user1767754 said, the key here is to not make any selection initially.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM