2

I have recently changed Windows OS for Ubuntu. As developer I really loved WinSCP on Windows and I just can't find any other suitable program on Linux.

However, everything works fine with WinSCP running on Wine except I am not able to simply double click on the file and open it in Sublime Text Editor.

in Preferences -> Editors I have tried to set /usr/bin/subl and /opt/sublime_text/sublime_text as default editors but none of those two works for me.

When I open a file on remote server Sublime is opened but it is empty with no content inside. Or if doing right click and choosing "Open" from menu I get "There is no Windows program configured to open this type of file".

Does anyone know how to set WinSCP to open file with local editor?

PerS
  • 23

1 Answers1

8

I had exactly the same problem (Ubuntu + Wine + WinSCP + Sublime) and Sublime was giving me blank pages once I've tried to edit with it. Googling gave me nothing so I've made my own solution for that (tested on Ubuntu 14.04 Trusty)

Here is my solution and research.

Solution

Solution tested and works with Ubuntu 14.04 Trusty + Sublime 3 + Wine-1.6.2. I think it will also work for Debian (haven't tested).

I've split solution into 2 scripts to have two universal tools instead of one specific.

The solution is add a script to fix the path before it's get's to Sublime:

  1. First script wine2unix-path will transform Wine "windows path" into real file system path and fix it's format (quotes and double backslashes \\)
  2. Second script sublime.exe will use fixed real file path from wine2unix-path and will pass it into Sublime.

I've named first script "sublime.exe" because otherwise Wine refuses to see it in "Editors" settings, when I update editor path.

Steps to do:

  1. Create /usr/local/bin/wine2unix-path
  2. Create /usr/local/bin/sublime.exe
  3. Update editor path in WinScp

Step 1. wine2unix-path

Example usage: wine2unix-path c:\\Program files\\WinScp\\Log.txt returns /home/YOUR_CURRENT_LINUX_USER/.wine/drive_c/Program files/WinScp/Log.txt.

  1. Create file sudo touch /usr/local/bin/wine2unix-path
  2. Make executable sudo chmod +x /usr/local/bin/wine2unix-path

Put following script into it:


#!/bin/bash

# Trim quotes
FILE=$(echo "$@" | sed -e "s#\(^['\"]*\)\|\(['\"]*$\)##g")
USER_NAME=$(id -u -n)
DRIVE_LETTER="`echo $FILE | sed -e "s/^\([a-Z]\):.*/\1/" | awk '{print tolower($0)}'`"

echo "$FILE" | sed \
    -e 's#\\#\/#g' \
    -e "s#^[a-Z]:#/home/${USER_NAME}/.wine/drive\_${DRIVE_LETTER}#"

exit 0

What this script do:

  1. FILE - Get file path and trim " and ' quotes if there is any
  2. USER_NAME - Get current user name /home/USER
  3. DRIVE_LETTER - Get drive letter from file path (first character). It may be different C:\ refers to .wine/drive_c, D:\ refers to .wine/drive_d etc.
  4. Update file path
    1. Replace root path C:\ with full path to wine folder. C:\ becomes /home/USER/.wine/drive_c
    2. Replaces \\ (escaped backslashes in a windows way) with \, otherwise C:\\users\\files become C:usersfiles when it passed to sublime.
  5. Output result file path (real file path)

Step 2. sublime.exe

  1. Create file sudo touch /usr/local/bin/sublime.exe
  2. Make executable sudo chmod +x /usr/local/bin/sublime.exe
  3. Put there provided below
  4. Change /opt/sublime_text/sublime_text path in script below (last line) with yours sublime path.

My Sublime version 3103 is located at /opt/sublime_text/sublime_text


#!/bin/bash

# Wrapper script to open sublime from Wine
# 
# File path passed as a first argument should be "quoted" and "escaped"
# other wise spaces will split path into two
# 
# For example: sublime.exe "c:\\Program files\\WinScp\\Log.txt"
# 

# wine2unix-path is a help script to convert "Wine windows paths"
# to real paths in linux so sublime can access it
# 
# For for example:
#     ./sublime.exe "c:\\Program files\\WinScp\\Log.txt"
# Will result:
#     /home/YOUR_CURRENT_LINUX_USER/.wine/drive_c/Program files/WinScp/Log.txt

unixpath="`wine2unix-path $1`"

/opt/sublime_text/sublime_text "$unixpath"

What this script do:

  1. Converts Wine file path to real file path.
  2. Passes this path quoted (to avoid split path by spaces) to sublime.

Step 3. Update editor path in WinScp

  1. Open WinScp
  2. Go to Options > Preferences > Editors
  3. If you already have Sublime in editors:
    1. Select line with sublime
    2. Click Edit button
    3. Set following path for "External editor" Z:\usr\local\bin\sublime.exe \"!.!\"
    4. Click Ok. Done
  4. If you don't have Sublime in editors:
    1. Click Add (to add sublime editor and assign it to file types).
    2. Set following path for "External editor" Z:\usr\local\bin\sublime.exe \"!.!\"
    3. Set you list of file types you want to edit with it in "Use this editor for following files". I use it for PHP files and some configs *.ini; *.txt; *.php; *.tpl or *.* for all file types
  5. Make sure Sublime line is on top of the list in Editor preferences. It will make priority for file types you selected for Sublime.

Picture: Sublime settings in WinScp

Research of the problem

The problem appear in file path that Wine passes to Sublime:

  • Path is not quoted with " or ' when it passed to sublime
  • Path backslashes are doubleescaped \\

An example:

  1. File to edit /server/user/www/my project/some other file.php
  2. Wine calls sublime Z:\opt\sublime_text\sublime_text /home/CURRENT_USER/.wine/drive_c/Program files/WinScp/server/user/www/my project/some file.php (path not quoted or escaped)
  3. System run it as /opt/sublime_text/sublime_text /home/CURRENT_USER/.wine/drive_c/Program files/WinScp/server/user/www/my project/some file.php.
  4. So command being interpretative as:
    1. Command /opt/sublime_text/sublime_text
    2. Argument /home/CURRENT_USER/.wine/drive_c/Program
    3. Argument files/WinScp/server/user/www/my
    4. Argument project/some
    5. Argument file.php

As the result path being split by spaces because of the lack quotes. As I found later there is also problem with double escapes \\ in a path

Devaldo
  • 96