Editing multiple lines in Sublime Text is a powerful feature that significantly boosts productivity, allowing you to make identical changes across several lines or instances of text simultaneously.
How to Edit Multiple Lines in Sublime Text
Sublime Text offers several intuitive methods for multi-line editing, catering to different scenarios, from simple column selection to advanced find-and-replace-like multi-selections. Mastering these techniques will streamline your coding and text manipulation tasks.
1. Multiple Selection (Column Editing)
This method is ideal when you need to edit text in a straight column or add/remove content at the same horizontal position across multiple lines.
- Using Mouse Drag:
- Windows/Linux: Hold
Shift
+Right Click
and drag your mouse vertically to select a column of text or place multiple cursors. - macOS: Hold
Option
(orAlt
) +Click
and drag your mouse vertically. You can also useCtrl
+Shift
+Click
to place individual cursors.
- Windows/Linux: Hold
- From Selected Lines:
- Select the lines you want to edit.
- Press
Ctrl + Shift + L
(Windows/Linux) orCmd + Shift + L
(macOS). This will split your selection into multiple cursors at the end of each selected line, or at the start if nothing is selected.
2. Find and Replace Multi-Selection
This is arguably the most frequently used and versatile method, particularly for editing non-contiguous instances of text.
- Select Next Instance:
- Select the first instance of the text you want to edit.
- Press
Ctrl + D
(Windows/Linux) orCmd + D
(macOS). This action selects the next occurrence of the currently selected text. You can repeatedly press this shortcut to select more instances one by one. This is a very common method for multi-line editing. - If you select an instance, then press Cmd+D to select the next instance of selected text.
- Skip an Instance:
- While using
Ctrl + D
orCmd + D
to select instances, if you encounter an instance you wish to skip, pressCtrl + K, Ctrl + D
(Windows/Linux) orCmd + K, Cmd + D
(macOS). This deselects the current instance and moves the focus to the next, allowing you to pick and choose your edits precisely.
- While using
- Select All Instances:
- Select the text you want to modify across the entire document.
- Press
Alt + F3
(Windows/Linux) orCtrl + Cmd + G
(macOS). This will instantly select all occurrences of the selected text within the file, placing a cursor at the end of each, ready for simultaneous editing.
- Manual Cursor Placement:
- Hold
Ctrl
(Windows/Linux) orCmd
(macOS) and click at any desired location to place multiple, independent cursors. This gives you absolute control over cursor placement.
- Hold
Key Shortcuts for Multi-Line Editing
Here's a quick reference table for the essential multi-line editing shortcuts in Sublime Text:
Action | Windows/Linux Shortcut | macOS Shortcut | Description |
---|---|---|---|
Column Selection | Shift + Right Click drag |
Option + Click drag |
Selects a vertical column of text or places cursors. |
Split Selection to Lines | Ctrl + Shift + L |
Cmd + Shift + L |
Places cursors at the end of each selected line. |
Select Next Instance | Ctrl + D |
Cmd + D |
Selects the next occurrence of the current selection. |
Skip Instance | Ctrl + K, Ctrl + D |
Cmd + K, Cmd + D |
Skips the current instance and selects the next. |
Select All Instances | Alt + F3 |
Ctrl + Cmd + G |
Selects all occurrences of the current selection in the file. |
Manual Cursor Placement | Ctrl + Click |
Cmd + Click |
Places an independent cursor at the clicked location. |
For more detailed information, you can always refer to the official Sublime Text documentation.
Practical Use Cases and Tips
- Renaming Variables/Functions: Quickly update all instances of a variable name throughout a scope.
- Adding/Removing Prefixes/Suffixes: Append or prepend text to multiple lines, like adding a bullet point to a list or a file extension.
- Editing Lists or CSV Data: Modify specific columns or elements across many rows.
- Refactoring Code: Efficiently change argument names, function calls, or HTML tags.
- Combine with other commands: Once you have multiple cursors, you can use other standard editing commands (e.g.,
Delete
,Backspace
,Ctrl+Shift+K
for deleting entire lines,Ctrl+Shift+V
to paste each line of clipboard as a new selection) for even more complex manipulations.
By leveraging these multi-line editing features, you can significantly reduce repetitive tasks and enhance your editing workflow in Sublime Text.