Notes to install Gentoo Linux

to install Gentoo Linux :gentoo:linux:install:en:

Intel integrated video card

Enable KMS (Kernel Mode Setting) for Intel (Device Drivers => Graphic => Intel) is critical:

sudo genkernel --menuconfig all

Grub2

Let grub2 create the cfg for me automatically,

grub2-mkconfig -o /boot/grub/grub.cfg

Network

Ethernet

#net.lo should exist! if NOT, `emerge --ask --oneshot net-misc/netifrc`
# @see https://wiki.gentoo.org/wiki/Netifrc
ln -s /etc/init.d/net.lo /etc/init.d/net.eth0
echo 'config_eth0="dhcp"' >> /etc/conf.d/net
/etc/init.d/net.eth0 start
rc-update add net.eth0 default

Wireless (wifi)

#net.lo should exist! if NOT, `emerge --ask --oneshot net-misc/netifrc`
# @see https://wiki.gentoo.org/wiki/Netifrc
# I need setup /etc/wpa_supplicant/wpa_supplicant.conf at first
ln -s /etc/init.d/net.lo /etc/init.d/net.ra0
echo 'config_ra0="dhcp"' >> /etc/conf.d/net
/etc/init.d/net.ra0 start
rc-update add net.ra0 default

Content of /etc/wpa_supplicant/wpa_supplicant.conf:

#let wpa_supplicant take care of scanning and AP selection
ap_scan=1

#simple case: WPA-PSK, PPSK as an simple passphrase
network={
  ssid="myssid"
  psk="mypassword"
  # the higher priority the sooner we matched
  priority=5
}

For kernel 3.17.*+, by default wpa_supplicant use nl80211 driver instead of wext.

We need for it use wext instead. To double check wext does work,

sudo wpa_supplicant -dd -B -i ra0 -Dwext -c /etc/wpa_supplicant/wpa_supplicant.conf

But OpenRC script does not have -Dwext,

# as root
cd /lib;grep -rsn --exclude-dir=/home "Starting wpa_supplicant on" *
# netifrc/net/wpa_supplicant.sh:85: ebegin "Starting wpa_supplicant on ${IF
# VAR}"

Modify /lib/netifrc/net/wpa_supplicant.sh to for it use wext,

opts="-Dwext ${opts} -c ${cfgfile}"

Clock Synchronization

htpdate is great because it only use HTTP protocol, don't forget add server in its config file.

Sound

Nothing to tell. There may be some issue, but I forgot.

Find files provided by the installed package

files provided by the installed package :linux:package:manager:en:

dpkg -L emacs # Debian/Ubuntu
pacman -Ql emacs # ArchLinux
rpm -qlp emacs # Redhat/Centos
cygcheck -l emacs # Cygwin
brew ls --verbose emacs # Homebrew
equery f emacs # Gentoo Linux, you need install gentoolkit
gem contents zurb-foundation # Ruby gem

Moving focus and buffer between Emacs window

focus and buffer between Emacs window :en:emacs:window:buffer:move:

Please read the excellent article Moving between Emacs windows for basic setup.

Here is my summary of that article:

  • you can use windmove.el (embedded in Emacs). Its hotkey is Shift+Arrow
  • switch-window.el is great. The default hot key is C-x o

I also use other packages which is more efficient.

window-numbering.el

window-numbering.el is great. It uses hot key M-number to switch window (for example, M-3 to select window with number 3). So I can press only one key to switch window. Besides, it has some extra features like M-0 to jump to the minibuffer.

Here is my configuration for window-numbering.el:

(require 'window-numbering)
;; highlight the window number in pink color
(custom-set-faces '(window-numbering-face ((t (:foreground "DeepPink" :underline "DeepPink" :weight bold)))))
(window-numbering-mode 1)

Evil

Evil is a vim-emulation. It use vim's keybindings C-w h/j/k/l to move. Please note C-w h is more efficient than C-x o. It's because w key is more close than x key and h/j/k/l is easy to press.

buffer-move.el

buffer-move.el moves the buffer instead of focus between windows. It can be installed through MELPA.

Here is my configuration:

(global-set-key (kbd "C-c C-b C-k") 'buf-move-up)
(global-set-key (kbd "C-c C-b C-j") 'buf-move-down)
(global-set-key (kbd "C-c C-b C-h") 'buf-move-left)
(global-set-key (kbd "C-c C-b C-l") 'buf-move-right)

Optimization

I use keyfreq to record all my key pressing for more than six months.

According to the data collected by keyfreq, more than 99 percent of my moving focus between windows is done through window-numbering.el.

So it's necessary to optimize window-numbering.el further. By default, window-numbering.el uses M-num key to select window which is already efficient. But as a geek, I have a tendency to push the efficiency to the extreme end.

Finally I use hotkey ,1, ,2, and ,3 to switch to the window numbering 1, 2, and 3.

Here is the code:

(define-key evil-normal-state-map ",1" 'select-window-1)
(define-key evil-normal-state-map ",2" 'select-window-2)
(define-key evil-normal-state-map ",3" 'select-window-3)
(define-key evil-normal-state-map ",4" 'select-window-4)

Check my setup at github.

Comment/uncomment line(s) is easy in Emacs

line(s) is easy in Emacs :en:emacs:

It's easy if you use evil-nerd-commenter because commenting/uncommenting line(s) is only pressing ",ci" in evil-mode.

You can also select text and press M-; to comment the lines in the region. If part of the line is not in the selected, the whole line will still be commented out. This saves your extra key pressing for moving cursor to the beginning of the first line or moving the cursor to the end of the last line.

The reason to make commenting efficient is that I need comment/uncomment code for debug purpose.

Some people say comment out the code is not a right way to debug. The right way is using debugger.

In some huge projects, debugger makes no sense for many reasons:

  • The debugger for that Domain Specific Language simply does not exist
  • Bugs can be reproduced ONLY on production server where debugger is not installed
  • Too much overhead to set up debugger

发表满一周年的感悟

原创日期: 2013-01-31 四

2012年1月31日我写了一年成为Emacs高手(像神一样使用编辑器) (后文简称<一年>),到今天(2013年1月31日)正好一周年.

这一年中,我不断地维护更新该文,力求能够反映最新社区咨询,解决读者的实际问题,这个过程本身使我受益匪浅.

最大的收获就是静下心来花时间把一件事情做到极致,对个人收获是最大的.

例如,我不断维护<一年>一文,使得这篇文章成为我所有文章中最受欢迎的文章,其评论数和读者私下联系我的次数超过了我所有其他文章的总和.很多读者还热心给我推荐了Emacs其他好用的功能(如插件switch-window),学习这些功能靠我自己摸索可能要花大量的时间.

该文之所以能够受欢迎,我想主要是因为文章质量比较高,信息丰富.我的总结是,只有花大量时间,反复修改,才能写出高质量的文章.例如,<一年成为Emacs高手>我前前后后改了十多个版本,前后花了一年时间,最新版本是两天前更新的.

持之以恒的最大优点是可以达到很深的深度,获得不可替代性,而报酬(读者的赞扬回馈,我个人知识智力的增长)是和我钻研的深度成正比的.

例如,在反复修改<一年>一文中,我自己对于该文的中心思想认识越来越深刻了.该中心思想就是,一个人能够达到的高度,是由他的设的起点有多高决定的,而最好的起点就是世界级的顶尖高手.这个道理除了写博客和Emacs外,还可以应用到很多地方.

持之以恒的道理我从小都懂,但是在维护<一年>的过程我才算真正领悟了这个道理(纸面上知道不算知道,做了才算真正知道).就我个人来讲,主要是因为对自己的智力比较自负,总觉得追求有趣新奇的东西才配得上自己的天赋,对于已知的比较枯燥的小修小补的工作有点不屑一顾.事实上,修补完善的过程是很轻松的,不需要花太多时间精力,而获得的回报是巨大的.我要做得就是适当地抑制一下自己追新求变的热情.

接下来是我给读者的问题.

如果你一年前读过了<一年成为Emacs高手>,你现在是否成为了Emacs高手?你的一周年感悟是什么?

无论你已经是高手了,或正在成为高手的路上,把你的感悟告诉我.我会汇总后发表在我的博客上.

这是我的twittergoogle plus以及微博,也可以通过我的email<chenbin DOT sh AT GMAIL DOT COM>联系我.我也在新浪weibo.com上开通账号emacsguru.

Open url in Emacs with external browser

Here is some handy elisp function which use browse-url-generic to open the url in external browser (firefox, for example).

The key point is how to detect the url. If we are in w3m-mode, we will use the link under cursor or the URI of current web page. Or else, we will let browse-url-generic to detect the url.

(defun w3mext-open-link-or-image-or-url ()
  "Opens the current link or image or current page's uri or any url-like text under cursor in firefox."
  (interactive)
  (let (url)
    (if (string= major-mode "w3m-mode")
        (setq url (or (w3m-anchor) (w3m-image) w3m-current-url)))
    (browse-url-generic (if url url (car (browse-url-interactive-arg "URL: "))))
    ))
(global-set-key (kbd "C-c b") 'w3mext-open-link-or-image-or-url)

To specify the external browser like firefox, add below code into ~/.emacs:

;; C-h v browse-url-generic-program RET to see the documentation

;; is-a-mac and linux is the boolean constants defined by me

(setq browse-url-generic-program (cond (is-a-mac "open") (linux (executable-find "firefox")) ))

Simplest workflow on email git commit in Emacs (No magit needed)

Sometimes I just want to email my trivial patch to the original author and forget it. In this case "github fork" or "git format-patch" is too heavy weight for me.

So here is the simplest workflow I can figure out:

  • Step 1, command "M-x vc-dir" whose hotkey is "C-x v d"
  • Step 2, command "M-x git-print-log" whose hotkey is "C-x v l"
  • Step 3, Move focus to the specific commit and run command "M-x log-view-diff" whose hotkey is "d".
  • Step 4, Yank the commit/diff
  • Step 5, Compose new email (command "M-x compose-mail" whose hotkey is "C-x m")
  • Step 6, Make sure the subject of email start with "[PATCH]". See this patch sumbit guide for reasons.
  • Step 7, Paste the content of commit/diff into email body.
  • Step 8, Send the email by command "M-x message-send-and-exit" whose hotkey is "C-c C-c"

Please note:

  • Step 1 is usually necessary unless you only need the diff of current file.
  • Step 2 and step 3 could be replaced with command "M-x vc-diff" or hotkey "C-x v =" if you want to email the diff of work directory.

Import Gmail contacts into BBDB

  • Go to http://contacts.google.com, click the menu "More>>Export…>>vCard format (blah, blah …)".
  • Click "Export" button. Download the contacts.vcf.
  • Make sure bbdb-vcard.el installed.
  • In Emacs run "M-x bbdb-vcard-import-file" and input the full path of contacts.vcf
  • In Emacs run "M-x bbdb-save"
  • Remove contacts whose names are empty. These are Google Plus people inserted automatically by Gmail:

Run the command in shell:

# the orginal ~/.bbdb will be saved as ~/.bbdb.bak
sed -i.bak '/["" ""/d' ~/.bbdb

Search at both stackoverflow and google code in Emacs

Two programming sites are most valuable to me:

stackoverflow.com
solution design and code prototype
google code search
code sample from real product

So I write some elisp code w3mext-hacker-search to automate the workflow.

Copy the into ~/.emacs and you can use hotkey "C-c ; s" to open search results in external browser (firefox, for example):

; external browser should be firefox
(setq browse-url-generic-program
      (cond
       (is-a-mac "open")
       (linux (executable-find "firefox"))
       ))

;; use external browser to search

(defun w3mext-hacker-search () "search word under cursor in google code search and stackoverflow.com" (interactive) (require 'w3m) (let ((keyword (w3m-url-encode-string (thing-at-point 'symbol)))) (browse-url-generic (concat "http://code.google.com/codesearch?q=" keyword)) (browse-url-generic (concat "http://www.google.com.au/search?hl=en&q=" keyword "+site:stackoverflow.com" ))) )

(add-hook 'prog-mode-hook '( lambda () (local-set-key (kbd "C-c ; s") 'w3mext-hacker-search)) )

Toggle http proxy in emacs-w3m

http proxy in emacs-w3m :emacs:proxy:http:en:

emacs-w3m is a browser which handles all my basic web surfing needs.

For some reason, I need switch my http proxy frequently.

The solution is to set/unset the environment variable "http_proxy" in elisp code. Restarting the w3m session after change the environment variable is not needed at all.

Here is my code:

(defun toggle-env-http-proxy ()
  "set/unset the environment variable http_proxy which w3m uses"
  (interactive)
  (let ((proxy "http://127.0.0.1:8000"))
    (if (string= (getenv "http_proxy") proxy)
        ;; clear the proxy
        (progn
          (setenv "http_proxy" "")
          (message "env http_proxy is empty now"))
      ;; set the proxy
      (setenv "http_proxy" proxy)
      (message "env http_proxy is %s now" proxy))))