简体   繁体   中英

Sublime Text 3 Jump through the highlighted word

With Visual Studio I can use the command Edit.NextHighlightedReference and Edit.PreviousHighlightedReference which are mapped to Ctrl + Shift + and Ctrl + Shift + . Using this command I can jump through the current highlighted word.

Does Sublime Text 3 have a similar command or keyboard shortcut that I can use?

I don't use Visual Studio, but according to this MSDN blog entry and it's description of this feature, it's possible to do something similar in Sublime.

The blog entry describes the feature as automatically highlighting all of the references to the symbol under the caret and allowing you to jump between those references in the current file with key bindings.

In Sublime it's possible to navigate between references to a symbol as well as to the definition of a symbol, but this is done based on the syntax in use and doesn't have the granularity that is required here.

You can see this in action by hovering your mouse over a symbol. If you have show_definitions and index_files turned on in your settings (they're both turned on by default) and the text under the cursor is a symbol, you'll be shown a popup that allows you to navigate to the definition and/or references.

Note that there is no "deep analysis" done on the symbol, so for example two open files with similar method names may be intermixed in the list, if the symbol isn't something like a method or function it may not be included, etc.

Additionally, although you can bind keys to these actions, if there is more than one reference you will be prompted with a quick panel instead of cycling between them.

Third party packages can enhance this by doing their own analysis of the code. Possibly the SublimeCodeIntel package does this, although I don't use it myself so I can't say for sure.

You can sort of replicate what you want in core Sublime by utilizing the search functionality in the current buffer, which may be enough depending on your exact needs. A downside of this is that it doesn't constrain the results to just the symbol, but any matching text (comments, similarly named variables, etc).

The workflow works something like this (key bindings here are for Windows/Linux):

  1. Place the cursor in the symbol and press Ctrl+D ; Sublime will select the word under the cursor and highlight the other occurrences in the buffer; You can also instead press Ctrl+F3 which will do this and then immediately jump to the next match.
  2. Press F3 to search forwards through the other occurrences in the current buffer, eventually wrapping around back to where you started.
  3. Press Shift+F3 to search backwards instead of forwards

Note that this only works when you use Ctrl+F3 or Ctrl+D to select; regular selections have no effect on this. If you do that, pressing the search keys will continue the search you used last (which can be a good thing or a bad thing depending on what you intended to do).

Adding the following to your custom key bindings would allow you to use the key bindings you're used to for navigating between the matches (although they override default keys for swap_line_up and swap_line_down ):

{
    "keys": ["ctrl+shift+up"], "command": "find_prev"
},
{
    "keys": ["ctrl+shift+down"], "command": "find_next"
},

If you're interested, the command find_under is what is mapped to Ctrl+F3 and find_under_expand is bound to Ctrl+D , should you want to bind those to something else as well.

For completeness, the associated key bindings for MacOS are ⌘+Alt+G and ⌘+D for the two commands that start the search and ⌘+G and ⌘+Shift+G for going forwards and backwards through the matches.

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