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 B

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

Creating Windows 10 Boot Media for a Lenovo Thinkpad T410 Using Only a Mac and a Linux Machine

TL;DR: Giovanni and I struggled trying to get Windows 10 installed on the Lenovo Thinkpad T410. We struggled a lot trying to create the installation media because we only had a Mac and a Linux machine to work with. Everytime we tried to boot the USB thumb drive, it just showed us a blinking cursor. At the end, we finally realized that Windows 10 wasn't supported on this laptop :-/ I've heard that it took Thomas Edison 100 tries to figure out the right material to use as a lightbulb filament. Well, I'm no Thomas Edison, but I thought it might be noteworthy to document our attempts at getting it to boot off a USB thumb drive: Download the ISO. Attempt 1: Use Etcher. Etcher says it doesn't work for Windows. Attempt 2: Use Boot Camp Assistant. It doesn't have that feature anymore. Attempt 3: Use Disk Utility on a Mac. Erase a USB thumb drive: Format: ExFAT Scheme: GUID Partition Map Mount the ISO. Copy everything from