How Superfile's Clipboard Works Across Multiple Panels
Superfile implements a single shared clipboard model stored in the main application state, enabling seamless copy-cut-paste operations that persist across all file panels regardless of which panel currently holds focus.
The yorukot/superfile terminal file manager uses a centralized clipboard architecture to synchronize file operations across its multi-panel interface. Unlike isolated per-panel buffers, superfile maintains one source of truth for copied and cut items, allowing users to transfer files between directories efficiently. This article examines the source code implementation behind superfile's clipboard functionality across multiple panels.
Architecture of the Shared Clipboard
The clipboard system centers on a dedicated model that lives outside individual panel scopes. In [src/internal/type.go](https://github.com/yorukot/superfile/blob/main/src/internal/type.go#L58-L59), the main model struct contains a clipboard field that instantiates clipboard.Model from [src/internal/ui/clipboard/model.go](https://github.com/yorukot/superfile/blob/main/src/internal/ui/clipboard/model.go). This single instance is accessible to all file panels, ensuring that any operation modifying the clipboard immediately reflects across the entire interface.
The Clipboard Data Structure
The Model struct stores file paths and operational metadata. It provides methods including Reset(cut bool), Add(item string), SetItems(items []string), and IsCut() to manage state. The struct maintains a collection of items and a boolean flag indicating whether the current operation is a cut or copy, which determines whether items clear after pasting.
Global State Integration
By embedding the clipboard in the main model rather than individual file panels, superfile ensures persistence across focus changes. When users switch between panels using navigation hotkeys, the underlying m.clipboard reference remains constant, preserving the path collection and cut flag for subsequent operations.
Copy and Cut Operations Across Panels
File operations originate in [src/internal/handle_file_operations.go](https://github.com/yorukot/superfile/blob/main/src/internal/handle_file_operations.go), where handlers manipulate the shared clipboard regardless of which panel initiated the action. For single-item operations at line 243, the code calls m.clipboard.Reset(cut) at line 247 to initialize the operation type, then invokes m.clipboard.Add(panel.GetFocusedItem().Location) at line 253 to store the focused item's path.
For batch selections, the handler at line 256 processes multiple items by calling m.clipboard.Reset(cut) at line 259, followed by m.clipboard.SetItems(items) at line 266. This populates the clipboard with all selected paths from the currently focused panel, making them available to any other panel in the interface.
// Single item copy from any panel (cut = false)
m.clipboard.Reset(false)
m.clipboard.Add(panel.GetFocusedItem().Location)
// Batch cut operation across panels (cut = true)
m.clipboard.Reset(true)
m.clipboard.SetItems(selectedPaths)
Paste Operations and State Validation
The paste workflow demonstrates how the shared clipboard functions across panel boundaries. In [src/internal/handle_file_operations.go](https://github.com/yorukot/superfile/blob/main/src/internal/handle_file_operations.go#L269) at line 269, the paste command retrieves clipboard contents through m.clipboard.PruneInaccessibleItemsAndGet() at line 270, which validates paths and removes stale entries. The function checks the operation type using m.clipboard.IsCut() at line 271 to determine whether to delete source files after copying.
After executing the paste into the currently focused panel's location, the clipboard clears automatically only if IsCut() returns true, preserving copied items for additional pastes into other panels.
copyItems := m.clipboard.PruneInaccessibleItemsAndGet()
cut := m.clipboard.IsCut()
m.executePasteOperation(&m.processBarModel, targetLocation, copyItems, cut, reqID)
Rendering and UI Consistency
Visual feedback remains consistent during panel switches through the Render() method in [src/internal/ui/clipboard/model.go](https://github.com/yorukot/superfile/blob/main/src/internal/ui/clipboard/model.go#L31-L58) (lines 31-58). This method constructs the clipboard panel view displayed in the footer, querying filesystem metadata via os.Lstat to indicate file types. Since the main rendering loop calls this method every frame, users see identical clipboard contents regardless of active panel focus.
Safety Mechanisms and Layout Validation
Superfile implements defensive checks to prevent errors during cross-panel operations. Beyond pruning inaccessible items before paste operations, the application validates clipboard panel dimensions in [src/internal/validation.go](https://github.com/yorukot/superfile/blob/main/src/internal/validation.go#L357-L364) (lines 357-364). This ensures the clipboard display fits within the terminal layout constraints, preventing rendering errors during window resizing or panel reconfiguration.
Summary
- Single shared instance: The
clipboard.Modelstored in the main application model at [src/internal/type.go](https://github.com/yorukot/superfile/blob/main/src/internal/type.go#L58-L59) provides one clipboard for all panels - Panel-agnostic operations: Copy and cut handlers in [
src/internal/handle_file_operations.go](https://github.com/yorukot/superfile/blob/main/src/internal/handle_file_operations.go) useReset()andAdd()orSetItems()to populate the shared state from any panel - Persistent across focus: Switching panels does not clear the clipboard; contents remain available via the central model reference
- Validation before execution:
PruneInaccessibleItemsAndGet()filters stale paths before paste operations execute - Conditional clearing: The clipboard only clears after paste if
IsCut()returns true, enabling multiple pastes for copy operations
Frequently Asked Questions
Does superfile maintain separate clipboards for each file panel?
No. Superfile implements a single shared clipboard model defined in [src/internal/ui/clipboard/model.go](https://github.com/yorukot/superfile/blob/main/src/internal/ui/clipboard/model.go) and embedded in the main application state at [src/internal/type.go](https://github.com/yorukot/superfile/blob/main/src/internal/type.go#L58-L59). All file panels reference this central instance through m.clipboard, ensuring that clipboard contents persist when you navigate between panels.
How does superfile handle deleted or moved files still in the clipboard?
Before executing any paste operation, superfile calls PruneInaccessibleItemsAndGet() at line 270 of [src/internal/handle_file_operations.go](https://github.com/yorukot/superfile/blob/main/src/internal/handle_file_operations.go). This method removes paths that no longer exist on the filesystem, ensuring the paste operation only attempts to process valid files and directories.
Can I paste clipboard contents into multiple different target panels?
Yes. The clipboard only clears automatically after a paste if the original operation was a cut, determined by the boolean flag checked via IsCut() at line 271. Copy operations leave items in the clipboard, allowing you to paste the same files into multiple target directories across different panels without recopying.
Where does superfile display the current clipboard contents?
The clipboard renders as a footer panel through the Render() method in [src/internal/ui/clipboard/model.go](https://github.com/yorukot/superfile/blob/main/src/internal/ui/clipboard/model.go#L31-L58) (lines 31-58). This view integrates into the main UI layout, displaying current items and their file types regardless of which panel currently holds focus.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →