Tired of the slow ssh access to one of the servers located outside India, I finally figured it was time to start playing with emacs to reduce the number of keystrokes needed.
First on, I wanted the linux-c-mode enabled *by default* whenever I open a file whose extension is ".c" (Yeah Yeah, I know extensions are meaningless for Linux.. but emacs is intelligent π ). For all the other type of files, linux-c-mode should be OFF, because it will mess-up their indentation.
Second, line numbers should be displayed on the left-hand-side panel of each file I open. Then to go a particular line I just "M-g g". Simple.
Third, All this should be accessible to my user account as well as root user.
So, I edit the ~/.emacs file (create if not already present) like this (for the first goal):
(defun linux-c-mode ()
"C mode with adjusted defaults for use with the Linux kernel."
(interactive)
(c-mode)
(c-set-style "K&R")
(setq tab-width 8)
(setq indent-tabs-mode t)
(setq c-basic-offset 8))
(add-to-list 'auto-mode-alist '("\.c$" . linux-c-mode))
The first chunk defines the linux-c-mode (from Documentation/CodingStyle) and the last line enables it by default ONLY for *.c files. Simple!
For the second part, I got a script linum.el from google (Vedang says it has been integrated into emacs)
You can find the script here: http://jitesh.1337.googlepages.com/linum.el
Drop this script at /usr/share/emacs/site-lisp/
There’s a problem with this script. It doesn’t print a space after the line number. Sad!
Here is a patch I wrote to fix it: http://jitesh.1337.googlepages.com/linum.el.patch
Now I want to display line numbers for EACH file I open in emacs. So, I add the following lines in the .emacs file:
(require ‘linum)
(global-linum-mode 1)
(First line is needed to autoload linum.el. The second line enables it by default)
To toggle the linum mode, use "M-x linum-mode"
Simple!
Finally, I wanted to give access to root user too.
A simple solution would be to make a symlink/hardlink from /root/.emacs to /home/jitesh/.emacs.
However, I wasn’t too concerned about the other users on the system π .So, I simply copied to .emacs file to /usr/share/emacs/site-lisp/site-start.d/ and renamed it as myinit.pl. So, now, these things get enabled by default for *all* users.
Now that everything is setup (and steps documented via this blog), let me go back to coding peacefully in emacs!
Tags: Emacs
June 25, 2009 at 12:21 pm |
linum.el comes inbuilt with Emacs from version 22 onwards. I don’t know about the spacing thing, since i don’t use linum myself, so people interested in this should simply get the latest copy of emacs (preferably from CVS) and then follow the steps mentioned by Jitesh from the patch point onwards.
June 25, 2009 at 12:40 pm |
I would also suggest changing the autoload line to:
(add-to-list ‘auto-mode-alist ‘(“\\.\\(c\\|h\\)$” . linux-c-mode))
July 4, 2009 at 1:26 pm |
there is a typo in the post: in the linux-c-mode definition, setq tab-width should be 8. it has been replaced by a smiley by stupid html processing.
(setq tab-width 8)
July 4, 2009 at 1:30 pm
see what i mean? π
June 26, 2009 at 6:06 am |
yeahhh.. forgot .h files.. thank you thank you π
About emacs,
# rpm -q emacs-common
emacs-common-22.3-11.fc11.i586
# rpm -q –list emacs-common | grep linum
May be its in CVS and not released yet?
Had a look at the fedora repository. It doesn’t strip anything from upstream. Simply adds some of its own stuff. so, not really sure what happening here π
September 9, 2009 at 2:19 pm |
I was going through my elisp code, sprucing things up, when I noticed that the linux-c-mode function imposes c-mode on even the cpp files, thereby suppressing (so far undiscovered) advantages of c++-mode for cpp files. This is the solution I propose:
August 7, 2010 at 6:06 am |
I was not able to get the above to work properly (it kept giving me 6 space indents instead of 8). Starting off with Linus’s coding standards and hacking your stuff around a bit I came up with the following that worked:
(defun linux-c-mode ()
“C mode with adjusted defaults for use with the Linux kernel.”
(interactive)
(c-mode)
(c-set-style “K&R”)
(setq c-indent-level 8)
(setq c-basic-offset 8)
(setq c-brace-imaginary-offset 0)
(setq c-brace-offset -8)
(setq c-argdecl-indent 8)
(setq c-label-offset -8)
(setq c-continued-statement-offset 8)
(setq indent-tabs-mode t)
(setq tab-width 8)
)
(add-to-list ‘auto-mode-alist ‘(“\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'” . linux-c-mode))
(add-to-list ‘auto-mode-alist ‘(“\\.[ch]\\'” . linux-c-mode))