Ora

How do you expand selection in Sublime?

Published in Sublime Text Selection 5 mins read

Expanding your selection in Sublime Text is a highly efficient way to highlight specific parts of your code or text, enabling quick editing, refactoring, or navigation. Sublime Text offers several intuitive keyboard shortcuts to expand your selection based on common structural elements like scope, brackets, or indentation.

The most direct methods for expanding your selection are:

  • Expand Selection to Scope: This selects the current logical block of code or text.
  • Expand Selection to Brackets: This highlights everything within the nearest pair of brackets.
  • Expand Selection to Indentation: This selects the block of text at the current indentation level.

Mastering Selection Expansion in Sublime Text

Efficient selection is a cornerstone of rapid text editing in any powerful editor, and Sublime Text excels in providing tools to precisely control your selections. By understanding and utilizing its selection expansion features, you can significantly speed up your workflow.

Key Selection Expansion Commands

Sublime Text provides powerful shortcuts to expand your selection based on the structure of your code or text. These commands are invaluable for tasks like refactoring functions, moving blocks of HTML, or modifying parameters within a method call.

Expand Selection to Scope

The "Expand Selection to Scope" command intelligently selects the logical code block or syntactic unit where your cursor is currently located. This is incredibly useful for quickly selecting an entire function body, a CSS rule set, or an XML tag's content. Sublime Text's syntax highlighting engine determines these "scopes."

  • Shortcut: Ctrl + Shift + Space (Windows/Linux), Cmd + Shift + Space (macOS)
  • Practical Use:
    • Place your cursor inside a function, and pressing the shortcut will select the entire function body.
    • In an HTML file, place the cursor within a <div> tag, and it will select the div and its contents.
    • Repeatedly pressing the shortcut will expand the selection to the parent scope, moving up the syntactic hierarchy.

Expand Selection to Brackets

This command allows you to effortlessly select the content enclosed within the nearest pair of (), {}, [], or <> characters. It's particularly useful in programming languages for selecting function arguments, array elements, or object properties.

  • Shortcut: Ctrl + Shift + M (Windows/Linux), Cmd + Shift + M (macOS)
  • Practical Use:
    • If your cursor is inside my_array[index], pressing Ctrl + Shift + M will select index.
    • If your cursor is inside function_call(param1, param2), it will select param1, param2.
    • Pressing the shortcut again will select the brackets themselves along with their contents.

Expand Selection to Indentation

For languages that rely heavily on indentation (like Python) or for structured text files, expanding the selection to the current indentation level is a quick way to select entire blocks of code.

  • Shortcut: Ctrl + Shift + J (Windows/Linux), Cmd + Shift + J (macOS)
  • Practical Use:
    • Place your cursor on a line within an indented block (e.g., inside an if statement or a loop).
    • Pressing Ctrl + Shift + J will select all lines at that exact indentation level.
    • This is ideal for quickly selecting and moving or deleting a block of code with consistent indentation.

Other Useful Selection Techniques

Beyond the dedicated expansion commands, Sublime Text offers other robust selection features that complement the expansion functionality:

  • Select Word: Ctrl + D (Windows/Linux), Cmd + D (macOS). Repeatedly pressing selects the next occurrence of the word, allowing for multiple cursors.
  • Select Line: Ctrl + L (Windows/Linux), Cmd + L (macOS). Selects the entire line the cursor is on. Repeatedly pressing selects subsequent lines.
  • Column Selection (Block Selection): Hold Shift + Right-click and drag, or use Ctrl + Shift + Up/Down (Windows/Linux), Cmd + Shift + Up/Down (macOS) to create multiple cursors for vertical block editing.
  • Select All: Ctrl + A (Windows/Linux), Cmd + A (macOS).
  • Select to Start/End of Line: Shift + Home/Shift + End (Windows/Linux), Shift + Cmd + Left/Shift + Cmd + Right (macOS).

Summary of Key Selection Shortcuts

Here’s a quick reference table for essential selection and expansion shortcuts in Sublime Text:

Action Windows / Linux Shortcut macOS Shortcut Description
Expand Selection to Scope Ctrl + Shift + Space Cmd + Shift + Space Selects the current logical code block or syntactic unit.
Expand Selection to Brackets Ctrl + Shift + M Cmd + Shift + M Selects content within the nearest (), {}, [], or <>.
Expand Selection to Indentation Ctrl + Shift + J Cmd + Shift + J Selects lines at the current indentation level.
Select Word / Multi-select Ctrl + D Cmd + D Selects current word, subsequent presses select next occurrences.
Select Line Ctrl + L Cmd + L Selects the current line.
Column Selection (multi-cursor) Shift + Right-click (drag) / Ctrl + Shift + Up/Down Shift + Right-click (drag) / Cmd + Shift + Up/Down Creates multiple cursors for vertical editing.
Select All Ctrl + A Cmd + A Selects the entire content of the file.
Select to Line Start Shift + Home Shift + Cmd + Left Extends selection from cursor to the beginning of the line.
Select to Line End Shift + End Shift + Cmd + Right Extends selection from cursor to the end of the line.

Customizing Key Bindings

If you find that these default shortcuts conflict with your workflow or if you prefer a different key combination, Sublime Text allows for extensive customization of its key bindings. You can access these settings via Preferences > Key Bindings to modify existing commands or add new ones. For more information on customizing Sublime Text, refer to the official Sublime Text Documentation.

By integrating these selection expansion techniques into your daily editing habits, you'll find yourself navigating and manipulating text with unparalleled speed and precision in Sublime Text.