A quick-tips Emacs post

by

In this post, I’m basically going to just cut-paste code from my .emacs file, customizations that I feel have been most useful to me:

1) Cscope customizations

;for cscope
(load-file "~/.emacs.d/xcscope.el")
(require 'xcscope)
(setq cscope-do-not-update-database t)
(define-key global-map [(control f3)] 'cscope-set-initial-directory)
(define-key global-map [(control f4)] 'cscope-find-this-file)
(define-key global-map [(control f5)] 'cscope-find-this-symbol)
(define-key global-map [(control f6)] 'cscope-find-global-definition)
(define-key global-map [(control f7)] 'cscope-find-this-text-string)
(define-key global-map [(control f8)] 'cscope-pop-mark)
(define-key global-map [(control f9)] 'cscope-find-functions-calling-this-function)
(define-key global-map [(control f10)] 'cscope-find-called-functions)
(define-key global-map [(control f11)] 'cscope-display-buffer)
;cscope settings end here

An earlier tutorial I wrote for the cscope-newbie.

2) Auto-completion with Smart Tab

;function to implement a smarter TAB
(global-set-key [(tab)] 'smart-tab)
(defun smart-tab ()
"This smart tab is minibuffer compliant: it acts as usual in
the minibuffer. Else, if mark is active, indents region. Else if
point is at the end of a symbol, expands it. Else indents the
current line."
(interactive)
(if (minibufferp)
(unless (minibuffer-complete)
(dabbrev-expand nil))
(if mark-active
(indent-region (region-beginning)
(region-end))
(if (looking-at "\\_>")
(dabbrev-expand nil)
(indent-for-tab-command)))))

EDIT :: if the above does not work for you, change the first line to

(global-set-key (kbd "TAB") 'smart-tab)

Now it should work with GTK / GUI mode as well as with –no-window-system, -nw option.

3) Settings to improve aesthetics and speed

;Settings from Steve Yegge's Effective Emacs
(global-set-key "\C-x\C-m" 'execute-extended-command)
(global-set-key "\C-c\C-m" 'execute-extended-command)
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
(global-set-key "\C-w" 'backward-kill-word)
(global-set-key "\C-x\C-k" 'kill-region)
(global-set-key "\C-c\C-k" 'kill-region)

From Effective Emacs, one of the best articles on leveraging the power of emacs.

4) Some settings that I have made over time

;My Settings (experimental)
(global-set-key "\C-z" 'multi-occur)
(global-set-key "\M-j" 'pop-to-mark-command)
(global-set-key "\M-q" 'revert-buffer)

I find these immensely helpful.

5) Settings for indentation

;indentation settings from Documentation/CodingStyle
(defun c-lineup-arglist-tabs-only (ignored)
"Line up argument lists by tabs, not spaces"
(let* ((anchor (c-langelem-pos c-syntactic-element))
(column (c-langelem-2nd-pos c-syntactic-element))
(offset (- (1+ column) anchor))
(steps (floor offset c-basic-offset)))
(* (max steps 1)
c-basic-offset)))
(add-hook 'c-mode-common-hook
(lambda ()
;; Add kernel style
(c-add-style
"linux-tabs-only"
'("linux" (c-offsets-alist
(arglist-cont-nonempty
c-lineup-gcc-asm-reg
c-lineup-arglist-tabs-only))))))
(add-hook 'c-mode-hook
(lambda ()
(let ((filename (buffer-file-name)))
;; Enable kernel mode for the appropriate files
(when (and filename
(string-match (expand-file-name "~/src/linux-trees")
filename))
(setq indent-tabs-mode t)
(c-set-style "linux-tabs-only")))))

The indentation style is from here

6) Another awesomely helpful tip:

;Copy-only from M-x all things emacs.
(defun copy-line (&optional arg)
"Do a kill-line but copy rather than kill. This function directly calls
kill-line, so see documentation of kill-line for how to use it including prefix
argument and relevant variables. This function works by temporarily making the
buffer read-only, so I suggest setting kill-read-only-ok to t."
(interactive "P")
(toggle-read-only 1)
(kill-line arg)
(toggle-read-only 0))
(setq-default kill-read-only-ok t)
(global-set-key "\C-c\C-k" 'copy-line)

From M-x all-things-emacs

So there. These are some of the most useful customizations I’ve discovered over time. I hope you find them as useful as I have. If you have good tips to offer, please let me know in the comments section!

About these ads

Tags: ,

One Response to “A quick-tips Emacs post”

  1. Graham Fawcett Says:

    I like the copy-line command, thanks for that! I find it’s a bit more intuitive if your cursor doesn’t move from one spot to another when you call it, though. Changing (kill-line arg) to (save-excursion (kill-line arg)) corrects this small issue.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: