Making a heading for files created in emacs - emacs

I've been a pretty adamant user of nano as my text editor for quite awhile for sake of simplicity. However, now I'm becoming a bit more "seasoned" and wanted to step up to a big-boy editor like emacs.
I know that emacs is highly customizable, and thought of something that I'd like to do.
Is there a way to create a generic heading for files created of a certain filetype?
For example, if I make a .* file, could I generate something similar to Xcode's generated "heading"?
//*************************************************************
//
// filename.*
//
// Created by some_static_user on MM/DD/YY.
// Copyright (c) YYYY some_static_user. All rights reserved.
//
//*************************************************************
It would be lovely and save me some time. Besides, I know there are some emacs apologists who would love to make an emac user out of me.
Thanks for your time and I hope this isn't too trivial.
erip

Try library header2.el.
Description here. You should be able to customize things to get just what you want.

I got it figured out based on #itsjeyd 's answer. For those Mac users reading this, add this to your ~/.emacs.d/init.el
(add-to-list 'load-path' "~/.emacs.d/")
(autoload 'auto-make-header "header2")
(add-hook 'write-file-hooks 'auto-update-file-header)
(add-hook 'emacs-lisp-mode-hook 'auto-make-header)
(add-hook 'c-mode-common-hook 'auto-make-header)
(add-hook 'tex-mode-hook 'auto-make-header)
after saving header2.el in the same directory as init.el. Thanks, everyone!

Related

Customize emacs space indentation

I have to adjust to 4 space indentation for a project I'm collaborating on at work. I hate looking at it, being accustomed to a nice compact 2 space indentation. Is there any way to have emacs display 2 spaces as one? Or maybe tabify on file load, and untabify on save. I'm working in c++. I have searched through google, but can't seem to find an answer to this.
You can try using prettify-symbols-mode, which is built into Emacs >=24.5.
(defun jpk/contract-spaces ()
(add-to-list 'prettify-symbols-alist '(" " . ?\ ))
(prettify-symbols-mode 1))
(add-hook 'c-mode-common-hook 'jpk/c-contract-spaces)
This changes the display, not the content of the buffer or file. I'm not sure this is the best idea. It will affect (auto)indentation and you won't see what the saved file will look like.

Is there any structure to .emacs file

I am trying to get into emacs. I have installed it using apt-get install on ubuntu.
Now I want to change the background color, and found some doc on this. The best doc I found is this:
http://www.emacswiki.org/emacs/FrameParameters
But when reading about how to add things to the .emacs file, none of the docs/info I found mention anything about structure, and the .emacs file just looks like a big mess. Do any of you experienced emacs users follow a specific structure when adding to .emacs, or do you just add anywhere?
I want to add the following:
(add-to-list 'default-frame-alist '(foreground-color . "white"))
(add-to-list 'default-frame-alist '(background-color . "black"))
(add-to-list 'default-frame-alist '(cursor-color . "coral"))
When opening the .emacs file, I don't file like just throwing it in, as I like structure. Before I try to make my own, I'd like to hear if there is a convention that I have missed?
Your .emacs file is a piece of Lisp code. There are conventions for how to format Lisp code, but I guess that's not really what you are looking for.
Part of the problem is that your .emacs will tend to grow organically, as you find new things you want to try and new knobs you want to tweak. Over time, it will build up into a huge, monolithic piece of unrelated code snippets.
The usual structuring conventions for code apply -- group related code together, add comments, modularize.
What you can do with the code you posted as an example is mainly to add a comment.
;; Frame colors
(add-to-list 'default-frame-alist '(foreground-color . "white"))
(add-to-list 'default-frame-alist '(background-color . "black"))
(add-to-list 'default-frame-alist '(cursor-color . "coral"))
Some people like to add folding mode markers so they can collapse/expand code sections, but perhaps at that point you should start thinking about breaking it up into smaller files.
Overhauling the rest of your .emacs file is out of scope here, and impossible without access to the full thing. But see http://www.emacswiki.org/emacs/DotEmacsModular for some suggestions. (Disclosure: A snippet of mine is prominently linked.)
Using customize also helps somewhat, but it has its own pros and cons. The customize settings are added programmatically at the end of your .emacs, and has very little by way of structure.

Emacs highlight specific words when opening

I can use M-x highlight-phrase when I am in the editor. But I want to see the phrase ERROR highlighted in red when I open any file that contains that phrase.
I still cannot figure out how to enable this on my init.el file. Any help?
Thanks in advance
There are existing modes for this sort of thing.
See http://www.emacswiki.org/emacs/FixmeMode
From the looks of it, the best version is likely to be this:
https://github.com/lewang/fic-mode
This looks gruesome to me but if you really want it:
(add-hook 'find-file-hook (lambda () (highlight-regexp "\\<ERROR\\>")))

Trying to edit init.el to customize emacs

So I'm relatively new in trying to customize emacs. But I really need to customize is asap. Tabs are a pain in emacs as they are two spaces, and the text is all messed up when it is opened with any other editor after that.
Currently, I only have few lines in my ~/emacs.d/init.el file:
(setq load-path (concat (getenv "HOME") "/.emacs.d/"))
(set-scroll-bar-mode 'right)
(require 'linum)
(global-linum-mode t)
I get an error while starting up emacs:
Loading encoded-kb...done
An error has occurred while loading `/Users/mycomp/.emacs.d/init.el':
Symbol's function definition is void: set-scroll-bar-mode
To ensure normal operation, you should investigate and remove the
cause of the error in your initialization file. Start Emacs with
the `--debug-init' option to view a complete error backtrace.
I tried srtating it with the --debug-init option, but my lisp knowledge is not enough to help me figure out what's wrong. Any help on how to get this working or redirecting me to some GOOD tutorials on editing init.el files will be really helpful (yes i did google tutorials on editing the initialization file, but every one of them was terrible).
I'm assuming my code for getting line numbers on the left is also wrong. Could someone please help me with this? Thanks a lot.
I think this line may be the problem:
(setq load-path (concat (getenv "HOME") "/.emacs.d/"))
First of all, I don't think this is required to load ~/emacs.d/init.el. Secondly, if you do want to add a directory to your load-path, you should probably be doing it like this instead:
(add-to-list 'load-path "~/.emacs.d/")
This code adds the directory to the load-path, your code just clobbers it with the single directory.
Use 'M-x apropos' and 'M-x customize-apropos'. For now, those will make your life much easier when you want to customize things.
For instance, to customize things to do with scrolling, 'M-x customize-apropos RET scroll RET' will give you a list of all things that you can customize that have 'scroll' in them. You can look around and find the things that you want by searching the buffer. If you find a particular thing that you want, there's usually a group that it belongs to. You can click on that, and just customize those particular values. Make sure you save the settings.
It might take you a while to figure out what things are called. If you've got an idea, try the apropos search. If that doesn't turn up anything, google can probably sort it out for you.
For now, don't worry about hacking the elisp. This method will write values to your startup file (probably the .emacs?) and you can look and check the syntax later if you're really interested. I customize most of my stuff this way; I only bother actually modifying the file by hand when I'm trying to write my own hooks or functions.

Emacs - how do I automatically enter minor modes when I enter a major mode?

I'm kind of a newb when it comes to emacs. I know about the .emacs file but have very little idea as to do anything more advanced than elementary stuff.
Whenever I enter latex-mode, I'd also like to automatically turn on flyspell-mode, reftex-mode, auto-fill-mode, and also set fill-column to 120. How do I edit my .emacs file to do this?
Thanks!
(add-hook 'latex-mode-hook
(function (lambda ()
(flymake-mode)
(reftex-mode)
(auto-fill-mode)
(setq fill-column 120))))
for example should work.
You can set a so-called hook to a major mode. Have a look at this page of the manual for some examples.