Home > Tech > emacs

emacs Archive

Emacs Again (9): some tips and not to be purist.

I got some reactions about Emacs via Twitter/Facebook.


Gary B at work told me about this command.


(global-set-key "\C-x\C-m" 'execute-extended-command)

It sets up another key stroke (C-x C-m) for “execute-extended-command“, which you can do with M-x as default. I was also originally thinking that “execute-extended-command” is such a frequent command and why Emacs doesn’t assign with Control key instead of Meta. As Gary told me “it’ll grow on you.” For me personally at this moment, I have no problems with doing M-x, but we’ll see. At least now I know how to do it. ;)


Steph told me 3 things at one message.

M-x occur

is absolute useful. My initial reaction was, “Hmm, it looks similar to tag jump?” But I was wrong. Because it takes regex, it can do a lot more flexible, and more over it’s on the fly. Neat!

M-x align-regexp

I haven’t used it yet but looks nice. And I found this page: Emacs wiki — Align commands comes with tons of other useful tricks for aligning.

Also % for finding the matching brace.

I found M-C-f (forward) and M-C-b (backward) in order to do that. How do I do with % sign?


Before I used Emacs on my dev box only very once in a while and I couldn’t like for various reasons. One of the biggest glitch was, lots of my config wasn’t simply working. So, I setup .emacs.el and .emacs.d on my linux dev box today. It was not still working, then I realize “Hmm, maybe I can update Emacs.” Gentoo default was 21. 22 was installed, but not defaultly used. And latest is 23. Why not? Boon, here you go. Emacs 23 works perfectly fine now.

As I explained before (Emacs Again (1): config), I setup all Emacs related config files on Dropbox. I know you can setup Dropbox on Linux but since all dev at work can log into my dev box, I don’t wanna do that. So what I did was setup rsync script on both my work mac and my linux box.


#!/bin/sh                                                                                

rsync --progress -a --rsh='ssh -p22' $HOME/Dropbox/Config/emacs/ takashi@my.devbox.local:emacs/ --exclude 'auto-save-list' --exclude '.DS_Store' --exclude 'tramp'

then I run this command everyday. Yay.


One last thing is about permission issue. I believe I talked about this with Mauvis before (and I don’t remember what he does, sorry!).

If you start to use Emacs, then you wanna do every text editing in Emacs. But what if you wanna edit something that requires root permission. You can do sudo emacs /something/important Yeah, you can do it but… It’s not efficient at all.

One other way I found was using tramp. But I don’t like the idea because it implicitly requires that you can login as root via ssh on your localhost. I think it’s definitely a nice hack but I don’t like the solution. And I talked to Gary about it, and he said “then I use vi.Touché, Gary.

Emacs Again (8): Recent Commands

I know some of them appeared before, but I re-find them again and again. That’s how you learn it, right?

Key Stroke Command Name Action
M-; comment-dwim comment or uncomment the selected region
M-g g goto-line Interestingly M-g g and M-g M-g are the same goto-line. Very self explainatory.
C-M-\ indent-region self explainatory. Without argument, Emacs will automatically find suitable indent. If you do throw argument like C-u 10 C-M-\ then it adds 10 spaces (or tab. depends on your mode) in the beginning of the selected region.
C-j eval-print-last-sexp You can only do this in “Lisp-Interaction” mode? Evaluate expression on. You can use it as temporary command. As usual, it display the last return value.
C-x 3 split-window-horizontally Split current window into two windows side by side. Vertical split is C-x 2. If you want to get windows back into 1 window do C-x 0 or C-x 1. 1 brings you to absolutely 1 window and 0 make split windows back to 1 (it makes difference when you have multiple sprits). If you want to switch between windows, do C-x o.
C-_ undo Undo and redo, smartly executed. It says _(underscore) but you basically can do as -(minus sign). Maybe because I use US-ASCII layouted keyboard. Those two characters are assigned on the same key, so I don’t have to hold Shift key in order to type _(underscore).
C-g C-_ cancel undo Explicitly you can redo by doing this.

Change Split Ratio

So I was wondering if I can change the window split ratio, when I do C-x 2 or C-x 3. I asked it on IRC, and somebody immediately answer me. It’s simple, as Emacs way. You pass the argument.


C-u 20 C-x 3

So this will make left side 20 cursor width and split window vertically.

Community channel

Related to the preceding section, you can ask question on IRC. And also there is a twitter account @learnemacs, and he (or she) can answer your question if you tweet something.

090823: learn

diretory の比較 on Unix

Wordpress をアップデート。いつも忘れてしまう diretory の比較。普段は Changes.app などという Mac OS X の GUI アプリを使ってしまって、いっこうに覚えなかった…

How To Compare Directories in Unix

$ diff --recursive --brief /tmp/dir1 /tmp/dir2
Files /tmp/dir1/dir11/file12 and /tmp/dir2/dir11/file12 differ
Files /tmp/dir1/file1 and /tmp/dir2/file1 differ
Only in /tmp/dir1: file2
Only in /tmp/dir2: file3

これを応用すると、例えば dir1 にだけ存在するファイルは

$ diff --recursive --brief /tmp/dir1 /tmp/dir2 | grep '^Only \/tmp\/dir1'

とすればよいですね。

emacs で major mode を変える

例えば .html なファイルを emacs で開くとほぼおそらく HTML mode で開かれると思います。でも実は内部の JavaScript を編集したいし、js2-mode (JavaScript-IDE) で編集したい。凄い基本ですがしばらく emacs を触ってないで忘れてしまっていました。

M-x js2-mode

でした。


またこれから emacs 勉強記録をつけていきたいと思います。今日はどうやってコピーするのか忘れてましたが (M-w Copy / C-w Cut / C-y Paste) それさえわかれば、それなりに使えrて結構びっくりした。

Emacs Again (7): Using Emacs…

Now I really started to use Emacs as a daily life. Well, I only use it at home at this point, but I actually am getting used to it slowly but surely. Here’s the list of commands I most frequently use. By using search and tag jump, Emacs makes me so happy. The code completion runs a bit weird right now. I will try to figure out and will share with you.

Key Stroke Command Name Action
C-s isearch-forward Do incremental Search forward. After typing some, it gets modal. You should see C-h k RET C-s to what you can do.
C-r isearch-backward Do incremental search backward.
M-g g goto-line Go to line.
C-x k kill-buffer It’s equivalent to “close” of usual editors.
M-. find-tag Find tag (in current tags table) whose name contains TAGNAME.
  tags-apropos Display list of all tags in tags table REGEXP matches.
C-x o other-window Select next window on the frame
C-x b switch-to-buffer Select buffer BUFFER in the current window
C-x C-b list-buffers Display a list of names of existing buffers.

Emacs Again (6): Learning GNU Emacs Chap 2

Here’s a list of commands I learned from Learning GNU Emacs, Third Edition Chapter 2.

Chpater 2 — Editing

Key Stroke Command Name Action
C-x Control-X-prefix Try C-h v and type ctl-x-map
M-x execute-extended-command Run a function by name. Try C-h f for each specific function name and what it does.
C-f forward-char Move point one character right
C-b backward-char Move point one characer left
C-p previous-line Move point vertically up
C-n next-line Move point vertically down
M-f forward-word Move point forward by a word
M-b backward-word Move point backward by a word
C-a move-beginning-of-line Move point to beginning of current line as displayed
C-e move-end-of-line Move point to end of current line as displayed
M-a backward-sentence Move point backward to start of sentence
M-e forward-sentence Move point forward to next "sentence-end". (The variable "sentence-end" is a regular expression)
M-} forward-paragraph Move point forward to end of paragraph
M-{ backward-paragraph Move point backward to start of paragraph
C-x ] forward-page Move pointforward to page boundary
C-x [ backward-page Move point backward to page boundary
C-q quoted-insert Read next input character and insert it. This is useful for inserting control characters. For example, if you want to insert \?, you'll type C-q DEL
C-v scroll-up Scroll text of current window upward
M-v scroll-down Scroll text of current window down
M-> end-of-buffer Move point to the end of the buffer; leave mark at previous position.
M-< beginning-of-buffer Move point to the beginning of the buffer; leave mark at previous position.
M-g M-g goto-line Goto line ARG, counting from line 1 at beginning of buffer.
  goto-char Set point to POSITION, a number or marker.
M-(number) digit-argument Repeat following command (number) times. For example: M-3 C-f will move point 3 characters forward.
C-u universal-argument Begin a numeric argument for the following command. For example: C-u 9 C-q DEL will insert nine \? at point, C-u 3 C-f will move point 3 chars forward.
C-l recenter Center point in window and redisplay frame.
C-d delete-char Delete the following character. Defaultly assigned on delete button
M-d kill-word Kill characters forward until encountering the end of a word.
M-DEL backward-kill-word Kill characters backward until encountering the beginning of a word
C-k kill-line Kill the rest of the current line; if no nonblanks there, kill thru newline.
C-y yank Reinsert ("paste") the last stretch of killed text.
C-SPC or C-@ set-mark-command Set the mark where point is, or jump to the mark.
C-x C-x exchange-point-and-mark Put the mark where point is now, and point where the mark is now
M-h mark-paragraph Put point at beginning of this paragraph, mark at end
C-x h mark-whole-buffer Put point at beginning and mark at end of buffer
C-x C-p mark-page Put mark at end of page, point at beginning
M-y yank-pop After C-y, Rotate Kill Ring. Let's say, you have ['latest', '2nd', '3rd'] as Kill Ring. When you do C-y, Emacs inserts latest at point. Then M-y will change latest to 2nd, and type M-y again, 2nd will be 3rd. M-y again, 3rd will be back to latest.
C-t transpose-chars Interchange characters around point, moving forward one character
M-t transpose-words Interchange words around point, leaving point at end of them
C-x C-t transpose-lines Exchange current line and previous line, leaving point after both
C-g keyboard-quit Signal a quit condition.
C-x u advertised-undo Undo some previous changes

Notes

Like other editors in Mac/Windows or even in vi… You know, when you start to select characters, background and foreground colors are inverted. In order to visualize where you are selecting (in Emacs way, it should say… make a region by using set-mark command), you have to add the following line in your .emacs.el. I just wonder why this is not default configuration… Anyway.

(setq-default transient-mark-mode t)

I’ve started my unix career with vi, like 1997? Even the first information technology class in my college taught me to use emacs, I liked vi better because, in that class, I think we have around 100 ~ 200 students one class, and all of them were connecting to “Clay Super Computer” with a few giga-bytes of memory space… It was just insane, to use a “memory sucker Emacs” with that terminal.

So, I still, when I did something wrong on editor, I type ESC! ESC! ESC!.. but instead, I should start to do C-g! C-g! C-g!!

Clipboard-Pasetboard interoperability/synchronization doesn’t work in my environment, Mac OS X 10.5 with Terminal Emacs 22.1.

Changing capital, Overwrite and Customization doesn’t seem to be important to me so I skipped it for now.

Emacs Again (5): Showing line number

wb-line-numberI found a nice elisp to show line number.

wb-line-number

As you see on screenshot, it shows line number on left side of the emacs window. As usual .el files, download it into your elisp directory, and add some lines in your emacs.el file. I usually like to wrap line by window size. And here’s what I did on .el file.

(require 'wb-line-number)
(setq truncate-partial-width-windows nil)  ; wrap line by window size
(wb-line-number-toggle)

Emacs Again (4): Learning GNU Emacs Chap 1

I got Learning GNU Emacs, Third Edition. And here’s the summary I learned from Chapter 1.

Key Stroke Command Name Action
C-x C-f find-file Find file and read it into a new buffer.
C-x C-v find-alternate-file Read an alternate file, replacing the one you’ve done with C-x C-f
C-x i insert-file Insert file at point position.
M-> end-of-buffer Move point to the end of the buffer; leave mark at previous position. By being followed by C-x i you can append file.
C-x C-s save-buffer Save buffer with the current buffer name.
C-x C-w write-buffer Emacs prompts you to set a new file name, and save it.
C-x C-c save-buffers-kill-emacs Exit Emacs with asking you if there are any un-saved buffer. But *scratch* is different. Even you’ve editted *scratch*, Emacs discard it silently.
C-h help-command Enter the online help.
C-h ? help-for-help (or help-for-help-internal) Display a list of available help command?
C-h f describe-function Display help for a given command.
C-h k describe-key Display help for a given key stroke.
C-h v describe-variable Display help for a given variable (a symbol).
C-h t help-with-tutorial Start Emacs tutorial.

Emacs Again (3): dired, js2-mode and ctags

dired

dired stands for DIRectory EDit. (Not sure how it’s pronounced.) It is very useful bundled application. When you do C-x C-f then just hit enter. You’ll see something like ls but with cursor on it. You can do a lot of stuff by this, but mainly, you’ll do open files, just choosing file with cursor, and hit enter. You can also delete by press d for marking. And then press x to execute it. I’m sure there’s a lot more to do, but that’s what I do so far.

colorIf you’re a Mac person, and If you using Display ANSI colors on Terminal.app, and if you use black background theme, such as Pro, you hardly see color of Blue. You can try TerminalColoreopard. Yoshimasa Niwa made a SIBML extension for choosing ANSI color. This ANSI color setting can be only for for one user. Cannot be like theming, but I don’t change theme everyday, so that works fine for me.

js2-mode

In emacs, there are Major mode and Minor mode. Major mode work like a Perspective in Eclipse IDE. It gives certain kind of framework to work on a given issue. There are ecmascript-mode.el, javascript.el and js2.el. I use js2.el for now.

In order to make js2.el work, emacs needs to know which directory autoload search for. It’s like a path setting in your shell. How do you know which directories your emacs search for? Type C-h v load-path. It will return your emacs’ search path. I found a nice article about load-path on emacs wiki. I wanna load everything in ~/.emacs.d is represented as follows.

(progn (cd "~") (normal-top-level-add-to-load-path '(".emacs.d")))

And download js2.el, put it into my ~/.emacs.d/. Open js2.el, then do M-x byte-compile-file. You’ll get js2.elc, which is pre-compiled version. Then add following lines in .emacs.el

;; js2-mode
(autoload 'js2-mode "js2" nil t)
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))

now whenever you open *.js, you’ll use Js2 Major mode.

ctags

Tag is not like delicious tag. It’s more like search index.

EmacsTags

I use exuberant ctags. Just compile and install (./configure ;; make;; sudo make install). And disable original ctags (sudo chmod a-x /usr/bin/ctags).

Outside of emacs, go to top directory of your source file. Try ctags -e --recurse --languages=javascript. Then you’ll get TAGS file in the same directory.

You can do
M-x visit-tags-table and choose TAGS file first. But if you don’t choose TAGS file and try to do tag jump by M-., you’ll be asked which file emacs should read anyway.

Emacs Again (2): auto-save and font

Auto save revisited

So I joined irc.freenode.net#emacs, and asked how to configure auto-save. Several nice people helped me out. Here’s my .emacs.el for backup and auto-save.

(setq make-backup-files t)       ; enable backup file
;;; locate where you backup files
(setq backup-directory-alist
      (cons (cons "\\.*$" (expand-file-name "~/.backup"))
            backup-directory-alist))

(setq version-control t)     ; enable versions of backup
(setq kept-new-versions 5)   ; how many keep new verisons
(setq kept-old-versions 5)   ; how many keep old versions
(setq delete-old-versions t) ; delete old version without asking
(setq vc-make-backup-files t) ; still make a backup for version-controled files                

;;; Autosave in .backup dir
(setq auto-save-file-name-transforms
  '(("\\([^/]*/\\)*\\([^/]*\\)\\'" "~/.backup/\\2" t)))

Originally, I was very confused by auto-save and backup. FIles of backup are named usually like FILE_NAME~ and autosave are like #FILE_NAME#. At this point, I’m not quite sure that auto-save-file-name-transforms will take, but that’s alright. What I wanted to d

Japanese (or other multibyte)

Me as a native Japanese speaker, I often times, use Japanese Characters, plus I learned Chinese these days, I may have chances to read and write in Chinese Characters as well. Here’s what I use now for Japanese language. They are all copied from some of XEmacs distribution package I guess. It’s still just a magic spells…

;; ============= Japanese language setting ======================                              

;; A. inline input method (window-system)
(when (eq window-system 'mac)
  (add-hook 'minibuffer-setup-hook 'mac-change-language-to-us)
  (mac-translate-from-yen-to-backslash) ;; yen => backslash
  (set-language-info "Japanese" 'input-method "MacOSX") ;; bad fix
  ;; input method in read-only buffer (e.g. C-s in dired-mode)
  (setq mac-pass-key-to-system-on-read-only-buffer t)
  )
;; A. end                                                                                      

;; B. language environment
(set-language-environment "Japanese")
(set-default-coding-systems 'utf-8-unix)
(set-keyboard-coding-system 'utf-8)
(set-clipboard-coding-system 'utf-8)
(set-terminal-coding-system 'utf-8)
;; (set-file-name-coding-system 'utf-8m) ; already set
(prefer-coding-system 'utf-8-unix)
;; B. end                                                                                      

;; C. fix: Unicode => Japanese mapping
;; Thanks to saiki-san (see [macemacsjp-users 870])
;; register circle around digits to cjk table (by Ando-san)
(defadvice utf-translate-cjk-load-tables
  (after my-ad-circled-digit activate)
  (dotimes (i 20)
    (let ((unicode (+ #x2460 i))
          (char (+ 54433 i)))
      (if (utf-translate-cjk-substitutable-p unicode)
          (puthash unicode char ucs-unicode-to-mule-cjk))
      (puthash char unicode ucs-mule-cjk-to-unicode))))
;; prevent to use half-width marks (by Nanba-san)
(utf-translate-cjk-set-unicode-range
 '((#x2e80 . #xd7a3)
   (#xff00 . #xffef)
   (#xa7 . #xa7)                        ;
   (#xb0 . #xb1)                        ;
   (#xb4 . #xb4)                        ;
   (#xb6 . #xb6)                        ;
   (#xd7 . #xd7)                        ;
   (#xf7 . #xf7)                        ;
   (#x370 . #x3ff)                      ; ギリシャ
   (#x400 . #x4ff)                      ; キリル
   (#x2000 . #x206f)                    ; 一般句読点
   (#x2103 . #x2103)                    ; ℃
   (#x212b . #x212b)                    ; Å
   (#x2190 . #x21ff)                    ; 矢印
   (#x2200 . #x22ff)                    ; 数学記号
   (#x2300 . #x23ff)                    ; 技術記号
   (#x2460 . #x2473)                    ; 円囲み数字
   (#x2500 . #x257f)                    ; 罫線
   (#x25a0 . #x25ff)                    ; 幾何学模様
   (#x2600 . #x26ff)                    ; その他の記号
   ))
;; C. end

Font for Terminal

And for terminal font, I really appreciate Droid Sans Mono. Droid Family is designed for Google’s Android.

Download the Android SDK

Unzip the archive, and you’ll find .ttf files under tools/lib/fonts/default/. what it’s cool about is, very readable with small font size, mono space and comes with CJK typeface as well. License-wise, maybe ok for your personal use, you can at least develop Android with this. Small note: Android SDK comes with Android.el as well.

Emacs Again (1): config

As my tradition, I moved, and I changed a title of my blog. It was called “Ten Eyck,” but now it’s called “Vantage Point of Queens.” Go visit my flickr page, you’ll see some of my house warming pictures.

So, I desperately try again on Scheme, Emacs and SICP. This time, I have a Kindle to read on, a friend working with me (again). I have experienced more than ever… Well let’s see what’s going on.

As a first time (and I wish this will keep going on), I will start with writing about how to configure emacs. Although there are tons of XEmacs implementation on Mac OS X, I run emacs on Terminal.app. I guess it’s because… (1) one of the fundamental reason to use emacs is because you can use on almost any platform. And if I just use one of those “OS X Specialized” emacs, then I will not remember how to configure. I don’t wanna miss anything. Terminal.app: Use Option as meta key(2) Terminal.app can do “Alt as Meta” natively. (3) Apple keeps evolving Termianl.app. It has great transparent setting, multi-byte char enable, UTF-8 clear and a lot more.

But I digress. In order to configure emacs, you edit .emacs.el file. It usually stay on your home directory. Since it’s already 21st century and the cloud computing is really coming, why don’t I put on the cloud? I setup Dropbox account, and I mkdir-ed ~/Dropbox/config/emacs. I mv-ed .emacs.el and .emacs.d into there and make a symbolic link to them. It works great.

no welcome message

(setq inhibit-startup-message t)

By having this line in your .emacs.el, you don’t see welcome message anymore. That also indicates that your change is applied to your emacs.

aggregate all backup craps

(setq make-backup-files t)
(setq backup-directory-alist
  (cons (cons "\\.*$" (expand-file-name "~/.backup"))
    backup-directory-alist))

You have to mkdir for this before relaunch emacs. Hmm… I don’t see this working. I may try it later again.

Home > Tech > emacs

Search
Feeds
Meta
Links
Ads!

Return to page top