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

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python. Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening.

The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for Bukkit s…

Apple: iPad and Emacs

Someone asked my boss's buddy Art Medlar if he was going to buy an iPad. He said, "I figure as soon as it runs Emacs, that will be the sign to buy." I think he was just trying to be funny, but his statement is actually fairly profound.

It's well known that submitting iPhone and iPad applications for sale on Apple's store is a huge pain--even if they're free and open source. Apple is acting as a gatekeeper for what is and isn't allowed on your device. I heard that Apple would never allow a scripting language to be installed on your iPad because it would allow end users to run code that they hadn't verified. (I don't have a reference for this, but if you do, please post it below.) Emacs is mostly written in Emacs Lisp. Per Apple's policy, I don't think it'll ever be possible to run Emacs on the iPad.

Emacs was written by Richard Stallman, and it practically defines the Free Software movement (in a manner of speaking at least). Stal…

JavaScript: Porting from react-css-modules to babel-plugin-react-css-modules (with Less)

I recently found a bug in react-css-modules that prevented me from upgrading react-mobx which prevented us from upgrading to React 16. Then, I found out that react-css-modules is "no longer actively maintained". Hence, whether I wanted to or not, I was kind of forced into moving from react-css-modules to babel-plugin-react-css-modules. Doing the port is mostly straightforward. Once I switched libraries, the rest of the port was basically:
Get ESLint to pass now that react-css-modules is no longer available.Get babel-plugin-react-css-modules working with Less.Get my Karma tests to at least build.Get the Karma tests to pass.Test things thoroughly.Fight off merge conflicts from the rest of engineering every 10 minutes ;) There were a few things that resulted in difficult code changes. That's what the rest of this blog post is about. I don't think you can fix all of these things ahead of time. Just read through them and keep them in mind as you follow the approach above.…