People where I work have the annoying habit of naming files the same as the class they define, except that the filename is capitalized differently than the class. So the class ZooAnimal is in files zooAnimal.h and zooAnimal.cpp. Why? Why???
It's annoying enough when I'm trying to locate the file for a given class, but a more frequent annoyance is that when I hit M-/ to run dabbrev-expand, the out-of-the-box behavior is to change the case of what I typed to match the expansion that dabbrev found. Using the example above, if I type "ZooAn M-/", dabbrev notices #include "zooAnimal.h", and gives me the completion "zooAnimal". Grrr...
The dabbrev package is so fundamental that you don't need to specially enable it in your .emacs file. In fact, I didn't even realize that's what I was using, so in my first efforts to change the completion case-folding, I was searching for variables named "*complet*", and hit a dead end. Only on the next day did I think to C-h k M-/ to find out what function was case-folding the expansions, and that led me immediately to the variable dabbrev-case-fold-search. The default setting for that is the symbol 'case-fold-search', which means to use the same value as case-fold-search. Now, I do find case-folding useful on searches, either for laziness' sake, or because you don't always remember the exact capitalization of what you're looking for. So I have case-fold-search set to 't', meaning the default behavior of dabbrev-expand was very annoying.
Solution: either enter C-h v dabbrev-case-fold-search and click the customize link (and choose "off" for "case is significant" -- both of which are confusing descriptions in my opinion), or in your .emacs put "(setq dabbrev-case-fold-search nil)".
I purposely left dabbrev out of the post title, hoping people who are confused like me will be more likely to Google it.
Showing posts with label emacs. Show all posts
Showing posts with label emacs. Show all posts
Saturday, June 14, 2014
Wednesday, April 10, 2013
Emacs tip: Use 'view-lossage' to answer the question "How did that happen?"
Does it ever happen that you are thumping along in emacs, and suddenly you are in a mode or buffer that you never knew about before? You must have unintentionally hit some keystroke that caused the change, but what was it?
You can find the answer by typing
For instance, this morning I was suddenly staring at a blank buffer called "ChangeLog". What is that thing? To find out, I typed
You start reading the lossage at the end. So, at the end I can see the
So what do those keys do? It opened a new buffer, but did it have some other harmful side-effect I want to know about? Hit
There tend to be two situations in which I call
Those are typos I've made before that have annoying effects, so I just completely disable those keystrokes (
You can find the answer by typing
C-h l (that's a little "L"). This runs view-lossage, which shows the last 100 characters typed.For instance, this morning I was suddenly staring at a blank buffer called "ChangeLog". What is that thing? To find out, I typed
C-h l. A *Help* buffer opened with a few lines of symbols. Here are the last couple lines: C-s F I X M E C-a C-n C-n C-n C-n C-n C-n C-n C-n C-x 2 C-x o C-x V a r C-x u C-h l
You start reading the lossage at the end. So, at the end I can see the
C-h l from invoking view-lossage. Right before that, an undo (C-x u) to get rid of the typing I had just done into the unknown ChangeLog buffer. Right before that, C-x V a r. Ah, I meant to search for "Var", but I must have hit C-x instead of C-s. This is the culprit.So what do those keys do? It opened a new buffer, but did it have some other harmful side-effect I want to know about? Hit
C-h k to start the describe-key function, and start typing C-x, v, a. Before I could type the 'r', the *Help* buffer showed up and explained that I had entered the keystroke for vc-update-change-log, a function that seems like it was more useful back in the days of RCS than today, but which doesn't harm anything else. Anyway, mystery solved!There tend to be two situations in which I call
view-lossage. Number one is where I think, "Hmm, I don't know what I did, but it might be useful." For that case, view-lossage lets you explore that functionality. That was the case this morning with ChangeLog; I was curious about it but it turned out not to be useful to me. Number two is where I think "I don't ever want that to happen again". In that case, figure out the offending keystroke, and then disable it in your .emacs file, like this:(global-unset-key "\C-[\C-[") ;; prefix-command (global-unset-key "\C-x\C-p") ;; mark-page (global-unset-key "\C-x\C-n") ;; set-goal-column
Those are typos I've made before that have annoying effects, so I just completely disable those keystrokes (
mark-page is especially nasty: it sets the mark at the end of the file, and moves the point to the beginning of the file, so there's not always an easy way to jump back to where you were in the file).Tuesday, September 11, 2012
Disabling Auto-Indent in Emacs
I have to edit a C++ file at work whose indentation is so screwed up that any punctuation key I type ends up changing the indentation of the line I'm working on to something that is way out of line with the text right around it. For sanity's sake I have to
M-x set-variable c-syntactic-indentation nil when I'm working on that file.
The c-syntactic-indentation variable does not start out buffer-local, meaning that for the sake of the one stupid file, I have turned off the very useful feature of syntactic indentation for every file I have open in that session. So, if you are ever going to set it temporarily, make sure you have this line in your .emacs file:(make-variable-buffer-local 'c-syntactic-indentation)
While you're at it, don't you want a shortcut for the set-variable? I amused myself greatly this morning as I tried to think what shortcut I would assign this action to. The obvious mnemonics I polled with
C-h k were taken: I have C-c TAB set to indent-region, C-c ; to comment-region, and C-c { set to a function to insert a skeleton class definition. What to do? I finally hit upon C-c (, and lo-and-behold I had already set that keystroke to toggle c-syntactic-indentation, sometime in the distant past! Here's the code: (global-set-key "\C-c("
'(lambda ()
(interactive)
(setq c-syntactic-indentation (not c-syntactic-indentation))
(message "c-syntactic-indentation set to %s"
(if c-syntactic-indentation "t" "nil"))))
Friday, June 1, 2012
Emacs: Scroll Other Window Up
It often happens that I have two related files open in one emacs frame, and I want to scroll around comparing them. Of course to scroll back and forth in a single buffer,
To keep two files synchronized as you peruse them, the power tool to use is
Ediff is great, but sometimes it is too heavy-handed for the eyeballing I'm trying to do. For one thing, if one of the files contains a large section that is missing in the other, scrolling both files at once means striding through the section in one window while inching down a line at a time in the file that lacks it. Also, navigating ediff can be tedious if there are a great number of changes or reordered chunks, and once your diffs no longer line up sensibly, ediff's highlighting is simply a nuisance.
So there are the old standbys
Turns out there is a keystroke to scroll higher in b.txt without switching windows:
Yes, the command to go higher in the file -- to scroll up as we humans say -- is "scroll-other-window-down".
C-v scrolls you down, and M-v scrolls you up. (In a perverse bit of emacs jargon, C-v's function is called "scroll-up", since the text moves "up" relative to the fixed window -- even though every human wanting to look lower in the file thinks and says "scroll down, scroll down" -- and M-v's function is called "scroll-down".)To keep two files synchronized as you peruse them, the power tool to use is
M-x ediff-buffers, which sets up an interactive diff session to step through the diffs. Within ediff, a simple lowercase "v" scrolls lower in both files at once, and uppercase "V" scrolls higher. Don't forget M-x ediff-revision as a handy tool for comparing a version-controlled file to its latest revision, or for comparing two different revisions interactively.Ediff is great, but sometimes it is too heavy-handed for the eyeballing I'm trying to do. For one thing, if one of the files contains a large section that is missing in the other, scrolling both files at once means striding through the section in one window while inching down a line at a time in the file that lacks it. Also, navigating ediff can be tedious if there are a great number of changes or reordered chunks, and once your diffs no longer line up sensibly, ediff's highlighting is simply a nuisance.
So there are the old standbys
C-v and M-v, plus there is the handy M-C-v, which scrolls lower in the other window. That is, if I have a.txt and b.txt open in a single frame, and the cursor is in a.txt, then M-C-v will show a lower chunk of b.txt, to keep up with C-v in a.txt. Until today, whenever I wanted to move higher in b.txt, I didn't know of a single keystroke to do that, so I always just switched windows and used M-v. I could do C-u - M-C-v, but I never liked that.Turns out there is a keystroke to scroll higher in b.txt without switching windows:
M-C-S-v -- same keys as scrolling lower, plus the shift key. Now, M-C-v is a pretty ergonomic companion to C-v. I lean the base of my left hand on the Ctrl key, hit V with my left index finger, and keep my thumb poised over the Alt key to choose which window to scroll. The shift key just does not fit in with this scheme. With some effort, I can hold it with my left pinkie, but it's not comfortable. So my new solution is to tie that command to C-c M-C-v. True, it's not a single keystroke, but unlike the C-u solution above, it's all in the left hand, and close together without being cramped. The .emacs line is:(global-set-key "\C-c\M-\C-v" `scroll-other-window-down)
Yes, the command to go higher in the file -- to scroll up as we humans say -- is "scroll-other-window-down".
Wednesday, November 17, 2010
Emacs: Copy Environment Variable from Shell
Sometimes I get annoyed when my emacs session has different environment variable settings than some shell buffer I have running in the session. The most painful is when a Makefile depends on environment settings that I don't have in my .profile. Command-name completion in a shell buffer can also be painful if you have changed your path. And of course I'd like gdb to start up with the correct environment every time -- you can set the variables inside gdb, but that gets old on those days when gdb itself crashes again and again.
I kept pasting
Run this function inside your shell buffer, and it will search backwards for the last environment variable action and bring that variable setting into the emacs session. It's also nice because it gives you a message in the minibuffer showing the change. Add a
I kept pasting
export FOO=bar into the *scratch* buffer and editing it into (setenv "FOO" "bar") and eval'ing that. After the 1000th time of doing that, I decided to automate it. Turns out emacs already has an interactive function for copying an environment variable from the shell, but you have to type in the variable name. I decided to write a little function to look for the last export, or the last echo $FOO, and copy that variable:(defun engisneering-shell-copy-env-var ()
(interactive)
(let* ((expat "\\(export +\\([^=\n]+\\)=\\(.+\\)\\)")
(echpat "\\(echo +\\$\\(.+\\)\n\\(.+\\)\\)")
(cshpat "\\(setenv +\\([^ \n]+\\) +\\(.+\\)\\)")
(patt (concat shell-prompt-pattern
"\\(" expat "\\|" echpat "\\|" cshpat "\\)")))
(save-excursion
(if (re-search-backward patt)
(let* ((m (or (and (match-beginning 2) 3)
(and (match-beginning 5) 6) 9))
(var (buffer-substring (match-beginning m) (match-end m)))
(oldval (or (getenv var) " ")))
(shell-copy-environment-variable var)
(setq val (getenv var))
(message "Old %s=%s; New %s=%s" var oldval var val))))))
Run this function inside your shell buffer, and it will search backwards for the last environment variable action and bring that variable setting into the emacs session. It's also nice because it gives you a message in the minibuffer showing the change. Add a
local-set-key -- I like C-c C-v -- in your shell-mode-hook, and you're good to go.
Friday, December 11, 2009
Emacs vs. Nicer dirs
In the last post I described a nicer version of the shell
One problem with it is that emacs shell-mode generally uses
Of course the natural solution is:
to pick up the shell's builtin
So you're stuck with needing a workaround:
dirs command that labels each directory with its position, so that you can pushd directly to the one you want.One problem with it is that emacs shell-mode generally uses
dirs to track which directory you're in. And you do want it to track directories for you. Our nicer version of dirs produces output that isn't readable to emacs.Of course the natural solution is:
(setq shell-dirstack-query "command dirs")
to pick up the shell's builtin
dirs. Sadly, you can't just throw that line into your .emacs file, because shell-mode sets shell-dirstack-query every time you start a new shell. It sets it unconditionally and globally, which is a ridiculous design flaw, but that's how it goes. There is no hook that runs after it sets the value, either.So you're stuck with needing a workaround:
- Name your command something other than
dirs. - Use a flag on your new
dirsto get the nicer behavior. - Write your own emacs function to start a shell:
(defun engisneering-shell ()
(interactive)
(shell)
(setq shell-dirstack-query "command dirs"))
Monday, November 2, 2009
Keep from Killing Emacs Shell Buffers
If you're someone who lives in emacs like I do, then you probably rely on shell buffers (
But occasionally I've been left kicking the wall after accidentally killing a shell buffer that had lots of work in it that I still needed. It really hurts when you accidentally kill a buffer where you're waiting for a long-running process to finish. To prevent industrial accidents like that, add a guard to
If you really do want to kill the shell buffer, type "yes", and you're done. But it will save you some grief if you just hit
What's more common for me is that my eyes are on a file that I want to refresh or replace with
M-x shell) to interact with the operating system. That way you can search through output and edit history commands in a natural way that a mere terminal doesn't allow.But occasionally I've been left kicking the wall after accidentally killing a shell buffer that had lots of work in it that I still needed. It really hurts when you accidentally kill a buffer where you're waiting for a long-running process to finish. To prevent industrial accidents like that, add a guard to
kill-buffer-hook in your .emacs file:
(add-hook 'kill-buffer-hook
'(lambda()
(and
(string= "Shell" mode-name)
(or
(yes-or-no-p
(format "Kill buffer `%s'? " (buffer-name)))
(error "Aborted")))))
If you really do want to kill the shell buffer, type "yes", and you're done. But it will save you some grief if you just hit
C-x k in the wrong buffer.What's more common for me is that my eyes are on a file that I want to refresh or replace with
C-x C-v (find-alternate-file), but the cursor is in a shell buffer. In that case, emacs will indeed load the alternate file, and you'll be asked "Kill buffer ` **lose**'?" Say no, then you'll have C-x b to " **lose**" (note the leading space), and rename the shell buffer to "*shell*" or whatever you had previously named it.
Monday, May 18, 2009
Emacs Registers
A few weeks ago when I was talking about tricks to get back to your editing location in emacs, I neglected to mention the jump-to-register command, by default bound to
The nice thing is, when you jump-to-register, you don't have to be in the same buffer or file as the saved location. So you can mark places that are interesting, go explore some other stuff, and then immediately leap back to the buffer and line that you need.
Do you use emacs registers much? There are a few things you can do with them, the most common being point-to-register/jump-to-register and copy-to-register/insert-register. Whereas the kill-ring is like a text clipboard vector, copy-to- and insert-register are like a clipboard associative array.
I find it useful to keep register identifiers (the single character that names a register) partitioned into namespaces:
C-x r j. Actually, the first step is to save a location to a register: C-x r SPC (point-to-register). You'll be prompted to enter a character to refer to the location.The nice thing is, when you jump-to-register, you don't have to be in the same buffer or file as the saved location. So you can mark places that are interesting, go explore some other stuff, and then immediately leap back to the buffer and line that you need.
Do you use emacs registers much? There are a few things you can do with them, the most common being point-to-register/jump-to-register and copy-to-register/insert-register. Whereas the kill-ring is like a text clipboard vector, copy-to- and insert-register are like a clipboard associative array.
I find it useful to keep register identifiers (the single character that names a register) partitioned into namespaces:
- punctuation marks: canned useful strings defined with set-register that my .emacs loads at startup
- numbers: locations for jump-to-register
- letters: temporary copy-to-register strings for ad hoc cut/paste operations
Wednesday, February 25, 2009
Getting Back to Where You Were in Emacs
When you do an interactive search in emacs (e.g.:
That's so second-nature to me that I am always a little surprised when
Double Undo: Usually the place you want to get back to is the place you've just been typing in the current file. So an undo --
Pop Global Mark: The command pop-global-mark --
Oh yeah, don't forget point-to-register!
C-s or C-r), the mark is set at the place you began your search. So when you're ready to go back to the editing you were doing when you started searching, a simple C-x C-x (exchange-point-and-mark) gets you back to where you were.That's so second-nature to me that I am always a little surprised when
C-x C-x doesn't take me where I expect, as can happen when you light one search off the end of another, or find yourself in another file. So here are a couple of tricks to try when C-x C-x fails you:Double Undo: Usually the place you want to get back to is the place you've just been typing in the current file. So an undo --
C-x u -- will snap the window back to that spot as it undoes your recent typing. But you didn't want to undo that, so you have to "undo the undo". Successive C-x u commands just undo more and more, so first break the cycle by issuing some innocuous command -- I like to use C-n (next-line) -- followed by a second C-x u. In case that's confusing, the sequence is: C-x u C-n C-x u. Of course, that only works if you didn't change files while you were off searching.Pop Global Mark: The command pop-global-mark --
C-x C-SPC -- is a power tool to center the window at a recent mark, no matter what file it was in. This is especially handy if your searching and grepping takes you across several files, or if you can't remember which file you were working in. Just keep popping marks until the file and location you want shows up. This command sometimes fails you, if the place you're trying to return to never had a mark set because you never started a search from there. If you know you're about to embark on a scavenger hunt and will want to return, set the mark explicitly -- C-SPC -- before you begin.Oh yeah, don't forget point-to-register!
Wednesday, January 21, 2009
Emacs Repeat Command
One of the handiest commands in emacs is
That way, if you just want to redo that thing you did, hit return. If you want to slightly modify it -- change "k" to "m", for example -- just edit the command before pressing return. Not very interesting in the "k" to "m" case, but if you run
Sometimes the thing that comes up in the minibuffer isn't something worth repeating:
In that case, just use the minibuffer history (M-p, M-n) to get to the command you're looking for.
Gotcha: I scratched my head for a few minutes today wondering why nothing happened when I re-ran a
repeat-complex-command (shortcut: M-x ESC ESC). It opens in the minibuffer the lisp formulation of the last command you ran, for example:
Redo: (query-replace "j" "k" nil nil nil)
That way, if you just want to redo that thing you did, hit return. If you want to slightly modify it -- change "k" to "m", for example -- just edit the command before pressing return. Not very interesting in the "k" to "m" case, but if you run
query-replace-regexp and then realize you got one tiny thing wrong in a huge regular expression, well, you can see that would be useful.Sometimes the thing that comes up in the minibuffer isn't something worth repeating:
Redo: (find-file-other-window "~/.emacs" 1)
In that case, just use the minibuffer history (M-p, M-n) to get to the command you're looking for.
Gotcha: I scratched my head for a few minutes today wondering why nothing happened when I re-ran a
replace-string. If your replace command is restricted to a region, the bounds of that region are present in the lisp command. So when you try to redo it, it redoes it for your old region -- not the current one. Best case for that is a no-op; worst case is changing a region you didn't want to. Be careful when redoing any command that might apply only to the region.
Monday, November 10, 2008
Timestamped Shell History
Occasionally I save myself a lot of head-scratching by being able to refer to a timestamped history of my work in a particular shell session. You know the feeling, when you say either "I thought I already did that" or "Didn't I do X before Y?" Of course, usually you can piece together a timeline by looking at file modification times or revision control logs. Sometimes you absolutely cannot figure out the history -- a deleted file has no modification time -- and other times it would just be handy to have a transcript telling you when you did what.
Here are the ingredients to keeping a timestamped history:
The hitch is that after a few weeks in your emacs session, a shell buffer can grow very large. Here is a Lisp function to put in your .emacs which knocks it back down to size (make sure the emacs shell-prompt-pattern variable matches your shell prompt):
That will also be useful when you're ready to save your timestamped history. The timestamping is easy -- put this shell script into a file called "timestamp", and give it execute permission:
There are two things to note about the script. First, when it outputs a timestamp, it will match the prompt pattern, so that it won't get deleted by our Lisp cleaning function. Second, it ends with "prompt> " so that the next time you hit return, emacs doesn't think the output from the timestamp is part of your next command.
So, whenever you start a shell buffer, run "timestamp&", to get the date and time printed out once an hour while you do other things.
Finally, here is a little script to help you save your shell histories. Create a directory to hold your histories; name the history files after the host the shell was on, and date them (including the year). Save them periodically, just like any of your work.
Here are the ingredients to keeping a timestamped history:
- Emacs shell buffer: M-x shell.
- Timestamp script running in the background.
- Lisp code to remove shell output, leaving prompts and history.
su, ssh, and even things like ftp. If you need multiple shell buffers (say, on different hosts), use M-x rename-buffer.The hitch is that after a few weeks in your emacs session, a shell buffer can grow very large. Here is a Lisp function to put in your .emacs which knocks it back down to size (make sure the emacs shell-prompt-pattern variable matches your shell prompt):
(defun engisneering-clean-shell-buffer ()
(interactive)
(let ((beg (point-min)))
(goto-char (point-min))
(if (> 10000 (buffer-size)) (buffer-disable-undo))
(while (re-search-forward shell-prompt-pattern nil t)
(if (not (= (point) (line-end-position)))
(progn
(forward-line 0) ;; beginning of line, ignore prompt boundary
(if (not (= (point) beg))
(delete-region beg (point)))
(forward-line 1)
(setq beg (point)))))
(buffer-enable-undo)
)
)
That will also be useful when you're ready to save your timestamped history. The timestamping is easy -- put this shell script into a file called "timestamp", and give it execute permission:
#!/bin/bash
while ((1)); do
echo -n "timestamp> "
date
echo -n "prompt> "
sleep 3600
done
There are two things to note about the script. First, when it outputs a timestamp, it will match the prompt pattern, so that it won't get deleted by our Lisp cleaning function. Second, it ends with "prompt> " so that the next time you hit return, emacs doesn't think the output from the timestamp is part of your next command.
So, whenever you start a shell buffer, run "timestamp&", to get the date and time printed out once an hour while you do other things.
Finally, here is a little script to help you save your shell histories. Create a directory to hold your histories; name the history files after the host the shell was on, and date them (including the year). Save them periodically, just like any of your work.
(defun engisneering-save-shell-buffer ()
(interactive)
(let ((name (buffer-name)))
(save-buffer 0)
(setq buffer-file-name nil)
(rename-buffer name)
(buffer-disable-undo)
(delete-region (point-min) (point-max))
(buffer-enable-undo)
)
)
Thursday, October 30, 2008
All About Emacs Macros
Emacs macros allow you to record a repetitive sequence of keystrokes, so they can be replayed. It's a pretty basic and useful emacs skill, but there are a couple of commands related to macros that can make them even more useful to you.
To record a macro, type "
Macros can contain any sequence of keystrokes, including control and meta characters (hence searches and other commands). After you type "
Saving Macros
One good thing often leads to another, and sometimes you find yourself wanting to use two macros at the same time. No problem, the command "M-x name-last-kbd-macro" lets you give a name to a macro that you've entered. Then, instead of typing "
If you have a macro that you think will be useful to you again and again, you can save it in your .emacs file. Name the macro, then open your .emacs file, and run "M-x insert-kbd-macro". Lisp code which defines and names your macro is written into the file, so the command will be available to you forever.
Editing macros
Sometimes you go to a lot of effort to record a macro but you have one typo in it, or can make it more useful by tweaking it a little. In that case, there is a way to edit either a named or unnamed keyboard macro. Just use "M-x edit-named-kbd-macro" or "M-x edit-last-kbd-macro".
To record a macro, type "
C-x (" (if you're new to emacs, C-x means "control-x"; M-x means "meta-x", usually entered by holding down the "Alt" key while pressing "x"). Then everything you type will be recorded until you type "C-x )". The string "Def" appears in the emacs status line while the macro is being recorded. Macro recording is aborted by any error operation -- basically anything that makes the bell ring.Macros can contain any sequence of keystrokes, including control and meta characters (hence searches and other commands). After you type "
C-x )", your macro can be replayed by typing "C-x e". If your macro is such that it leaves the cursor in a place where it makes sense to run the macro again, you can run many iterations of the macro by using a prefix argument. For example, to run the macro 100 times, type "C-u 1 0 0 C-x e".Saving Macros
One good thing often leads to another, and sometimes you find yourself wanting to use two macros at the same time. No problem, the command "M-x name-last-kbd-macro" lets you give a name to a macro that you've entered. Then, instead of typing "
C-x e" you type "M-x" and the name you gave your macro.If you have a macro that you think will be useful to you again and again, you can save it in your .emacs file. Name the macro, then open your .emacs file, and run "M-x insert-kbd-macro". Lisp code which defines and names your macro is written into the file, so the command will be available to you forever.
Editing macros
Sometimes you go to a lot of effort to record a macro but you have one typo in it, or can make it more useful by tweaking it a little. In that case, there is a way to edit either a named or unnamed keyboard macro. Just use "M-x edit-named-kbd-macro" or "M-x edit-last-kbd-macro".
Thursday, September 4, 2008
Emacs File Mode Tricks
Usually emacs figures out what mode to be in -- such as which programming or script language -- based on the extension of the filename you're editing. It can also figure it out from a "pound-bang" line, like
But sometimes you have a file with no extension and no pound-bang. You can still make emacs automatically choose the right file mode, by specifying it in a comment on the first line of the file, like:
or
The
Another use for file-mode comments is if your C++ header files have the extension ".h", which emacs decides is a C file. Just make the first line be a comment containing
Sometimes you do have a file extension which is meaningful to you, but emacs just doesn't recognize it. For example, if you're crazy enough to use the Inline Guard Macro idiom, emacs doesn't automatically recognize ".ipp" as a C++ extension.
Rather than put mode comments in every ".ipp" file, put this line in your .emacs file:
#!/bin/bash. You want the right file mode: it colorizes the text correctly, makes indentation easier, and lets you easily comment regions of text.But sometimes you have a file with no extension and no pound-bang. You can still make emacs automatically choose the right file mode, by specifying it in a comment on the first line of the file, like:
# -*-tcl-*-
or
##############-*-makefile-*-##############
The
-*- markers tell emacs to look for the mode name between them. You can have any other text you need in the line.Another use for file-mode comments is if your C++ header files have the extension ".h", which emacs decides is a C file. Just make the first line be a comment containing
-*-c++-*-.Sometimes you do have a file extension which is meaningful to you, but emacs just doesn't recognize it. For example, if you're crazy enough to use the Inline Guard Macro idiom, emacs doesn't automatically recognize ".ipp" as a C++ extension.
Rather than put mode comments in every ".ipp" file, put this line in your .emacs file:
(if (null (assoc "\\.ipp\\'" auto-mode-alist))
(setq auto-mode-alist
(cons '("\\.ipp\\'" . c++-mode) auto-mode-alist)))
Subscribe to:
Posts (Atom)