我的Emacs(Ubuntu下写C)
关键字: ubuntu c emacs最近实践Ubuntu下编写C语言程序,在Emacs配置和使用上费了不少功夫,所以在这里重新整理
1.配置文件
为了方便修改(比如增加一些新插件产生冲突的情况),把配置文件分开,方便管理和修改
.emacs,某些实验的插件先放在主配置文件中,后期会分类出去。目前使用的插件主要有color-theme.el,cedet,linum.el,gud,gdb,yasnippet,javascript.el,插件的安装方法还算简单,但是想让它们都生效并且一起工作的很好就不那么容易,上面几乎每个插件我都折腾了好久才能正常使用。后面的部分我会记录几个容易犯错的地方
;;将配置文件分成多个文件 ;;face.el emacs外观 ;;plugin.el 插件 ;;cmode.el C/C++配置 ;插件路径 (add-to-list 'load-path "~/.emacs.d") ;; 导入配置的文件 (load "face.el") (load "plugin.el") (load "cmode.el") ;;gdb配置 (setq gdb-many-windows t) (load-library "multi-gud.el")
cmode.el(参考http://www.caole.net/diary/emacs_write_cpp.html)
;;;; CC-mode配置 http://cc-mode.sourceforge.net/
(require 'cc-mode)
(c-set-offset 'inline-open 0)
(c-set-offset 'friend '-)
(c-set-offset 'substatement-open 0)
;;cscope
;;2008422增加
(add-hook 'c-mode-common-hook
'(lambda()
(require 'xcscope)))
(setq cscope-do-not-update-database t)
;;;;C/C++语言编辑策略
(defun my-c-mode-common-hook()
(setq tab-width 4 indent-tabs-mode nil)
;;; hungry-delete and auto-newline
(c-toggle-auto-hungry-state 1)
;;按键定义
(define-key c-mode-base-map [(control \`)] 'hs-toggle-hiding)
(define-key c-mode-base-map [(return)] 'newline-and-indent)
(define-key c-mode-base-map [(f7)] '(compile-command "make"))
(define-key c-mode-base-map [(meta \`)] 'c-indent-command)
;;(define-key c-mode-base-map [(tab)] 'hippie-expand)
;;(define-key c-mode-base-map [(tab)] 'my-indent-or-complete)
(define-key c-mode-base-map [(meta ?/)] 'semantic-ia-complete-symbol-menu)
;;预处理设置
(setq c-macro-shrink-window-flag t)
(setq c-macro-preprocessor "cpp")
(setq c-macro-cppflags " ")
(setq c-macro-prompt-flag t)
(setq hs-minor-mode t)
(setq abbrev-mode t)
)
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
;;;;我的C++语言编辑策略
(defun my-c++-mode-hook()
(setq tab-width 4 indent-tabs-mode nil)
(c-set-style "stroustrup")
;; (define-key c++-mode-map [f3] 'replace-regexp)
)
;载入cedet插件
(load-file "~/.emacs.d/cedet-1.0pre4/common/cedet.el")
;配置Semantic的检索范围
(setq semanticdb-project-roots
(list
(expand-file-name "/")))
;自定义自动补齐命令
(defun my-indent-or-complete ()
(interactive)
(if (looking-at "\\>")
(hippie-expand nil)
(indent-for-tab-command))
)
(global-set-key [(control tab)] 'my-indent-or-complete)
;hippie的自动补齐策略
(autoload 'senator-try-expand-semantic "senator")
(setq hippie-expand-try-functions-list
'(
senator-try-expand-semantic
try-expand-dabbrev
try-expand-dabbrev-visible
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-expand-list
try-expand-list-all-buffers
try-expand-line
try-expand-line-all-buffers
try-complete-file-name-partially
try-complete-file-name
try-expand-whole-kill
)
)
face.el,设置界面、主题,可以看附件中的截图,黑色主题、最大化和一些习惯设置
;------------------------------- 设置界面------------------------------- ;最大化 (defun my-maximized () (interactive) (x-send-client-message nil 0 nil "_NET_WM_STATE" 32 '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0)) (x-send-client-message nil 0 nil "_NET_WM_STATE" 32 '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0)) ) ;启动时最大化 (my-maximized) (setq inhibit-startup-message t);启动不出现闪屏 (setq visible-bell t);关闭出错提示声 (setq make-backup-files nil);不产生备份文件 (global-font-lock-mode t);语法高亮 (auto-image-file-mode t);打开图片显示功能 (column-number-mode t);显示列号 (tool-bar-mode nil);去掉那个大大的工具栏 (scroll-bar-mode nil);去掉滚动条 (setq x-select-enable-clipboard t);允许和clipboard交互 (transient-mark-mode t);加亮选中部分 (require 'linum) (global-linum-mode t);显示行号 (require 'color-theme) ;;(color-theme-initialize) ;;(color-theme-tty-dark) (color-theme-comidia) ;-----------------------------end of 设置界面-------------------------- ;------------------------------- 设置显示时间--------------------------- (display-time-mode 1) (setq display-time-24hr-format t) (setq display-time-day-and-date t) ;--------------------------------endof 设置显示时间--------------------- (global-set-key [f11] 'my-fullscreen) ;------------------------------- 设置屏幕初始大小--------------------------- ;全屏 (defun my-fullscreen () (interactive) (x-send-client-message nil 0 nil "_NET_WM_STATE" 32 '(2 "_NET_WM_STATE_FULLSCREEN" 0)) )
plugin.el(http://code.google.com/p/yasnippet/),一个模拟TextMate的东西,可以自定义snippet,目前用用感觉不是特别好,希望后期版本能有提高,还与其他插件有冲突,这个就比较头疼
(require 'yasnippet-bundle)
(autoload 'javascript-mode "javascript" nil t
(add-to-list 'auto-mode-alist '("\\.js\\'" . javascript-mode))
2.使用gdb插件进行调试
算是一个插件的使用了,但是说起来还是挺烦的,搞了很久才运行的差不多,参考好多网站,安装的话看这个:http://blog.csdn.net/nhczp/archive/2007/11/02/1862695.aspx,安装很容易,然后就可以打开编辑器调试了,我用的时候一直无法手动添加断点,提示是Current buffer has no process,不知道是啥问题,索性每次都是命令行敲命令(b main或者break (line),挺郁闷。还有一个比较容易出错的地方是编译的文件需要加上-g的选项,确保每个文件都可以进入调试模式,否则将无法出现在调试界面里面。
gdb在Emacs里面使用很方便,提供了很多命令的快捷键绑定,我目前常用三个,为了形象起见,对比Eclipse Debug mode快捷键说明:
C-c C-N 单行执行 对应Eclipse的F6 C-c C-s 单步执行 对应Eclipse的F5 C-c C-r continue命令,执行到下一断点 对应Eclipse的F8
另外还有一些常用的命令行命令
run (args) 启动程序,如果程序包含参数,直接在后面写 b (method_name) 为指定方法加断点 (还不知道怎么为某个文件某行加断点怎么做) break (line_number) 为当前视图中的文件指定行增加断点 quit 退出gdb print (args) 打印变量 如果args是$1这种形式,则可以打印历史变量,它是GDB按照顺序记录的 whatis (args) 打印变量的类型
3.日积月累的快捷键
用Emacs已经有半年多,写Java的时候主要是在Eclipse里面用Emacs Mode,感觉还是不错的,而且还能郁闷其他想用你电脑的同事:P。
Emacs快捷键实在是太多了,所以只能列点比较常用的,虐待自己手指头哇
C/M-F B N P C/M-V M-< > C-a C-e经典Emacs位移快捷键,没啥好说的 C-x o / C-x 1,2... 窗口移动 数量控制 C-x b/ C-x C-b 切换buffe C-x k 关闭当前buffer C-o 创建新行 光标不动 C-j 创建新行 光标到新行并缩进(类似于Vim的o) C-s r /C-M-s r 增量搜索和正则搜索 C-%/M-% 替换 C-M-2 Mark Set(超级不爽的快捷键,除非你愿意损失C-Spc) C-x h 全选 配合 C-M-\ 格式化整个文本 C-x C-c 关闭Emacs C-z 最小化 C-x C-s 保存当前文本 C-h m 打开帮助文档 C-h k 查看快捷键锁定(看看你设定的快捷键是否正确绑定功能) C/M-w 复制、剪切 C-y 将killing-ring中的东西粘贴 C-k 删除光标以后到行尾 C/M-d 删除光标后单字或单词 C-c C-c 格式化 C-x C-f 打开文件
4.在emacs中使用cscope对C/C++代码管理
参考http://pluskid.lifegoo.com/wiki/EmacsCscope.html
安装cscope,然后将安装包中自带的xcscope.el加载到emacs配置文件中即可使用,可以方便查看源代码
- 03:23
- 浏览 (523)
- 评论 (1)
- 分类: Lots of IDE
- 相关推荐
发表评论
- 浏览: 90798 次
- 性别:

- 来自: 上海

- 详细资料
搜索本博客
最近加入圈子
最新评论
-
使用JS实现拖动 BBCode版
看下bbcode
-- by zjpsh -
Skype 大家一起来
我只是在windows下用过,还没有在ubuntu上用过,过几天尝试一下。
-- by xiaobin268 -
我的Emacs(Ubuntu下写C)
羡慕死你了·~~
-- by Emy -
使用JS实现拖动 BBCode版
用框架做简单
-- by fei1985 -
MyEclipse你搞什么
为了装逼,不用myeclipse
-- by fuwang






评论排行榜