Selection to File - VS Code Extension
A simple VS Code extension that allows you to quickly copy, move, append, or remotely copy selected text to files.
Features
- Copy selection to file: Copy selected text to a new file while keeping the original
- Move selection to file: Cut selected text and move it to a new file
- Append selection to file: Append selected text to an existing file (or create if it doesn't exist)
- Copy selection to remote file: Copy selected text to a file on a remote server via SSH
- Append selection to remote file: Append selected text to a remote file via SSH (or create if it doesn't exist)
All commands are available in the right-click context menu when you have text selected.
Usage
Local File Operations
- Select the text you want to work with
- Right-click to open the context menu
- Choose one of:
- "Copy selection to file" - copies the selection to a new file
- "Move selection to file" - cuts and moves the selection to a new file
- "Append selection to file" - appends the selection to an existing file (creates if doesn't exist)
- Enter the file path relative to the current file or an absolute path
- Press Enter to create/modify the file
Remote File Operations
- Select the text you want to copy to a remote server
- Right-click and choose either:
- "Copy selection to remote file" - creates/overwrites remote file
- "Append selection to remote file" - appends to existing remote file (creates if doesn't exist)
- Enter the remote path in format:
user@hostname:/path/to/file
- Choose authentication method (Password or SSH Key)
- Enter credentials (these can be saved for future use)
- The file will be created/updated on the remote server
Important: Path Resolution
All relative paths are resolved relative to the current file's directory (where you're making the selection):
- Relative paths (e.g.,
./utils/helper.js or ../shared/config.js ) → relative to current file's directory
- Absolute paths (e.g.,
/home/user/code/file.js or C:\Code\file.js ) → saved to exact location
Examples
When editing /home/user/projects/src/app.js :
./utils/helper.js → /home/user/projects/src/utils/helper.js
../shared/config → /home/user/projects/shared/config
../../lib/api → /home/user/lib/api
/tmp/snippets/code.js → /tmp/snippets/code.js
/etc/nginx/sites-available/mysite → /etc/nginx/sites-available/mysite
When editing C:\Projects\frontend\components\Button.js :
./styles.css → C:\Projects\frontend\components\styles.css
../utils/theme → C:\Projects\frontend\utils\theme
C:\Temp\extract → C:\Temp\extract
Remote files:
user@example.com:/home/user/project/config.json
deploy@192.168.1.100:/var/www/html/index.html
admin@server.local:/opt/app/settings.conf
root@backup-server:/etc/nginx/nginx.conf
pi@raspberrypi.local:/home/pi/scripts/startup
Features in Detail
Local Operations
- Automatic directory creation: If the directory doesn't exist, it will be created automatically
- File overwrite protection: For copy/move operations, you'll be asked to confirm overwrite
- Append mode: Appends to existing files with a newline separator, or creates new file if it doesn't exist
- Automatic file opening: Newly created/modified files open automatically (except append mode)
- No extension required: Files can be saved with or without extensions
Remote Operations
- SSH Authentication: Supports both password and SSH key authentication
- Credential Storage: Option to save credentials securely for future use
- Auto-retry: If saved credentials fail, they're cleared and you can re-enter them
- SFTP Transfer: Uses secure file transfer protocol for all remote operations
- Remote Append: Reads existing remote file content and appends to it
- No Extension Required: Remote files don't need to have a file extension
Requirements
- VS Code 1.74.0 or higher
- Node.js (for remote SSH operations)
Installation
From VS Code Marketplace
- Open VS Code
- Go to Extensions (Ctrl+Shift+X)
- Search for "Selection to File"
- Click Install
From VSIX file
- Download the .vsix file
- In VS Code, go to Extensions (Ctrl+Shift+X)
- Click the "..." menu and select "Install from VSIX..."
- Select the downloaded .vsix file
Extension Settings
This extension doesn't require any configuration. It works out of the box!
Known Issues
- Remote operations require SSH access to the target server
- Large files may take time to transfer over SSH
- On Windows, use forward slashes (/) or escaped backslashes (\) in paths
Development
- Run
npm test to execute tests.
- Run
npm run vsix to build the extension.
- Run
npm run release to release a new version.
Release Notes
[1.0.4] - 2025-06-02
- Removed ssh2, it was making the extension work
- Removed copy selection to remote file via SSH
- Removed append selection to remote file via SSH
[1.0.3] - 2025-06-01
- Files can be saved without extensions (both local and remote)
[1.0.2] - 2025-06-01
- All relative paths are relative to the current file's directory
- Support for absolute paths in all contexts
[1.0.1] - 2025-06-01
- Fixed command not found error
[1.0.0] - 2025-06-01
- Initial release
- Copy selection to file command
- Move selection to file command
- Append selection to file command
- Copy selection to remote file via SSH
- Append selection to remote file via SSH
- Right-click context menu integration
- Automatic directory creation
- File overwrite confirmation
- Automatic opening of newly created files
- File extension validation (local files only)
- SSH authentication (password and key-based)
- Credential storage for remote connections
- Support for remote files without extensions
- Works without requiring an open workspace
- Intelligent path resolution (workspace-relative or file-relative)
- Support for absolute paths in all contexts
Contributing
Feel free to submit issues and enhancement requests!
License
MIT
| |