红联Linux门户
Linux帮助

Windows平台Sublime Text编辑远程Linux平台上的文件

发布时间:2014-07-26 09:46:55来源:linux网站作者:zyz511919766

Sublime Text是个跨平台的强大的代码编辑工具,不多说。想使用Sublime Text完成linux平台下django站点的代码编辑工作以提高效率(原来使用linux下的vim效率较低,适合编辑一些小脚本)。

下载linux平台下的Sublime_Text_2.0.2_x64.tar.bz2(http://www.sublimetext.com/

解压使用:
tar -xjvf Sublime_Text_2.0.2_x64.tar.bz2
cd Sublime\ Text\ 2/

执行
./sublime_text

报错
./sublime_text: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.11' not found (required by ./sublime_text)
./sublime_text: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.9' not found (required by ./sublime_text)
./sublime_text: /lib64/libc.so.6: version `GLIBC_2.11' not found (required by ./sublime_text)

原因是所使用的系统为CentOS5,版本过低,c/c++库版本过低。
公司的电脑不能随便升级库或者升级系统,只能另想办法。


设想一:
linux服务器上的代码拉下来存放在windows机器上,使用windows平台上的sublime对其进行编辑,之后重新上传到linux服务器中。

问题:代码编辑后通常需要实际运行调试,尤其是站点类型的代码,常常需要编辑好后立刻验证一下显示结果。而我们的整个运行环境是搭建在linux平台下的,如此一来,便需要先将代码从linux服务器拉倒windows平台,改动一个地方后重新传回linux服务器,测试运行,若有问题则重新拉下来编辑,重新上传、测试,如此下去,非常繁琐耗时。有没有一种更好的办法?


设想二:
在linux服务器上使用git为代码建立仓库,在windows上clone该仓库,使用sublime对clone下来的仓库中的代码进行编辑,编辑完成后一条push命令将所有提交的更新内容推到远程的代码仓库中进行测试。过程与设想一类似,改进之处在于使用git命令来向远程linux服务器推更新,而不是手工逐个将更新的文件上传到远程服务器。

不知道有没有问题,先试试看:
之前已经在linux服务器上创建了代码仓库,位置为
/var/www/site/mycitsm
直接使用 /var/www/site/mycitsm目录下的.git目录创建供其他机器clone的代码仓库
sudo git remote add origin ssh://username@IP:PORT/var/www/site/mycitsm/.git

之后远程机器就可以使用
git clone ssh://username@IP:PORT/var/www/site/mycitsm/.git d:/www/mycitsm
该命令将远程服务器上的代码仓库拉倒本地d:/www/mycits目录中

我们在windows平台上下载安装Git-1.9.2(http://git-scm.com/download/win)或者第三方的gitHub等等
clone仓库:
进入git bash(安装Git后创建的命令行工具)
$git clone ssh://username@IP:PORT/var/www/site/mycitsm/.git d:/www/mycitsm
可能提示让输入远程服务器上op1的密码,(如果将本地服务器的公钥写进了远程服务器的authorized_keys文件则无需密码即可。具体可参考如何设置无密码SSH至远程主机,Windows系统与linux系统之间与linux系统与linux系统之间的设置方法是相同的。)
也可能提示找不到git-upload-pack,这是因为远程服务器中git安装的位置不是标准的默认位置,只需要将这些工具复制到标准的位置,或在标准位置设置软连接连接至这些工具即可,也可以在clone命令中指定工具的位置如: --upload-pack /home/op1/bin/git-upload-pack。

此后远程仓库中的目录结构和内容被clone到了本地windows服务器的d:/www/mycitsm目录中了,可以使用sublime直接打开该目录对内部的文件进行编辑。

在git bash中进入本地仓库
$cd d://www/mycitsm

查看git状态
$git status

提交变化
$git add filename
$git commit -m "some text"

将本地仓库推到远程仓库
$git push (origin master)

报错:
remote: error: refusing to update checked out branch: refs/heads/master
remote: error: By default, updating the current branch in a non-bare repository
remote: error: is denied, because it will make the index and work tree inconsistent
remote: error: with what you pushed, and will require 'git reset --hard' to match
remote: error: the work tree to HEAD.
remote: error:
remote: error: You can set 'receive.denyCurrentBranch' configuration variable to
remote: error: 'ignore' or 'warn' in the remote repository to allow pushing into
remote: error: its current branch; however, this is not recommended unless you
remote: error: arranged to update its work tree to match what you pushed in some
remote: error: other way.
remote: error:
remote: error: To squelch this message and still keep the default behaviour, set
remote: error: 'receive.denyCurrentBranch' configuration variable to 'refuse'.
To ssh://op1@192.168.83.36:1022/var/www/site/mycitsm/.git
! [remote rejected] master -> master (branch is currently checked out)
error: failed to push some refs to 'ssh://op1@192.168.83.36:1022/var/www/site/my
citsm/.git'

意思为:默认情况下不允许在非裸仓库中更新当前分支,因为推送的内容会导致索引和工作目录树不一致。
尽管可以通过通过将git配置文件中'receive.denyCurrentBranch'变量值设置为ignore或warn来允许推送至当前分支,但非常不推荐这么做,除非你安排更新工作目录树来匹配你推送的内容。

既然如此,那我们设置一个罗仓库会怎么样呢?
来到linux服务器的/var/www/site/目录
创建裸仓库(具体参考http://git-scm.com/book/zh):
$ git clone --bare mycitsm mycitsm.git
该命令实际上相当于
$ cp -Rf mycitsm/.git mycitsm.git

之后把该罗仓库移动至你期望的位置即可。
$sudo git remote add origin ssh://username@IP:PORT/var/www/site/mycitsm.git
$git clone ssh://username@IP:PORT/var/www/site/mycitsm.git d:/www/mycitsm

用sublime编辑,本地提交,推送至远程仓库。没有看到异常。
那,是否意味着我们可以在远程的仓库中测试了呢?

连到linux服务器进入/var/www/site/mycitsm/目录发现里边的文件并没有发生任何变化。哦对,我们是将更新推送到了linux服务器上的裸仓库mycitsm.git中。
进入mycitsm.git,恩并没有工作目录树(这也是罗仓库之所以叫裸仓库的原因),它只记录了提交历史信息。

费了好大劲,也就是说我们并不能通过这种方式来将本地最新内容同步到linux原始仓库中,只能将变化信息同步到相关的裸仓库中,而我们又不能在裸仓库中获取到可以进行测试的目录文件。

且上述两种方案都有一个问题,那就是linux服务器中的文件变化不能主动直接的反馈到windows端,我们必须重新拉回最新的文件才能看到这种变化。


设想三:
了解到sublime有个叫sftp的插件,可以通过它直接打开远程机器上的文件进行编辑,并在保存后直接同步到远程机器上,听起来很诱人。

先为sublime安装包管理插件Package Control:
按Ctrl+`调出sublime控制台, 粘贴以下代码到控制台并回车
import urllib2,os;
pf='Package Control.sublime-package';
ipp=sublime.installed_packages_path();
os.makedirs(ipp) if not os.path.exists(ipp) else None;
open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read())

重启sublime,在Perferences->package settings中看到package control这一项,则安装成功。

用Package Control安装插件
按下Ctrl+Shift+P调出命令面板
输入install 调出 Install Package 选项并回车,然后输入ftp,下拉列表中会出现一些相关的插件,选中sftp进行安装就行了,装好后还需配置如下:选菜单栏中的File->SFTP/FTP->Set up Server,然后出现一个配置窗口如下:

{
// The tab key will cycle through the settings when first created
// Visit http://wbond.net/sublime_packages/sftp/settings for help

// sftp, ftp or ftps
"type": "ftp",

"sync_down_on_open": true,
"sync_same_age": true,

"host": "IP",
"user": "username",
"password": "passowrd",
//"port": "22",

"remote_path": "/var/www/site/mycitsm/",
//"file_permissions": "664",
//"dir_permissions": "775",

//"extra_list_connections": 0,

"connect_timeout": 30,
//"keepalive": 120,
//"ftp_passive_mode": true,
//"ftp_obey_passive_host": false,
//"ssh_key_file": "~/.ssh/id_rsa",
//"sftp_flags": ["-F", "/path/to/ssh_config"],

//"preserve_modification_times": false,
//"remote_time_offset_in_hours": 0,
//"remote_encoding": "utf-8",
//"remote_locale": "C",
//"allow_config_upload": false,
}

一般配置一下一下参数即可
"host": "yourIP",
"user": "yourUsername",
"password": "yourPassowrd",
"remote_path": "yourPath",

这种方法要求远程的linux服务器可以通过sftp或ftp连接上去,也就远程linux服务器上需要运行有类似ftp server的东西,ftp server产品很多,选其一安装配置好即可。安装配置参考具体的产品。

linux的ftp server端和sublime端都配置好后便可通过file->stfp/ftp/browser server来查看远程服务器上的目录和文件了,然后可根据提供的命令重命名目录,编辑文件等。编辑好的文件保存后可立即同步至远程的linux服务器中。
该方案貌似很好的解决了我们的问题,但让不够完美,我们在sublime中浏览到linux服务器中工作目录的体验很不好,必须使用一条条命令来返回值至上一目录,对文件进行编辑,重命名等操作。在不同目录和文件中导航很不方便。

有没有一种方案可以吧远程linux服务器中指定的目录同步到windows本地,直接用sublime打开windows本地的目录,显示出一颗完美的目录树,迅速的在各文件夹和文件间切换编辑,并实时的将更新内容同步到远程服务器呢?


最终方案:
功夫不负有心人,查找到确实存在这么一个东西。可以通过SSH到远程服务器上,把远程服务器的某个目录挂载到本地,远程或本地的变化能实时的反应到另一端(实际上,两者是同一位置,你编辑的就是linux服务器上的文件)。这样我们只需把linux服务器上所需的目录挂载到windows本地,使用sublime打开该目录跟打开本地其他目录完全一样,直接对目录内的文件进行编辑。

这个工具叫sshfs使用ssh访问远程主机,由windows版本和linux版本的分别用于在windows上和linux上挂载远程目录。

下载安装windows版本的工具
要先装依赖:dokan library -->DokanInstall_0.6.0.exe(http://dokan-dev.net/wp-content/uploads/DokanInstall_0.6.0.exe)
在安装sshfs本身:win-sshfs-0.0.1.5-setup.exe (https://win-sshfs.googlecode.com/files/win-sshfs-0.0.1.5-setup.exe)

启动、配置指定host、port、username、password、directory等内容后将目录挂载到本地,之后便可以使用sublime随心所欲的编辑了。

目标终于达成了!