Wednesday, March 19, 2008

Emacs: Mixing it Up

A lot of you know that I'm a hardcore Vim fanatic. However, I'm also burnt out right now, so I'm mixing things up. I'm going to switch to Emacs for a while. Help me out by leaving a comment with a couple of your favorite "power" commands.

I'm especially interested in figuring out how to tell Emacs things like "When coding in C, the tab key indents 4 spaces, but change every list of 8 spaces into a real tab. Also, when I go down a line, indent to exactly where I was on the line above." Intelligent indentation is nice, but for cases where it doesn't do what I want, I'd like it to still be helpful. In Vim, I can just enter ":set shiftwidth=4 tabstop=8 autoindent".

Vi is like have capslock for your control key.


Anonymous said...

I'm not an emacs user, but I'm just curious... why do you want to turn a list of 8 spaces into a real tab?

Shannon -jj Behrens said...

I'm referring to the FreeBSD style guide for C code.

Anonymous said...

(setq c-default-style "bsd")


Shannon -jj Behrens said...

> (setq c-default-style "bsd")

Hahaha, thanks ;)

Shannon -jj Behrens said...

How do I deal with a language that Emacs doesn't know about that I want to indent in the same way as Python: i.e. 4 space indents, no tabs?

Glyph Lefkowitz said...

M-x customize

There are dozens and dozens of options. Don't actually add lisp to your .emacs, i.e. (setq c-default-style), until you've messed around with customize first. Otherwise you will stomp all over your customize-set options.

Brandon L. Golm said...

first of all, find emacs help for no tabs from jwz.

second, my favorite power command is:

C-x C-c

I say that as a long time emacs/vim switch hitter, which you are aware of.

Third, opening new window and stuff is fun:

C-x w
C-x 5 2 (in x11, etc)
C-x 5 0

bimone said...

I switched from emacs to vim about a year ago, and something I was missing was an easy buffer explorer. I just found the minibufferexplorer vim plugin and now I'm happy.

For C/C++ vim is really cool, :mak and then you have the quickfix mode to go fix your compile errors.
:make install works too.

But I don't want to start another religion war :)

Libor said...

A few selections from my .emacs, which has been a 15+ year work in progress. hope the formatting holds:
(autoload 'python-mode "python-mode" "Python editing mode." t)
(setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist))

(setq py-indent-offset tab-width)
(setq py-smart-indentation t)
(setq python-mode-hook
'(lambda ()
"python mode hook override."
(setq tab-width 4)
(setq py-indent-offset 4)

Libor said...

Hah, I tried to post the elisp code for my own highlight/coloring mode and got an error from blogger:

Your HTML cannot be accepted: Tag is not allowed:

Brett said...

Highlight a 'square' of text using C-x space. (This is from where you start, to diagonal where your cursor is). Hit C-x r k to 'kill' that column of text. Likewise C-x r t to insert a column of text. Very handy.

Shannon -jj Behrens said...

Thanks, guys. That's exactly the stuff I was looking for ;)

Shannon -jj Behrens said...

> Highlight a 'square' of text using C-x space. (This is from where you start, to diagonal where your cursor is). Hit C-x r k to 'kill' that column of text. Likewise C-x r t to insert a column of text. Very handy.

I'm using Aquamacs. I get "C-x SPC is undefined".

Hmm, it looks like rectangle mode is documented here:


thisfred said...

One thing I like is "stupid" autocompletion (meaning, it only looks for strings in the current file, and doesn't do any library/module lookups) which works amazingly well, even for plain text mode. (At least your typos will be consistent ;) Basically it works a lot like command line tab-completion I have this in my .emacs:

(defun indent-or-expand (arg)
"Either indent according to mode, or expand the word preceding
(interactive "*P")
(if (and
(or (bobp) (= ?w (char-syntax (char-before))))
(or (eobp) (not (= ?w (char-syntax (char-after))))))
(dabbrev-expand arg)

(defun my-tab-fix ()
(local-set-key [tab] 'indent-or-expand))

;;and this to activate it for all the modes I want it in:

(add-hook 'c-mode-hook 'my-tab-fix)
(add-hook 'sh-mode-hook 'my-tab-fix)
(add-hook 'emacs-lisp-mode-hook 'my-tab-fix)
(add-hook 'python-mode-hook 'my-tab-fix)
(add-hook 'rest-mode-hook 'my-tab-fix)

Shannon -jj Behrens said...

Actually, I was going to ask about "stupid" autocompletion. This is one feature that I've really grown use to in Vim. (In Vim, I hit C-n to autocomplete on the given word.)

Shreevatsa said...

In general, you might want to read Steve Yegge's Emergency Elisp, or read the Emacs Lisp intro to learn it "properly" (which I haven't).

You don't need C-x SPC for the rectangle commands, you can just use C-SPC at the beginning of the rectangle to set mark and move to the other place before running the rectangle commands; they automatically act on the rectangle.

Stupid autocomplete is M-/ although it does look in other buffers as well if it doesn't find it in the current buffer.

Hope this helps,