emacs注释快捷键

Emacs下支持多行代码的注释/反注释,命令是comment-or-uncomment-region。我喜欢把它绑定在快捷键C-c C-/上,如下:[plain] view plaincopy(global-set-key [?\C-c ?\C-/] 'comment-or-uncomment-region)   这样当选定多行代码的时候就可以方便的进行注释/反注释了。但是这个命令有一个小问题,那就是只能针对当前选中的行(region)做操作。 如果当前没有选中任何行的话就什么也不做。 用过Eclipse的同学都知道,在Eclipse里面用C-/来进行注释/反注释操作时, 如果选中多行则注释/反注释选中行,如果什么都没有选中,则针对当前光标所在行进行操作。 这个功能还是比较方便的,如何能让Emacs也能做到这一点呢? 在自己的.emacs追加如下函数定义就可以了,这个函数会加入针对region的判断和处理:  
(defun my-comment-or-uncomment-region (beg end &optional arg)  
  (interactive (if (use-region-p)  
                   (list (region-beginning) (region-end) nil)  
                 (list (line-beginning-position)  
                       (line-beginning-position 2))))  
  (comment-or-uncomment-region beg end arg)  
)  
(global-set-key [remap comment-or-uncomment-region] 'my-comment-or-uncomment-region)  
现在Emacs的行为接近于Eclipse了,不过还是有几处细微的不同:(1)它不会像Eclipse那样,在选中多行注释的时候,把其中的空行也加上注释 (2)Eclipse的注释一律加在一行顶头的位置,而Emacs会加在相应indent对齐的位置。 当然,具体的行为方式在Emacs中肯定是可以调整的,对我来说现在Emacs的方式已经可以满足我的需要了。

猜你喜欢

转载自blog.csdn.net/u011508640/article/details/52809862
今日推荐