Skip to content

Using Ctrl+X, Ctrl+E for Better Terminal Command Editing

Posted on:June 29, 2025 at 02:30 PM

Using Ctrl+X, Ctrl+E for Better Terminal Command Editing

I’ve been using the terminal for years, but recently discovered a useful keyboard shortcut that’s improved how I work with complex commands. If you haven’t come across it yet, Ctrl+X, Ctrl+E might be worth adding to your workflow.

How It Works

When you’re typing a command and press Ctrl+X followed by Ctrl+E, your default editor opens with whatever you’ve typed so far. You can then edit the command using your editor’s full functionality, save and close, and the command executes immediately.

A Practical Example

When working with a complex command like:

curl -X POST \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer ${API_TOKEN}" \
  -H "User-Agent: MyApp/1.0" \
  -H "Accept: application/json" \
  -H "X-Request-ID: $(uuidgen)" \
  -H "X-Client-Version: 2.1.0" \
  -H "Cache-Control: no-cache" \
  -H "Accept-Encoding: gzip, deflate" \
  -d '{
    "query": "mutation CreateUser($input: UserInput!) { createUser(input: $input) { id name email } }",
    "variables": {
      "input": {
        "name": "John Doe",
        "email": "john@example.com",
        "role": "admin"
      }
    }
  }' \
  https://api.example.com/graphql

If you realize there’s a typo near the beginning, your usual options are:

  1. Navigate back using arrow keys
  2. Use Ctrl+A to jump to the beginning, then arrow to the right position
  3. Cancel and start over

Instead, Ctrl+X, Ctrl+E opens your editor with the command ready to modify. You can use familiar editor shortcuts to fix the issue, save, and have the corrected command execute automatically.

Common Use Cases

This shortcut is useful in several scenarios:

Multi-line commands: You can write complex shell scripts on the fly, including proper loops, conditionals, and functions.

Command history editing: When you want to modify a complex command from your history that’s difficult to edit inline.

Building from templates: Start with a basic command structure, then expand it into something more comprehensive in your editor.

Configuration

This works out of the box in most shells (bash, zsh) on most systems. Your terminal uses whatever’s set in your $EDITOR environment variable.

If you don’t have $EDITOR set, add this to your .bashrc or .zshrc:

export EDITOR=vim  # or nano, emacs, code, whatever you prefer

For VS Code users, you can even use:

export EDITOR="code --wait"

The --wait flag is crucial - it tells the terminal to wait for you to close the editor before proceeding.

Testing It Out

To try this shortcut:

  1. Start typing any command (such as ls -la)
  2. Press Ctrl+X, then Ctrl+E
  3. Your editor should open with the command
  4. Make any change (or don’t), save and quit
  5. The command will execute

This shortcut can be particularly helpful when working with complex commands that are difficult to edit using only terminal navigation. It provides access to your editor’s full feature set while staying within your terminal workflow.