Archive for the ‘emacs’ Category

the title of this post is misleading … i only talk about commenting and uncommenting region.

this is a small tip for emacs users who want to edit matlab m-files and octave m-files without installing some major mode for editing matlab files. emacs already ships with a built in gnu octave mode and the true hacker will use free software only. but i m no where near being a true hacker thus i often have to write some matlab stuff. i associate all .m files with the octave mode and i have the following line in my .emacs file to achieve that

(setq auto-mode-alist (cons '("\\.m$" . octave-mode) auto-mode-alist))

the major annoyance is that matlab uses the ‘%‘ as the comment char while gnu octave uses ‘#‘. so when editing m files in emacs i often include the following local file variables

% Local Variables:
% comment-start: "%"
% comment-column: 0
% End:

at the end of the m-file. these lines are treated as comments by the matlab interpreter due to the ‘%’ prefix but emacs scans the file and reads these lines and now you can highlight text and use M-x comment-region or M-x uncomment-region. after adding the lines either kill the buffer and visit the file again or hit C-x C-v and then choose the same filename again.

there are probably many good features of the matlab editor but i have not used them much apart from the ability to run the current m file by hitting F5 or setting and clearing breakpoints with mouse clicks. i do like the F5 thingy but i m sure a little bit of elisp can solve that problem. i m sure there are some good major modes for editing matlab files or integrating with matlab but i haven’t explored this area at all.

my emacs version is “GNU Emacs 23.4.1 (i686-pc-linux-gnu, GTK+ Version 2.24.10) of 2012-04-12 on shirley.hoetzel.info”

Read Full Post »

i have always defined matrices of two dimensions as a vector of a vector container as follows

vector< vector <double> > v; // note the distance between the two right angle brakets just before v

however i recently learned that c++0x allows one to get rid of the extra space between the right angle brackets as in the following

vector< vector <double> > v_old_style; 
vector< vector <double>> v_new_style; 

however emacs 23 breaks indentation when faced with this format

i also use 3 and 4 dimensional tensor like objects for which i would much like the following:

vector < vector < vector < vector <double> > > > v1; // don't like it
vector<vector<vector<vector<double>>>> v2; // like this a lot 

for me at least the saving in horizontal space matters when i am comparing code side by side … also its quite easy to figure out the dimensionality of the tensor at one glance.

i don’t know how to fix this 😦 help me please

Read Full Post »

emacs color theme

been working with the default color scheme that comes with emacs on archlinux. most of the times i found that after long sessions and particularly at night my eyes would hurt looking at a white background. though i have an lcd screen but the white was too much.

so i started looking for a dark color theme. first thing i tried was to install the emacs-color-theme package on archlinux. there r about 50 color themes in there.  most of them put me off so i started exploring various color themes. one of the themes that i liked is tango. i m not saying it is the best as i have not explored a whole lot of choices but suffice it to say that looking at the screen with the tango theme hurt my eyes less than the default theme.

Attempt 0 —> failure 😦

0) put the color-theme-tango.el file some where so that emacs can find it. i simply put it in the .emacs.d directory of my $HOME.

1) add the following lines in ur .emacs

;; add the .emacs.d directory to emacs search path, since i put the .el file in there
(add-to-list 'load-path "/home/m/.emacs.d")
(require 'color-theme)

2) save the .emacs

unfortunately on restarting emacs i encounted the following problem

Symbol's function definition is void: color-theme-tango

i do not know enough ELISP to make sense of that. 😦

Attempt 1 —> Success  😦    (note the sad face!)

0) put the color-theme-tango.el file ANYWHERE u feel like . i simply put it in $HOME/.emacs.d/myThemes directory
1) add the following lines in ur .emacs

(require 'color-theme)
(load "/home/m/.emacs.d/myThemes/color-theme-tango.el")

2) save the .emacs, close and restart emacs

that worked finally and my eyes could breathe easy. the final result is as follows

though i got the theme working there are somethings i do not understand
0) the color theme page at emacswiki does not give any instruction of the sort “(load “path/to/file.el”)” so i must be missing something.
1) even when i added the file containing directories to the load-path in emacs, the files could not be found. again i must be doing something wrong.
2) what is the meaning of the error/warning message that i got?

Read Full Post »

emacs maximized

back when i was on ubuntu hardy heron i used the following in my .emacs to maximize the emacs window on startup

;;i tried the following with ``emacs -nw" and it does not work
 (defun toggle-fullscreen ()
   (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
 	    		 '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
   (x-send-client-message nil 0 nil "_NET_WM_STATE" 32

now that i am on archlinux with emacs 23.2.1 i follow a different strategy

i have defined the following function in my .bashrc

function e()
    # i was advised to not use $*
    emacs --maximized "$@" &

the reason i defined a function and not an alias is so that i can use the & at the end.

this works great.

i just wanted to make this post for reference as i m modifying my .emacs

Read Full Post »

yasnippet is not available in the archlinux repos so we will install it from the AUR

first go to AUR home and search for yasnippet … u will get the following page

download the tarball from that page .. in my case the link to the tarball was

put the tarball in a convenient location and extract it to get an emacs-yasnippet directory. within that folder you will find 2 files: PKGBUILD and yasnippet.install

now from within the emacs-yasnippet directory run the following command

makepkg -s

the -s flag will ensure that all dependencies are installed first.

when i ran the command i got the following error

$ makepkg -s
==> ERROR: Fakeroot must be installed if using the ‘fakeroot’ option
in the BUILDENV array in /etc/makepkg.conf.

which means that the fakeroot program is missing …
some more digging told me that i might be missing the base-devel group of packages

so i first have to install it

sudo pacman -Ss base-devel

this will install all the packages needed for building software.

now let me try running makepkg again …

makepkg -s

…ok it was successful this time and now the folder contains the following files and folders

emacs-yasnippet-0.6.1c-2-any.pkg.tar.xz  pkg  PKGBUILD  src  yasnippet-0.6.1c.tar.bz2  yasnippet.install

all is fine now.. we will simply install now using pacman

sudo pacman -U emacs-yasnippet-0.6.1c-2-any.pkg.tar.xz

that is it you are done …

of course you must add the following in your .emacs to use yasnippet

(add-to-list 'load-path "/usr/share/emacs/site-lisp/yas")
(require 'yasnippet)
(yas/load-directory "/usr/share/emacs/site-lisp/yas/snippets")
  1. yasnippet
  2. AUR

Read Full Post »

emacs c++ tags

source code navigation using tags is a lifesaver in large projects … i have been using the system for sometime but was vexed with a problem for a long time.  i m on ubuntu 8.04 which has the exuberant ctags package available.  my makefiles have a phony target called `tags’ that generate the TAGS file to use in emacs. this is how i generate the TAGS file by writing make tags at the command line


find -L $(srcDirs) -name '*.cpp' -o -name '*.h' |  etags -L -

when ever i searched for a tag in emacs using M-. i was always taken to the function definition in the .cpp files (source files) but never to the .h files (header files)

the reason for this is two fold

  • i practice separation of interface and implementation i.e. the function declaration and function definition are in separate files viz. .h files and .cpp files.
  • i did not know the use of –<LANG>-kinds=[+|-]kinds option available with ctags

–<LANG>-kinds=[+|-]kinds is then the key to the solution of this vexing problem. since the language i use is c++ then LANG becomes c++, that is –c++-kinds=[+|-]kinds while kinds for c++ could be any of the following

c  classes
d  macro definitions
e  enumerators (values inside an enumeration)
f  function definitions
g  enumeration names
l  local variables [off]
m  class, struct, and union members
n  namespaces
p  function prototypes [off]
s  structure names
t  typedefs
u  union names
v  variable definitions
x  external and forward variable declarations [off]

notice that p (function prototypes) is off by default … which means that etags was skipping over the function declaration in header files… so i could not go to them when searching for a tag. so the solution to my problem is that i should generate tags using the following:


find -L $(srcDirs) -name '*.cpp' -o -name '*.h' |  etags --c++kinds=+p   -L -

+p simply turns on (+) the tagging of funtion prototypes (p) .

that’s it and happy days r here!

Read Full Post »

in a typical c++ file i have many functions and many a times i needed to search a keyword/variable or carry out a search and replace operation … however it becomes very confusing if multiple functions exist that use the same variable names.

often times i keep hitting C-s and i never can know if i m still within the function i started in or ventured in to a different function altogether … this is more confusing since i use long functions spanning many screenfuls of code 😦

a good thing that i discovered is which-function-mode that i talked about here … which can be helpful but one has to always keep a watch on the modeline …

but i wanted to confine the search or  search and replace within the scope of a function definition … turns out the all powerful emacs can do that and it does it well i think 🙂

first …

emacs has the ability to make every thing else invisible … everything else but your region of interest … the stuff won’t disappear it will simply be not visible… of course u can make it visible again at the click of a few key chords … here is how you do it

first select the region of interest if you simply want to select the entire function simply hit the following keychord within the body of function definition


then to make everything else disappear hit the following

C-x n n

you might have to first all the usage of this if you haven’t used this before.. apparently it confuses some beginners so they disable it by default and warn u on its first use.  onwards now …

now that everything else has become invisible… the search and replace commands miraculously behave as if the other stuff does not exist … buffer movement keys also behave as if the selected region alone exists in the buffer … lucky for us this is precisely what we wanted … after you are done with search and or replace simply hit the following key chord

C-x n w

and everything else will come back !!!

of course its not limited to c++ files alone


Read Full Post »

emacs cpu hog

i m on ubuntu 8.04 ( hardy heron ) and every once in a while emacs freezes … i use htop to find out that emacs and X are hogging up cpu cycles … when i kill emacs it all returns to normal.

some times i see the same thing happening when i have firefox open but this is quite rare … this leads me to believe that probably emacs alone may not be the culprit. but how can i find out exactly what is causing the problem ?

Read Full Post »

in emacs one may use tags to navigate source code. i use etags to generate a tags list for my source code tree and then to navigate one simply needs to hit


which stands for


now to find the next occurance of the same tag one needs to hit

C-u M-.

several times in succession to navigate to all the occurances of the tag. typing that key chord is very discomforting and annoying. one has to move the hands away from the home row and then contort the digits to execute the key chord. in an earlier attempt to make life easier i mapped the Control key to the CapsLock key on the pc keyboard, but that did not offer much relief so i thought of biding an usused key to this key chord.

being an inexperienced noob and after querrying the WWW for many related searches i found a working solution which is as follows

edit your .emacs file to include the following line

(global-set-key  [f9]   (read-kbd-macro "C-u M-."))

this basically maps the function key F9 to the culprit key chord in question.

inspiration/hints for the above hack comes from the following page


Read Full Post »

emacs which c++ function am i in ?

some time when there are a lot of functions in a c++ file, one may get lost trying to figure out which funtion the current line belongs to.  the problem is intensified when the function spans more than a couple screen fuls. and then there is an occasional C-s ( incremental search ) which will lead you to many matches making you forget where you were and confusing you about where you currently are.

for a long time now i wished where was way where emacs could tell me the current function i was in, turns out that emacs does have such a feature:



M-x describe-function

and then type in


to learn more about it.

to turn on globally just add the following in your .emacs

(which-function-mode 1)

Read Full Post »

Older Posts »