Monday, January 30, 2006

emacs

I was a vim user. But i always wanted to switch to emacs. I ve heard that vim can do whatever emacs can. Anyway .. after i started exploring lisp, i heard from lisp community that how good emacs was. So i bought "Learning GNU Emacs" in the hope that one day i will learn it. I use command prompt (windows) quite often in my office. One thing that bothered me was running so many command prompts and switching between those. So for all those ppl who suffer these things, im going to write how to use emacs. Start using it. Get the installation from here (windows).
After extracting, run addpm.exe from bin.

note:
C means Ctrl and M means Alt. So C-x means pressing Ctrl + x. C-c C-x means pressing Ctrl + c + x (ie hold Ctrl and type c and x). C-c x means Ctrl + c then release Ctrl, press c

Useful Commands

Opening a file - C-x C-f
A prompt comes below (near status bar) asking for file. you can use TAB for navigating directories or files.
Quitting a Command - C-g
Often you start typing some command and want to quit (ie escaping .. note ESC doesnt work that way). Use this.
For eg. you wanted to open file and did C-x but but you want to cancel it. do a C-g
Quitting emacs - C-x C-c
Saving a file - C-x s
Saving to another file (Save As) - C-x C-w
Saving all opened files (see buffers) - C-x C-s
Windows Commands
Emacs can open many windows inside it. Often it happens that some command or command completion opens a new window and your window splits. This will be annoying in the beginning. Use the commands below to navigate windows.

Going to other window - C-x o
Maximizing current window (current means where cursor is) - C-x 1
Splitting current window - C-x 2
Note that if you open new file after this, file will be opened in that window.
Splitting current window Vertically - C-x 3
See that C-x 2 opens window horizontally. Also you can use this and previous command inside a subwindow also.

Buffers
Buffers represent all the open files and buffers not connected to files ;). If you are using the GUI version, you can see the buffers in the menubar. You can switch to the buffers from there.
Going to another Buffer - C-x b
Press enter. This will go to the previously used buffer
Listing Buffers - C-x C-b (see the difference between previous and this command. In previous you needed to release Ctrl after x was pressed, and in this you need to use Ctrl for both x and b)
This will open another window listing all the buffers. To move to that window use C-x o, or use mouse. You can navigate through lines using 'n' and 'p' (next and previous .. You can use C-n instead of downarrow or C-p instead of uparrow. In Buffer List you need not use C-n, instead you can use n itself .. similarly for p). To select any buffer you can press '1'. This will make that buffer in a maximized window.
There are some other useful things to do in this buffer list. Like if you want to delete some buffers press 'd'. A 'D' will come next to that buffer. To undo anything press 'u'. Finally to execute these press x.

Closing a buffer - C-x k
Press enter. To close many buffers see above

Editing Commands.
This section will represent all the basic things like cut, copy, search etc.

Up Arrow alternative - C-p
Down Arrow alternative - C-n
Left Arrow alternative - C-b
Right Arrow alternative - C-f
(i suggest to use these once you get comfortable with emacs, otherwise all these will garble your brain)
Refreshing screen with current line in middle - C-l

Moving to start of line - C-a
Moving to end of line - C-e
Moving to start of file - M-< Moving to end of file - M->
(use shift key for '<' or '>')
Selecting some text (for copying or deleting).
Use C-space for setting a mark. Use arrow keys (or alternatives) to move to the location till you want the text to be selected. (By default it will not show selected region highlighted. See using .emacs file (below) to how to change the behaviour)

Once you have selected your text use below commands for copying, pasting etc
Copying Text - M-w
Cutting Text - C-w
Note that C and M (ctrl and alt) have many similarities in commands
Pasting Text - C-y (this is called yanking. in vim teminology yank means copy, but here it means paste)
Undo - C-/ or C-x u

Incremental search - C-s
Incremental search backwards - C-r
(note use C-s or C-r multiple times to find previous thing)
Ordinary Search - C-s enter

Running any command - M-x
I heard you can run any command using this. Type M-x and press TAB to see various commands. One example would be killing current buffer by M-x kill-buffer. Renaming buffer by M-x rename-buffer

Running command prompt or shell - M-x shell
enjoy. you can rename the shell to some other thing using M-x rename-buffer, and open as many shells as you can
Previous/Next commands in shell - M-p and M-n

Telnet - windows telnet doesnt work in shell. You need to dowload say jtelnet
(a java app). put the jar in CLASSPATH, and run
java thor.app.tenet.
One good thing is autocompletion of directory/files work probably because in shell.

.emacs file
In *nix .emacs resides in your home. in windows if you do a default installation, i think it takes from C:\. See the windows installation faq. Note that in windows it is named _emacs not .emacs (windows wont allow you to).
You can highlight the selected region by appending this in your emacs file
(transient-mark-mode t)
You can use syntax highlighting always by using
(cond ((fboundp 'global-font-lock-mode) (global-font-lock-mode t)
(setq font-lock-maximum-decoration t)))


Im a little bit tired now (no breakfast and lunch) .. if you want anything else do comment, i will update

5 comments:

Anonymous said...

EMACS: Escape-Meta-Alt-Control-Shift

uno said...
This comment has been removed by a blog administrator.
uno said...

way to go daaa.....
one question.... If I want to save a file in a particular folder, how do I do it??.. as ya know, the file names in windows are so big that ya will go crazy typing it...
Does it have anything like the TAB in Vim

dog said...

It does have autocompletion. Say for eg you do an open file

C-x C-f and press TAB, it lists all the files in new window, and autocompletes also

If you want to do a Save As instead of Save, do
C-x C-w

btw i forgot about undo its
C-/ or
C-x u

dog said...

>>EMACS: Escape-Meta-Alt-Control-Shift

hmm .. there are funny expansions for emacs. see http://www.ntu.edu.sg/home5/PG04878518/EmacsName.html

According to wikipedia http://en.wikipedia.org/wiki/Emacs
emacs is for Editor MACroS