Understanding Multiple Selections
The foundation of efficient multi-line editing in Sublime Text is the ability to have multiple cursors active at once. You can create these selections through various methods, such as holding Ctrl (or Cmd on macOS) and clicking different locations, or using the Ctrl+D shortcut to select the next occurrence of the current word. Once multiple cursors are established, any text editor command you execute will apply to every cursor position simultaneously.
This capability extends beyond simple text selection. When you have multiple vertical cursors aligned on different lines, pressing Tab indents all those lines equally, pushing them further to the right. Conversely, pressing Shift+Tab outdents them, moving them back toward the left margin. This behavior is particularly useful when refactoring code blocks, aligning function arguments, or adjusting nested structures where uniform spacing is required.
Basic Indentation with Multiple Cursors
To demonstrate this workflow, consider a scenario where you have three separate lines of code that need to be grouped under a single parent block. First, ensure you have cursors on each of these lines. You can achieve this by selecting the first line, then holding Alt and clicking at the start of the second and third lines to add additional cursors.
- Select the target lines: Place your primary cursor on the first line you wish to modify. Hold Alt (Windows/Linux) or Option (macOS) and click at the desired column position on subsequent lines. This creates vertical cursors.
- Apply indentation: Press the
Tabkey. All lines with active cursors will shift right by one indentation unit (typically two or four spaces, depending on your user settings). - Reverse if necessary: If you have indented too far, press
Shift+Tab. This removes one level of indentation from all selected lines simultaneously.
This process ensures that the relative indentation between lines is preserved while changing their absolute position within the document structure. It eliminates the error-prone process of manually hitting the space bar or tab key for each individual line.
Adjusting Indent Settings
The width of a single indentation step is determined by your project or global preferences. By default, Sublime Text often uses four spaces for Python files and two spaces for JavaScript or HTML, but this varies based on the file type and user configuration. To verify or change this setting, navigate to Preferences > Settings.
In the settings file, look for the tab_size property. If it is not present, you can add it to set the number of spaces that represent one tab character. For example, adding "tab_size": 4 will make the Tab key insert four spaces instead of two. Additionally, ensure that "translate_tabs_to_spaces" is set to true if you prefer spaces over literal tab characters for consistent cross-platform rendering.
When working with multiple selections, understanding these underlying settings is crucial because an incorrect tab_size can lead to misaligned code blocks that are difficult to read. Consistent indentation settings across your team or projects help maintain code quality standards.
Advanced Multi-Column Editing Tips
Beyond basic indentation, multiple selections enable powerful editing patterns. For instance, you can use Ctrl+Shift+L to split the current selection into individual lines, placing a cursor at the end of each line. From there, you can move all cursors up or down to adjust indentation levels dynamically.
Another useful technique involves using regular expressions with multiple selections, though this requires caution. While Sublime Text does not support regex directly in the multi-cursor interface, you can combine it with find-and-replace operations after establishing your cursors. However, for pure structural changes like indentation, the keyboard shortcuts remain the most reliable method.
If you frequently work with deeply nested code, consider installing packages that enhance multi-cursor functionality. Packages like SideBarEnhancements or Emmet can integrate with your existing workflow, but native multi-selection remains the core tool for quick structural adjustments. For related tasks, such as running scripts or managing file formats, you might also explore Running VBScripts in Sublime Text to extend your editor's capabilities beyond standard text manipulation.
Common Pitfalls and Solutions
A common issue arises when users attempt to indent mixed selections—some lines already indented, others not. The result may appear uneven if the base indentation levels differ significantly. Always check the visual alignment after applying Tab or Shift+Tab to ensure the logical structure of your code has not been corrupted.
Another pitfall is accidentally creating horizontal selections instead of vertical cursors. If you drag your mouse across multiple lines, you create a block selection, which behaves differently during indentation. Block selections indent the entire block uniformly, whereas vertical cursors allow for more granular control. Use Alt+Click to switch between these modes effectively.
Frequently Asked Questions
How do I select multiple non-contiguous lines?
You can select multiple non-contiguous lines by holding Ctrl (or Cmd) and clicking at the beginning of each line you wish to edit. Alternatively, hold Alt (or Option) and click to place vertical cursors at specific column positions across different lines, allowing for precise multi-column editing.
Why is my indentation inconsistent after using multiple selections?
Inconsistency usually occurs because the lines had different base indentation levels before you applied the command. Since Tab adds a fixed amount of whitespace to each cursor, lines starting further left will still end up further left than those starting closer to the margin. Ensure all target lines have similar initial spacing before adjusting.
Can I undo multiple indentation actions at once?
Yes, pressing Ctrl+Z (or Cmd+Z on macOS) will undo the last action performed on all cursors simultaneously. Each undo step reverses one application of Tab or Shift+Tab, so you may need to press it multiple times if you indented several levels.
Does this feature work with comments and strings?
Yes, multiple selection indentation applies to all text types, including comments and string literals. If you have cursors inside comment blocks or within quoted strings, pressing Tab will indent those specific segments along with the rest of the line, maintaining the overall structure of the code.
How do I remove trailing whitespace from multiple selections?
While multi-selection handles indentation, removing trailing whitespace is best done via the command palette. Press Ctrl+Shift+P, type "Remove Trailing Whitespace," and execute the command. This operation cleans up all lines in the file, complementing your manual indentation efforts.