Emacs中的完美多窗口操作

典型的工作流

  1. 在两个子窗口中对比文件内容
  2. 打开更多的窗口以参考其他信息.
  3. 对某个窗口内容比较有兴趣,跳转到该窗口,将该窗口最大化
  4. 看完最大化的窗口内容后,undo最大化操作,重新回到早先的多个小窗口模式以便继续纵览全局

将子窗口自动编号,然后按M-0…9跳转(最爱)

安装window-numbering.el,然后在.emacs中添加以下代码,

(require 'window-numbering)
(window-numbering-mode 1)

undo/redo之前的窗口操作(最爱)

安装Winner Mode,然后在.emacs中添加以下代码,

(winner-mode 1)

;; copied from http://puntoblogspot.blogspot.com/2011/05/undo-layouts-in-emacs.html (global-set-key (kbd "C-x 4 u") 'winner-undo) (global-set-key (kbd "C-x 4 r") 'winner-redo)

焦点移动到下一窗口(常用)

M-x other-window,快捷键是C-x o.

水平/垂直切分当前窗口(常用)

快捷键C-x 2/3

将当前子窗口最大化(常用)

M-x delete-other-windows,快捷键C-x 1.

删除当前子窗口(偶尔用)

M-x delete-window (快捷键C-x 0)

顺便说一下,M-x kill-buffer-and-window (快捷键C-x 4 0),我现在用得更多 些.

移动焦点到上下左右的窗口(基本不用)

因我较喜欢vi快捷键,所以我安装了Evil Mode.快捷键和Vi一样. C-w h/j/k/l移动至各方向的子窗口.

存储/载入窗口布局到register(基本不用)

C-x r w存储,C-x r j载入.

Install perl module through source package on cygwin by using cpanm

perl module through source package on cygwin by using cpanm :cpanm:cpan:perl:module:install:en:

I failed to install package "Template" by running `cpanm Template'.

It's because the binary package requires gcc4 while on my cygwin only gcc3 available.

So I install it from source package (with some hack, of course),

cpanm --look Template # Download and unpack the distribution and then open the directory with your shellI
cpanm . #install from local directory

Updated: The simpler solution is to install gcc4 at cygwin.

Convert TEX to PDF

TEX to PDF :en:perl:cpan:latex:pdf:

Install TexLive in your OS.

Install latex2pdf which is dependent on TexLive.

cpan App::cpanminus # handy tool to install cpan module
cpanm Template # Template is missing on cygwin
cpanm latex2pdf # finally

Convert now!

pdf2latex cv.tex

How to convert org to PDF

All you need to do is to install TexLive. The org-mode has some handy function to find the TexLive and do the conversion. So all you need to trigger the conversion in Emacs is pressing

C-c C-e p
.

At Cygwin and Linux, installing TexLive is a piece of cake by using default package system.

At Mac, I use the default TexLive source package (install-tl-unx.tar.gz). After installation, you need set up environment varaibles.

If you use Bash (default at my OS X 10.7.3), you can paste below code to you ~/.bashrc:

if [[ `uname -s` == *Darwin* ]]; then
   #tex live on OS X
   export PATH=/usr/local/texlive/2011/bin/universal-darwin:$PATH
   export MANPATH=/usr/local/texlive/2011/texmf/doc/man:$MANPAH
   export INFOPATH=/usr/local/texlive/2011/texmf/doc/info:$INFOPATH
fi

Notes on Emacs chat between John Wiegley and Sacha Chua

on Emacs chat between John Wiegley and Sacha Chua :emacs:linux:en:

See http://sachachua.com/blog/2012/07/transcript-emacs-chat-john-wiegley/ for the video and transcript.

I only record key points,

  • Use gnus heavily
  • ERC in another Emacs instance
  • Look at Magit buffer to get overview
  • Use built in vc mode, `C-x v =' to get diff of current file
  • One single org, other seven are archives
  • Helm (fork of anything.el)
  • Gnus read mail and rss (gwene), Adaptive scoring is good.
  • fetchmail to fetch from Gmail
  • Organize Structure (rename/moving files folders)
  • Twenty items in hot list in org-mode linking to other lists and areas of machine
  • Dropbox to record voice at http://dropbox.com and org-mode hook to get voice message
  • M-m to call `org-capture'
  • 'Quick Keys' on Mac plus some apple script (Viewing page in Chrome, M-n to activate emacs, put a link)
  • Emacs can view PDF
  • Reading Emacs manual from the scratch
  • Emacspeak to read the manual
  • Ledger for the accounting

How to configure yasnippet 0.7.0 and use it with Auto-Complete-Mode

to configure yasnippet 0.7.0 and use it with Auto-Complete-Mode :en:yasnippet:emacs:autocomplete:

I will only talk about how to set up yasnippet 0.7.0 or higher version.

If you follow `normal install` way, setup is simple

See its official documentation.

Install the yasnippet into somewhere and add following code into your .emacs:

(add-to-list 'load-path
              "~/.emacs.d/plugins/yasnippet-x.y.z")
(require 'yasnippet) ;; not yasnippet-bundle
(yas/initialize)
(yas/load-directory "~/.emacs.d/plugins/yasnippet-x.y.z/snippets")

If you use elpa package system, setup is even simpler

After installation, you only need two lines in .emacs.

(require 'yasnippet)
(yas/initialize)

Yasnippet 0.7.0 have already defined two locations for the snippets, "~/.emacs.d/yasnippet-install-path/yasnippet-x.y.z/snippets" and "~/.emacs.d/snippets". Yasnippet will load snippets in BOTH directories at startup.

So you only put your own snippets in "~/.emacs.d/snippets" and done. No need to tweak .emacs at all. To verify my claim, you can `C-h v yas/snippet-dirs' to check value of "yas/snippet-dirs". Please note "yas/root-directory" is the alias of "yas/snippet-dirs".

The real world setup is NOT simple

I will explain the reasons at first and give my complete yasnippet configuration code at the end of the this post.

Two snippets may share the same key, so I need activate `yas/dropdown-prompt'

One issue is I need a user-friendly dropdown window popped up when the key I input has several candidates. For example, when I type "inc" in C code. There are two candidates `#include "…"' and `#include <…>' available. A handy dropdown popup will help me to choose one of them as efficient as possible.

The good news is such fancy popup is a standard component of yasnippet. It's called `yas/dropdown-prompt'. Its default algorithm will activate it at highest priority.

The bad news is for some strange reason yasnippet won't load that dropdown-list by default. So you need manually load that component by one line of elisp code `(require 'dropdown-list)'.

`yas/dropdown-prompt' is not perfect

I cannot scroll down the dropdown window when there are more candidates it can display. That's especially annoying when calling `yas/insert-snippet'. In this case, we need use `yas/completing-prompt' instead. I will show the fix at the end of this article.

Yasnippet conflicts with other plugins

I use Auto Complete Mode (version 20120327 in elpa). There are two issues when using it with yasnippet.

First, it use TAB key to do the auto-complete thing while yasnippet also uses TAB key. So I need re-configure hotkeys of yasnippet.

Second, yasnippet changed its API `yas/get-snippet-tables' since version 0.7.0. This make the auto-complete cannot use yasnippet at all. This issue is reported and fixed by tkf. Actually all you need do is simple:

cd auto-complete-install-dir
rm auto-complete-config.elc
curl -L https://raw.github.com/tkf/auto-complete/337caa2ccc254a79f615bb2417f0d2fb9552b547/auto-complete-config.el > auto-complete-config.el

My final yasnippet setup

(require 'yasnippet)
(yas/initialize)
;; default TAB key is occupied by auto-complete
(global-set-key (kbd "C-c ; u") 'yas/expand)
;; default hotkey `C-c & C-s` is still valid
(global-set-key (kbd "C-c ; s") 'yas/insert-snippet)
;; give yas/dropdown-prompt in yas/prompt-functions a chance
(require 'dropdown-list)
;; use yas/completing-prompt when ONLY when `M-x yas/insert-snippet'
;; thanks to capitaomorte for providing the trick.
(defadvice yas/insert-snippet (around use-completing-prompt activate)
     "Use `yas/completing-prompt' for `yas/prompt-functions' but only here..."
       (let ((yas/prompt-functions '(yas/completing-prompt)))
             ad-do-it))

在我的Mac OS X 10.7.3上为heroku开发rails程序

rails版本为3.1,OS X版本10.7.3,postgresql版本9.1.3j

数据库必须用postgresql

heroku官方主页反对开发版本用sqlite3而生产版本用postgresql.

在OS X 10.7.3上安装postgresql

首先,检查你的OS X版本,从10.7开始OS X自带postgresql

sw_vers -productVersion

如果要安装最新版的postgresql,必须删除老版本的数据库。具体做法参考这篇文章,实际上也就是一行命令:

curl http://nextmarvel.net/blog/downloads/fixBrewLionPostgres.sh | sh

强烈推荐用homebrew安装数据库,

brew install postgresql

具体安装细节请参考postgresql官方wiki.

检测已安装的postgresql

这里是一些数据库基本操作的官方文档. 可以psql -l看一些已有数据库列表.

数据库用postgresql的rails项目

rails new myapp --database=postgresql

如果是从一个已经使用sqlite的项目切换数据库,就必须改config/database.yml,有一些catch,请参考stackoverflow上的讨论.

然后,createdb myapp_development创建postgresql数据库,rake db:migrate建立数据库schema并导入数据在本地数据库.简单测试可证明数据库和web程序可以无缝工作.

最后不要忘记git commit最新代码.

部署web程序到heroku

请参考heroku官方文档,提醒一下不要忘记运行heroku run rake db:migrate,否则数据库是空的.

部署结束后可以运行heroku open打开浏览器访问web程序首页.

如打开网站有问题,可以heroku logs查看日志.

针对Ruby on Rails开发的Emacs插件

我主要用两个插件rinariyari.

rinari支持在rails项目中多个文件快速跳转,另外它提供的grep工具我发觉也很有用.快捷键很容易记.

yari对ruby自带的ri工具提供一个emacs界面,优点是兼容最新的ruby,操作简单,只要使用一个函数yari-anything即可.但是其官方文档推荐的yari-anything的快捷键F1在有些远程terminal下有问题.我改成了C-c ; r.

阅读了rails中使用bootstrap相关文章的小结

Luca Pette的Twitter Bootstrap on Rails比较简单,直接使用了bootstrap的css,没用less,测试了simple_form,show_for,kaminari(分页数据)三个应用。要点在于kaminari有点兼容性问题,用twitter-bootstrap-kaminari-views解决这个问题,其他没难度。

@metaskill的LESS Is More - Using Twitter's Bootstrap In The Rails 3.1 Asset Pipeline讲了如何用bootstrap的less函数和变量,我很有兴趣,根据我的经验能通过less语法调用twitter的函数比较有价值。安装gem less-rails-bootstrap就行了,调用bootstrap的函数的技术细节省略,基本就是less语法。

Adding Twitter's Bootstrap CSS to your Rails app的作者也读了以上两篇文章,要点在于确认了@metaskill的方法,less的一些详细的例子。也提到了可以用sass,但我没有深入读下去。

Twitter Bootstrap Basics at railscasts.com非常好,一个完整的例子。Comments也不错,一些html细节。要点在于用了gemtwitter-bootstrap-rails,看起来很强大。

结论,看来我需要转向less而不是sass,因为bootstrap用less。

关于"一年成为Emacs高手(像神一样使用编辑器)"一文的答疑

我陆续收到Email,一些读者使用了我推荐的Emacs牛人配置后,Emacs启动或者使用还是有问题,

主要原因是,

  • 牛人如Steve Purcell用Emacs的最新版本24.x,而读者使用的是主流版本23.x。
  • 对于Emacs的一些基本命令还是不熟悉
  • 对github不是很熟悉

我建议不要丧失信心,事实上你离成为高手只有一步之遥了,以下是我的建议,

  • 还是要按照我原文所说,至少要读一下Emacs自带教程,启动emacs时可带"-Q"参数快速启动。
  • 学点github和git的操作很有帮助,如我原文所说。
  • 牛人Steve Purcell的配置还是很好改的,我一直在同步,可以轻易改为支持Emacs23 前提是你对于git和Emacs比较熟悉,当然这个前提需要你花点时间
  • 如果直接用Steve Purcell的配置有难度,可以考虑Emacs Starter Kit作为起步 Emacs Starter Kit有两个版本,一个版本支持Emacs24,一个版本支持Emacs22和Emacs23 我个人还是强烈建议最终使用Steve Purcell的配置。
  • M-x list-packages一次,如果你的Emacs在启动时下载软件由于网络问题有点卡的话
  • tsocks和export http_proxy=http://127.0.0.1:8000有时候是用的 在中国,技术人必须懂什么叫代理