Sublime Text: Select all instances of a variable and edit variable name

丶灬走出姿态 提交于 2019-11-27 09:55:36

1. PUT THE CURSOR IN THE VARIABLE

⚠️ the key is to start with an empty selection ⚠️ – don't highlight; just move your cursor there.

2. PRESS D as needed. Not on a Mac? CtrlD

🤔 didn't work? Try again, making sure you started with an empty selection.

🥕 more commands:

Find All: CtrlG Selects all occurences at once. Not on a Mac? AltF3

Undo Selection: U Steps backwards. Not on a Mac? CtrlU

Quick Skip Next: KD Skips the next occurence. Not on a Mac? CtrlKCtrlD

📙 Sublime Docs


h/t adzenith.

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

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:

CtrlG - selects all occurrences of the current word (AltF3 on Windows/Linux)

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

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

E, H - uses the current selection as the "Find" field in Find and Replace (CtrlE,CtrlH)

hobs

This worked for me. Put your cursor at the beginning of the word you want to replace, then

CtrlK, CtrlD, CtrlD ...

That should select as many instances of the word as you like, then you can just type the replacement.

The Magic is, you have to start with an empty selection, so put your cursor infront of the word/character you want to multi-select and Press 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.

It's mentioned by @watsonic that: in Sublime Text 3 on macOS, starting with an empty selection, simply G (AltF3 on Windows) does the trick, instead of D + G in Sublime Text 2.

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.

Just in case anyone else stumbled on this question while looking for a way to replace a string across multiple files, it is Command+Shift+F

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!