Skip to main content

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.

Comments

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?
jjinux said…
I'm referring to the FreeBSD style guide for C code.
Anonymous said…
(setq c-default-style "bsd")

http://www.emacswiki.org/cgi-bin/wiki/IndentingC


Andrea
jjinux said…
> (setq c-default-style "bsd")

Hahaha, thanks ;)
jjinux 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 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
vim

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
bsergean 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 :)
Anonymous 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)
)
)
Anonymous 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:
Anonymous 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.
jjinux said…
Thanks, guys. That's exactly the stuff I was looking for ;)
jjinux 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: http://blog.interlinked.org/tutorials/emacs.html

Thanks!
Unknown 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
point."
(interactive "*P")
(if (and
(or (bobp) (= ?w (char-syntax (char-before))))
(or (eobp) (not (= ?w (char-syntax (char-after))))))
(dabbrev-expand arg)
(indent-according-to-mode)))

(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)
jjinux 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.)
Anonymous 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,

Popular posts from this blog

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within Tunes.org , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and jabber.org. Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p