Skip to content

cli: Add --reuse flag for replacing workspace in existing window - #38131

Merged
ConradIrwin merged 17 commits into
zed-industries:mainfrom
mateonoel2:main
Oct 21, 2025
Merged

cli: Add --reuse flag for replacing workspace in existing window#38131
ConradIrwin merged 17 commits into
zed-industries:mainfrom
mateonoel2:main

Conversation

@mateonoel2

@mateonoel2 mateonoel2 commented Sep 14, 2025

Copy link
Copy Markdown
Contributor

Closes #ISSUE

it is was still in discussion

Release Notes:

  • Added: --reuse (-r) CLI flag to replace the workspace in an existing window instead of opening a new one

This PR adds a new --reuse (-r) CLI flag that allows users to replace the workspace in an existing Zed window instead of opening a new one or adding files to the current workspace.

What it does

The --reuse flag finds an available local workspace window and replaces its workspace with the newly specified paths. This provides a third workspace opening mode alongside the existing --add and --new flags.

Implementation Details

  • CLI Flag: Added --reuse (-r) flag with proper mutual exclusion with --add and --new
  • Window Replacement: Uses the existing replace_window option in workspace::OpenOptions
  • Window Selection: Reuses the first available local workspace window
  • Fallback Behavior: When no existing windows are found, creates a new window
  • Test Coverage: Added comprehensive test for the reuse functionality

Behavior

  • zed -r file.txt - Replaces the workspace in an available window with file.txt
  • If no windows are open, creates a new window (same as default behavior)
  • Mutually exclusive with -a/--add and -n/--new flags
  • Works with multiple files and directories

Files Changed

  • crates/cli/src/cli.rs - Added reuse field to CliRequest::Open
  • crates/cli/src/main.rs - Added CLI argument definition and parsing
  • crates/zed/src/zed/open_listener.rs - Implemented reuse logic and added tests
  • crates/zed/src/zed/windows_only_instance.rs - Updated for Windows compatibility

Testing

  • ✅ Unit tests pass
  • ✅ Manual testing confirms expected behavior:
    • Works when no windows are open
    • Replaces workspace in existing window
    • Maintains compatibility with existing -a and -n flags
    • Proper help text display

Manual testing

In this first video we do a couple of tests:

  • 1: What happens if we use the -r flag when there are no windows open?
    - works as expected. It opens the files in a new window.

  • 2: Does it work as expected if there is already a window open. Does it overrides the workspace?
    - yes it does. When opening a different file it overrides the current window instead of creating a new one.

  • 3: Does the -n flag still works as expected?
    - yes, it creates the project in a new window

  • 4: What about the -a flag?
    - yes, on the last accessed page

  • 5: we do the replace command. It overrides the first opened window, do we want this behavior?
    - It is good enough that it overrides one of the opened windows with the new project. It still makes the user automatically go to the window with the specified files

  • 6: we use the -r command again replacing the workspace with a new one.
    - this indeed worked as expected

more_cli_testing.mp4

In here the we check how the --help flag now displays the new command. (Description was later updated)

comparing_cli_help.mp4

@cla-bot cla-bot Bot added the cla-signed The user has signed the Contributor License Agreement label Sep 14, 2025
@kevinnkansah

This comment was marked as spam.

@maxdeviant maxdeviant changed the title feat: Add --reuse/-r flag to CLI for replacing workspace in existing window cli: Add --reuse/-r flag for replacing workspace in existing window Sep 14, 2025
@maxdeviant maxdeviant changed the title cli: Add --reuse/-r flag for replacing workspace in existing window cli: Add --reuse flag for replacing workspace in existing window Sep 14, 2025

@kevinnkansah kevinnkansah left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the intended functionalities i wanted in Zed

Updated the thread spawning mechanism for the CLI receiver to use `thread::Builder` for improved clarity and customization. This change enhances the readability of the code while maintaining the existing functionality.

Release Notes:
- Refactored thread spawning to use `thread::Builder` instead of `thread::spawn`.
Introduced a new `reuse` parameter in the CLI's main function to allow for reusing existing workspaces. This enhancement improves user experience by providing more flexibility in workspace management.

Release Notes:
- Added `reuse` option to the CLI for workspace handling.
@ConradIrwin
ConradIrwin merged commit 4b489f4 into zed-industries:main Oct 21, 2025
22 checks passed
@ConradIrwin

Copy link
Copy Markdown
Member

Thanks for this!

@kevinnkansah

Copy link
Copy Markdown

thank you @mateonoel2 i no longer require vs code!

@JonGretar

Copy link
Copy Markdown
Contributor

Thanks. This might not seem such a big change, but it shaves off a couple seconds many times a day for me.

@o3wiz

o3wiz commented Oct 29, 2025

Copy link
Copy Markdown

amazing feature!

@olafgeibig

Copy link
Copy Markdown

I'm really disappointed that you made this the default behavior. I'm a CLI first user and my workflow is to change into a directory and then do zed . To be honest, I think this is really not okay to change the default behavior on the CLI. This is not what users who live mostly on the CLI expect. I will create an alias for me now, but still I'm a bit angry about this. At least there should be an option to configure the default behavior

@injust

injust commented Apr 27, 2026

Copy link
Copy Markdown
Contributor

At least there should be an option to configure the default behavior

@olafgeibig cli_default_open_behavior?

@JonGretar

Copy link
Copy Markdown
Contributor

I think this is really not okay to change the default behavior on the CLI.

It asked you what the default should be the first time you ran it after the update. You must have just 'enter'-ed your way past it.

@olafgeibig

Copy link
Copy Markdown

@olafgeibig cli_default_open_behavior?
Oh did not see this option. I somehow missed it. My bad. Then I'm happy!

jolutz pushed a commit to jolutz/zed that referenced this pull request Aug 8, 2026
…ed-industries#38131)

Closes #ISSUE 

it is was still in
[discussion](zed-industries#37983)

Release Notes:

- Added: `--reuse` (`-r`) CLI flag to replace the workspace in an
existing window instead of opening a new one

This PR adds a new `--reuse` (`-r`) CLI flag that allows users to
replace the workspace in an existing Zed window instead of opening a new
one or adding files to the current workspace.

### What it does

The `--reuse` flag finds an available local workspace window and
replaces its workspace with the newly specified paths. This provides a
third workspace opening mode alongside the existing `--add` and `--new`
flags.

### Implementation Details

- **CLI Flag**: Added `--reuse` (`-r`) flag with proper mutual exclusion
with `--add` and `--new`
- **Window Replacement**: Uses the existing `replace_window` option in
`workspace::OpenOptions`
- **Window Selection**: Reuses the first available local workspace
window
- **Fallback Behavior**: When no existing windows are found, creates a
new window
- **Test Coverage**: Added comprehensive test for the reuse
functionality

### Behavior

- `zed -r file.txt` - Replaces the workspace in an available window with
`file.txt`
- If no windows are open, creates a new window (same as default
behavior)
- Mutually exclusive with `-a/--add` and `-n/--new` flags
- Works with multiple files and directories

### Files Changed

- `crates/cli/src/cli.rs` - Added `reuse` field to `CliRequest::Open`
- `crates/cli/src/main.rs` - Added CLI argument definition and parsing
- `crates/zed/src/zed/open_listener.rs` - Implemented reuse logic and
added tests
- `crates/zed/src/zed/windows_only_instance.rs` - Updated for Windows
compatibility

### Testing

- ✅ Unit tests pass
- ✅ Manual testing confirms expected behavior:
  - Works when no windows are open
  - Replaces workspace in existing window
  - Maintains compatibility with existing `-a` and `-n` flags
  - Proper help text display


## Manual testing

#### In this first video we do a couple of tests: 

* **1**: What happens if we use the -r flag when there are no windows
open?
        - works as expected. It opens the files in a new window.
        
* **2**: Does it work as expected if there is already a window open.
Does it overrides the workspace?
- yes it does. When opening a different file it overrides the current
window instead of creating a new one.
        
* **3**: Does the -n flag still works as expected?
        - yes, it creates the project in a new window

* **4**: What about the -a flag?
       - yes, on the last accessed page 
       
* **5**: we do the replace command. It overrides the first opened
window, do we want this behavior?
- It is good enough that it overrides one of the opened windows with the
new project. It still makes the user automatically go to the window with
the specified files

* **6**: we use the -r command again replacing the workspace with a new
one.
       - this indeed worked as expected


https://github.com/user-attachments/assets/f1cd7f4b-f4af-4da2-a755-c0be7ce96c0d


#### In here the we check how the --help flag now displays the new
command. (Description was later updated)


https://github.com/user-attachments/assets/a8a7a288-d926-431b-a9f9-a8c3d909a2ec
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla-signed The user has signed the Contributor License Agreement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants