Sunday, May 16, 2010

Rediscovering Emacs

I get the idea that Emacs is all about power and efficiency, but somehow I fell out of using it because I would forget all those darn keystrokes and I had to USE the text editor RIGHT AWAY to get stuff done.

But now my curiosity is again piqued. Is it really possible to do all code editing operations while keeping my hands in the standard position - no arrow keys and no mouse? That would be sweet, and worth the learning time.

So here goes - a comprehensive documentary of relearning Emacs:

To open Emacs and have it stay in the terminal, use
emacs -nw

Basic Operations (C = ctrl, M = alt)
C-x C-c = close Emacs
C-x C-s = save file
C-x s ! = save all open files
C-x C-f = open file

Cursor Navigation
C-v = pg down
M-v = pg up
C-l = center on cursor text
C-l C-l = cursor text to top
C-l C-l C-l = cursor text to bottom
C-p = up arrow
C-n = down arrow
C-f = right arrow
C-b = left arrow
M-f = forward a word
M-b = back a word
C-a = home (cursor to beginning of line)
C-e = end (cursor to end of line)
M-a = forward a sentence
M-e = back a sentence
M-< = cursor to beginning of document M-> = cursor to end of document

Undo Redo
C-/ = Undo
C-/ [do stuff] C-/ = Redo (undoing previous undos)

Editing Text
? = backspace
C-d = delete

Essentials for Coding
C-j = intelligent newline (accounts for indentation)
Mark a region (for example M-> M-<>
C-M-\ = auto-indent region
(auto-indent, auto-format)
M-g g = go to line
M-/ = autocomplete word

Search and Replace
C-s = search forward
C-r = search backward
C-M-s = regex search
M-% = incremental search and replace
M-x replace-string = global search and replace

Selecting, Copying, Pasting, and Marking
C-spacebar = set mark on cursor
Shift+(any cursor navigation) = set mark on cursor
C-x C-x = swap mark and cursor
C-u C-SPC = cycle through mark ring (previous 16 marks)
region = the text between the last set mark and the cursor
M-w = copy region
C-w = cut region
C-k = cut from cursor to end of line
C-y = paste
M-y = replace pasted text with previously copied text (scrolls through copy history)

Arguments
Arguments can be passed to any text editing or navigation operation:
C-u 123 = give '123' or whatever number you type as an argument to the operation you are about to do.
M-123 = the same thing. For example,
C-u 100 * = M-100 * = type 100 '*' characters
C-u 8 C-v = M-8 C-v = scroll up 8 lines
M-x z = repeat command

Cancel
C-g = cancel anything (scrap arguments, or cancel pending operation)

Macros
You can record a sequence of operations and then play it back repeatedly:
F3 = start recording macro (stuff you do is recorded)
F4 = stop recording macro
F4 = execute recorded macro

Buffers
When you have multiple files open, they are in "buffers".
C-x C-b = list buffers
C-x b = switch to buffer
C-x k = close buffer
C-x rightArrow = next buffer
C-x leftArrow = previous buffer

Windows
You can have many files open at once, either in their own "window" (screen partition) or in offscreen "buffers" (like tabs).
C-x 2 = split window vertically
C-x 3 = split window horizontally
C-x o (letter o)= move cursor between windows
C-x 0 (number zero) = close window
C-x } = expand window horizontally
C-x { = contract window horizontally
M-x z = repeat command (useful with C-x } and C-x {)

Variables
C-h v var RET = view variable value and documentation
M-x set-variable RET var RET value RET = set variable value

File Browsing and Manipulation
M-x dired = DIRectory EDitor (see manual)
...once in dired...
C-x C-q = make dired writable, so editing names as text renames actual files.
f = go into directory
^ = go up out of current directory
To delete files:
d = flag file for deletion
u = unflag file for deletion
x = delete files flagged for deletion

Diff
M-x ediff

Case

Miscellaneous Linux Keyboard Tips
M-Tab = switch between windows
C-M-rightArrow = switch desktop right
C-M-leftArrow = switch desktop left

Version Control
see http://www.gnu.org/software/emacs/tour/ search for "Version Control"

Groovy in Emacs
Download groovy-mode:
sudo wget http://svn.groovy.codehaus.org/browse/~raw,r=HEAD/groovy/trunk/groovy/ide/emacs/groovy-mode.el -O /usr/share/emacs23/site-lisp/groovy-mode.el
You might need to replace 23 with your version number.

Put this in ~/.emacs:
;;; turn on syntax hilighting
(global-font-lock-mode 1)

;;; use groovy-mode when file ends in .groovy or has #!/bin/groovy at start
(autoload 'groovy-mode "groovy-mode" "Groovy editing mode." t)
(add-to-list 'auto-mode-alist '("\.groovy$" . groovy-mode))
(add-to-list 'interpreter-mode-alist '("groovy" . groovy-mode))


Links:
Groovy mode

2 comments:

TC/Copywriter Underground said...

I've been working my way through all the Ubuntu-compatible text editors, trying to find the One True Editor for all my online writing (which nowadays includes almost all my output).

A tarted-up version of Gedit is the current leader, but I'm playing with Emacs to see if it's worth the complexity. Being as I had to spend an hour searching for a way to add word count, I'm not sold yet...

Thanks for the helpful list.

Mervyn said...

months | grilled pork chops | nyc seafood restaurants

Ajax homes | nissan parts | English to French translation

bar stools | short jokes | universities in dallas

Caribean condos | Ajax homes | kids songs