The following description is how I customize
Emacs to work my
personal prerference, which may not complete satisfy what you want.
Anyway, here is a complete solution of my
If you find it useful, don't forget to leave a comment :)
First, set up your personal information.
This is typically used in
Doxymacs for generating file header information.
;; User information (setq user-full-name "Liangfu Chen") (setq user-mail-address "firstname.lastname@example.org")
Generally, to make the configure portable across multiple platforms
(like Windows, Linux, Mac OS etc.),
system-type value is compared
to a list of predefined strings to identify the current operating system,
according to SO . And here is the list of predefine strings:
`gnu` compiled for a GNU Hurd system. `gnu/linux` compiled for a GNU/Linux system. `darwin` compiled for Darwin (GNU-Darwin, Mac OS X, ...). `ms-dos` compiled as an MS-DOS application. `windows-nt` compiled as a native W32 application. `cygwin` compiled using the Cygwin library.
The above values can be used in the following way:
(if (eq system-type 'darwin) ; something for OS X if true ; optional something if not )
Optionally, to revert backgournd and foreground color in Windows, we apply
;; Customized syntax highlighting colors (for Emacs in Windows) (set-foreground-color "#ffffff") (set-background-color "#000000") (set-cursor-color "#ffffff") (custom-set-variables '(load-home-init-file t t))
When we have splited the window into multiple buffers, Emacs provide us
C-x o command to switch between them, but I don't think that
is intuitive. So we define a set of short-cuts here to easily move around
all the visible buffers.
;; Switching between windows (global-set-key [M-left] 'windmove-left) (global-set-key [M-right] 'windmove-right) (global-set-key [M-up] 'windmove-up) (global-set-key [M-down] 'windmove-down)
And for quickly switching between C/C++ header files and its corresponding
when, typically, source files is available in
src folder and
header files in
include folder, the following setting is applied.
;; Bind the toggle function to a global key (global-set-key (kbd "C-c o") 'ff-find-other-file) (setq cc-other-file-alist '(("\\.cpp$" (".hpp" ".h")) ("\\.h$" (".c" ".cpp" ".m" ".mm")) ("\\.hpp$" (".cpp" ".c")) ("\\.m$" (".h")) ("\\.mm$" (".h")) )) (setq ff-search-directories '("." "../src" "../include"))
To force refresh buffer after external changes, I personally combine
revert-buffer command with a single F5 key press :)
;; Using [F5] to refresh file list (defun refresh-file () (interactive) (revert-buffer t t t)) (global-set-key [f5] 'refresh-file) (global-set-key [f9] 'compile)
Quite often, the directory looks so ugly large amount of backup files.
People like me would like to disable
backup-files in Emacs and use
version control systems (e.g. Git, SVN) to manage version history.
;; Diable back up files option (setq make-backup-files nil)
To type a single key
y, instead of
yes, to confirm in
you need to configure it as following.
;; Treat 'y' or <CR> as yes, 'n' as no. (fset 'yes-or-no-p 'y-or-n-p) (define-key query-replace-map [return] 'act)
Well, there are much other options that might be useful,
e.g. displaying time in 24hr format,
indent-tab-width to 2
;; Display time options (setq display-time t display-time-24hr-format t) (display-time)
;; Set indentation preference (setq indent-tabs-mode nil) (setq default-tab-width 2) (setq tab-width 2) (setq tab-stop-list '(2 4 6 8 10 12 14 16 18 20 22 24))
;; Add .fx to c-mode, add .h to c++-mode (add-to-list 'auto-mode-alist '("\\.fx\\'" . c++-mode)) ;; HLSL shader source code (add-to-list 'auto-mode-alist '("\\.cl\\'" . c++-mode)) ;; OpenCL source code (add-to-list 'auto-mode-alist '("\\.cu\\'" . c++-mode)) ;; CUDA source code (add-to-list 'auto-mode-alist '("\\.glsl\\'" . c++-mode)) ;; OpenGL shader source code (add-to-list 'auto-mode-alist '("\\.h\\'" . c++-mode)) ;; (add-to-list 'auto-mode-alist '("\\.m\\'" . octave-mode)) (add-to-list 'auto-mode-alist '("\\SConstruct\\'" . python-mode)) (add-to-list 'auto-mode-alist '("\\SConscript\\'" . python-mode)) (if (file-exists-p "SConscript") (setq compile-command "scons -u")) (if (file-exists-p "SConstruct") (setq compile-command "scons")) ;; (fset 'remove-linenums ;; "\C-@\C-[[1;5C\C-w\C-[OB") ;; (global-set-key (kbd "C-x C-a") 'remove-linenums)
;; This is the logic, and can be used in a program. The other ;; functions use this interactively in various ways. (defun wc-non-interactive (start end) "Count the number of words in the current region." (save-excursion (save-restriction (narrow-to-region start end) (goto-char (point-min)) (count-matches "\\sw+")))) ;;;###autoload (defun count-words-region (start end) "Display number of words in the region." (interactive "r") (message (concat "The current region contains " (number-to-string (wc-non-interactive start end)) " words."))) (global-set-key (kbd "M-#") 'count-words-region)
;; Setting AUCTex mode (setq TeX-auto-save t) (setq TeX-parse-self t) (setq-default TeX-master nil) (setq TeX-PDF-mode t)
;; Using xcscope in emacs (require 'xcscope) ;; Setting Doxymacs mode (require 'doxymacs) (add-hook 'c-mode-common-hook 'doxymacs-mode) ;; (require 'google-c-style) ;; (add-hook 'c-mode-common-hook 'google-set-c-style) ;; (add-hook 'c-mode-common-hook 'google-make-newline-indent)
;; Using matlab-mode (autoload 'matlab-mode "matlab" "Enter MATLAB mode." t) (setq auto-mode-alist (cons '("\\.m\\'" . matlab-mode) auto-mode-alist)) (autoload 'matlab-shell "matlab" "Interactive MATLAB mode." t) (setq matlab-indent-function-body t) ; if you want function bodies indented (setq matlab-verify-on-save-flag nil) ; turn off auto-verify on save (defun my-matlab-mode-hook () (setq indent-tabs-mode nil) (setq default-tab-width 2) (setq tab-width 2) (setq tab-stop-list '(2 4 6 8 10 12)) ) (add-hook 'matlab-mode-hook 'my-matlab-mode-hook)
There is a problem in the default
python-mode that it fails to find other modules
in the same directory, and I've found a
on SO, which worked well for me.
(defun python-reinstate-current-directory () "When running Python, add the current directory ('') to the head of sys.path. For reasons unexplained, run-python passes arguments to the interpreter that explicitly remove '' from sys.path. This means that, for example, using `python-send-buffer' in a buffer visiting a module's code will fail to find other modules in the same directory. Adding this function to `inferior-python-mode-hook' reinstates the current directory in Python's search path." (python-send-string "sys.path[0:0] = ['']")) (add-hook 'inferior-python-mode-hook 'python-reinstate-current-directory)