Sort Text File Lines in VS Code

Sorting lines alphabetically in VS Code is a built-in feature, so you don’t need any extensions to get it done.

Here are the two ways to handle it, depending on whether you prefer the Command Palette or a keyboard shortcut.

Option 1: Using the Command Palette (Easiest)

  1. Select the text you want to sort. If you want to sort the entire file, press Ctrl+A (Windows/Linux) or Cmd+A (Mac).
  2. Open the Command Palette by pressing Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (Mac).
  3. Type „Sort Lines Ascending“ and press Enter.

Option 2: Case Sensitive vs. Case Insensitive

VS Code gives you a few different flavors of sorting depending on how you want to handle Capital letters:

CommandResult
Sort Lines AscendingStandard alphabetical order (A-Z).
Sort Lines DescendingReverse alphabetical order (Z-A).
Sort Lines (Case Sensitive)Groups uppercase (A-Z) separately from lowercase (a-z).
Sort Lines (Unique)Sorts the lines and removes duplicates in one go.

Pro Tip: Multi-Cursor Sorting

If you have multiple blocks of text (like three different lists in one file) and you want to sort them individually:

  1. Highlight the first block.
  2. Hold Alt (or Option on Mac) and highlight the second and third blocks.
  3. Run the Sort Lines Ascending command. VS Code will sort each block independently rather than merging them all together.