# Fix Vim Netrw Not Opening Remote SCP SSH Directories
You try to browse a remote directory using Vim's built-in Netrw file explorer:
:e scp://user@remote-server:/var/www/But instead of showing the directory listing, you get:
netrw is unable to list the files in this directoryOr:
Permission denied (publickey,password).
netrw: scp command failedNetrw uses SCP/SFTP to access remote directories, and any authentication or configuration issue breaks the connection.
Step 1: Test SSH Connectivity
Before debugging Netrw, verify SSH works from the command line:
ssh user@remote-server ls /var/www/If this fails, the issue is with SSH, not Netrw. Fix SSH first:
# Test with verbose output
ssh -v user@remote-server ls /var/www/Common SSH issues:
- Wrong username or hostname
- SSH key not added to ~/.ssh/authorized_keys on the remote server
- SSH agent not running locally
- Non-standard SSH port
Step 2: Non-Standard SSH Port
If the remote server uses a non-standard SSH port (not 22), specify it in the Netrw URL:
:e scp://user@remote-server:2222/var/www/Or configure it in SSH config (~/.ssh/config):
Host remote-server
HostName remote-server.example.com
User user
Port 2222
IdentityFile ~/.ssh/id_remoteThen the Netrw command is simpler:
:e scp://remote-server:/var/www/Netrw uses the SSH configuration automatically.
Step 3: SSH Key Authentication
If the remote server requires key-based authentication, ensure your SSH agent has the key:
ssh-add -l
ssh-add ~/.ssh/id_remoteTest that the key works:
ssh -i ~/.ssh/id_remote user@remote-server echo "connected"For headless servers without an SSH agent, you can configure Netrw to use a specific identity file by setting the g:netrw_scp_cmd variable:
let g:netrw_scp_cmd = 'scp -i ~/.ssh/id_remote'Step 4: Netrw Protocol Choice
Netrw supports multiple protocols for remote file access:
```vim " SCP protocol :e scp://user@host:/path/
" SFTP protocol (often more reliable) :e sftp://user@host:/path/
" SSH with rsync :e rsync://user@host:/path/ ```
If SCP fails, try SFTP. SFTP uses a different subsystem and may work when SCP does not:
:e sftp://user@remote-server:/var/www/Step 5: Netrw Browsing Options
Once connected, Netrw provides a file browser interface. Key commands:
<Enter>-- Open file or enter directory--- Go up one directoryD-- Delete file/directoryR-- Rename file/directoryd-- Create new directorygh-- Toggle hidden filesi-- Cycle listing style (thin, long, wide, tree)s-- Sort by name/time/size
Step 6: Netrw Configuration
Configure Netrw for better remote access experience:
```vim " Use SFTP instead of SCP (more reliable) let g:netrw_scp_cmd = 'sftp'
" Keep remote connection alive longer let g:netrw_sftp_cmd = 'sftp -o ServerAliveInterval=30'
" Enable transparent editing (auto-upload on save) let g:netrw_keepdir = 0
" Use a specific SSH key let g:netrw_scp_cmd = 'scp -i ~/.ssh/deploy_key' ```
Step 7: Debugging Netrw Failures
Enable Netrw debugging:
let g:netrw_debug = 1Then try the remote connection again. Netrw writes debug output to netrwDebug.vim in your current directory. Check it for the exact SCP/SFTP command that failed.
You can also manually run the command Netrw uses:
# Netrw typically runs something like:
scp -q -l user@remote-server:/var/www/Run this manually to see the actual error message.
Step 8: Alternative: Use SSHFS
For frequent remote editing, consider mounting the remote filesystem with SSHFS:
sudo apt install sshfs
mkdir -p ~/remote
sshfs user@remote-server:/var/www ~/remoteThen edit normally:
:e ~/remote/index.htmlThis provides better performance and reliability than Netrw for interactive editing, because all file operations use the local filesystem API.
Unmount when done:
fusermount -u ~/remote