Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically replace certain text string as they are typed in emacs

Tags:

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.

like image 710
Staffan Avatar asked Jul 08 '10 19:07

Staffan


2 Answers

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.

like image 129
danlei Avatar answered Oct 16 '22 22:10

danlei


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.

like image 25
Jérôme Radix Avatar answered Oct 16 '22 22:10

Jérôme Radix