Staffan
Staffan

Reputation: 1754

Automatically replace certain text string as they are typed in emacs

There's a small nuance that's been bugging me for a while, namely that I frequently type #inclued instead of #include. If it wasn't obvious, I program a lot of C and C++. That typo has wrecked countless builds and consumed time that would have been better spent drinking coffee or surfing stackoverflow. Surely emacs can be helpful and rectify my mistakes as I type (in cc-mode only, of course). But how?

Googling and searching stackoverflow didn't provide any answers.

Upvotes: 7

Views: 1372

Answers (5)

Jérôme Radix
Jérôme Radix

Reputation: 10533

A nicier and more global solution than abbrevs (because you can't predict all the typos you'll make) is to use flymake (which comes with emacs distribution).

http://flymake.sourceforge.net/

Flymake checks your source code behind the scene while you're still typing your code into the buffer. It highlights what's wrong with your code (that is : what gcc tells is wrong).

Running gcc in the background does not use more CPU than your antivirus bloatware. Moreover, if you have 2 or more cores, gcc can take advantage of parallelization. It only checks the syntax, not compiling anything.

Upvotes: 3

Mica
Mica

Reputation: 19637

if you're interested in something a bit different, you could write all your little piece into snippets, using the YAsnippet package, then you could type something like #in, hit TAB, and it will expand into... whatever you want.

Upvotes: 1

phils
phils

Reputation: 73345

I was going to suggest that this could be a slightly odd application for flyspell, but danlei's answer looks better.

Upvotes: 0

Notthinking
Notthinking

Reputation: 11

Sounds like flymake is exactly what your after. It runs a compiler in the background, and will hightlight errors, as you type.

Upvotes: 0

danlei
danlei

Reputation: 14291

You could use this in abbrev-mode: After you entered #inclued, do C-x a i g include RET, and from then on, every time you type #inclued, it will be changed to #include automatically. If you want that abbrev to be local to a mode, use C-x a i l instead of C-x a i g. Also, you can edit your abbrevs with M-x edit-abbrevs.

Upvotes: 9

Related Questions