How to Sort Lines Alphabetically in VS Code
Sorting lines alphabetically in VS Code is one of those tasks that sounds simple but can save you significant time when working with large files, configuration lists, import statements, or data exports. VS Code has a built-in command for this — and it takes less than a second to run.
Using the Built-In Sort Lines Command in VS Code
VS Code has a native command palette option for sorting lines. Here is how to use it:
- Open your file in VS Code
- Select the lines you want to sort — or press Ctrl+A (Windows/Linux) or Cmd+A (Mac) to select everything
- Open the Command Palette with Ctrl+Shift+P (Windows/Linux) or Cmd+Shift+P (Mac)
- Type Sort Lines Ascending and press Enter
Your selected lines will be sorted in ascending alphabetical order (A to Z) instantly. For descending order (Z to A), use Sort Lines Descending from the same command palette.
What is the keyboard shortcut to sort lines in VS Code?
VS Code does not assign a default keyboard shortcut to Sort Lines Ascending. You can create a custom keybinding by going to File → Preferences → Keyboard Shortcuts, searching for “Sort Lines Ascending”, and clicking the plus icon to assign any shortcut you want — for example Alt+Shift+S.
Sort Lines Alphabetically Using a VS Code Extension
If you need more advanced sorting options — case-insensitive sorting, sorting by line length, reversing order, or sorting specific columns — the Sort Lines extension by Daniel Imms is the most widely used solution. Install it from the VS Code Marketplace, then access all its options from the Command Palette by typing “Sort”.
The extension adds these commands:
- Sort Lines Ascending — A to Z, case-sensitive
- Sort Lines Descending — Z to A, case-sensitive
- Sort Lines Case Insensitive Ascending — ignores uppercase/lowercase
- Sort Lines by Length — shortest to longest
- Sort Lines Reverse — reverses current order without sorting alphabetically
- Sort Lines Unique — sorts and removes duplicate lines
Can I sort only selected lines in VS Code, not the whole file?
Yes. Select only the lines you want to sort before running the command. VS Code applies Sort Lines Ascending or Descending only to your selected text. If no text is selected, it sorts the entire file. This is useful when you want to sort a specific list within a larger file without affecting the surrounding code.
Sort Lines Online Without VS Code
If you are working outside VS Code — on a different machine, in a browser, or with a file type that isn’t code — you can sort lines instantly using Format Pilot’s free text utilities. Paste your content, click Sort Lines, and copy the result. No installation, no extension, no account.
Common Use Cases for Sorting Lines in VS Code
Alphabetical line sorting is useful across many development workflows. Sorting import statements at the top of a JavaScript or Python file keeps them tidy and makes merge conflicts easier to resolve. Sorting CSS properties alphabetically within each rule makes stylesheets consistent and predictable. Sorting JSON object keys alphabetically helps when comparing two versions of a config file. Sorting lists of environment variables, API endpoints, or database column names makes them easier to scan and maintain.
Sort Lines in VS Code Terminal Using the Command Line
If you prefer the terminal over the GUI, you can sort a file’s lines from VS Code’s integrated terminal using the sort command on Mac and Linux:
sort filename.txt
To sort and save the output back to the same file:
sort filename.txt -o filename.txt
On Windows using PowerShell in the VS Code terminal:
Get-Content filename.txt | Sort-Object | Set-Content filename.txt
Frequently Asked Questions
Does VS Code have a built-in sort command?
Yes. VS Code includes Sort Lines Ascending and Sort Lines Descending in the Command Palette (Ctrl+Shift+P / Cmd+Shift+P). Select your text, open the palette, and type Sort Lines to access both options without any extensions.
How do I sort lines without duplicates in VS Code?
Install the Sort Lines extension, then use the Sort Lines Unique command. It sorts all selected lines alphabetically and removes any duplicate lines in one step.
Does sorting lines in VS Code affect indentation?
Yes — line sorting treats each line as a complete string, including any leading whitespace. Indented lines will sort based on their leading spaces or tabs first. For code blocks where indentation matters (like nested functions or HTML), sort selectively by highlighting only the lines you want reordered.