Giter Club home page Giter Club logo

evil-nerd-commenter's Introduction

evil-nerd-commenter (v1.5.10)

This program can be used independently WITHOUT Evil!

A Nerd Commenter emulation, help you comment code efficiently. For example, you can press key `,,99j` or `99,ci` to comment out 99 lines.

I recommend using it with Evil though Evil is optional.

Tested on Emacs 23.4, 24.3, 24.4

Why?

A simple use case on the efficiency

If you want to comment out nine lines. Instead pressing “C-space M-9 C-n M-;”, now you can press “M-9 M-;” or “,,9j” or “9,ci”.

The point is that you need NOT mark any text to comment line(s)!

So you strike less keys than the original “M-x comment-dwim”.

Live demo: https://raw.github.com/redguardtoo/evil-nerd-commenter/master/evil-nerd-commenter-demo.gif

It will even fix Emacs bug for you

I’ll provide long-term support for ANY language in the future. Here is an example how I work around a bug in autoconf.el.

Perfect integration with org-mode

The code snippet embedded in org file will automatically be detected and correct comment syntax will be used!

Install

evil-nerd-commenter is already uploaded to http://melpa.org. The best way to install is Emacs package manager.

Set up

The easy way (RECOMMENDED)

You need add one liner into ~/.emacs to use default hotkeys,

(evilnc-default-hotkeys)

That’s all unless you want to modify default key bindings!

The definition of evilnc-default-hotkeys:

(defun evilnc-default-hotkeys ()
  "Set the hotkeys of evil-nerd-comment"
  (interactive)
  (global-set-key (kbd "M-;") 'evilnc-comment-or-uncomment-lines)
  (global-set-key (kbd "C-c l") 'evilnc-comment-or-uncomment-to-the-line)
  (global-set-key (kbd "C-c c") 'evilnc-copy-and-comment-lines)
  (global-set-key (kbd "C-c p") 'evilnc-comment-or-uncomment-paragraphs)
  (eval-after-load 'evil
       (define-key evil-normal-state-map ",ci" 'evilnc-comment-or-uncomment-lines)
       (define-key evil-normal-state-map ",cl" 'evilnc-comment-or-uncomment-to-the-line)
       (define-key evil-normal-state-map ",cc" 'evilnc-copy-and-comment-lines)
       (define-key evil-normal-state-map ",cp" 'evilnc-comment-or-uncomment-paragraphs)
       (define-key evil-normal-state-map ",cr" 'comment-or-uncomment-region)
       ))

As you can see, the default hotkey for commenting lines is “M-;”, if you don’t use evil.

You can set mark at any column of the first line, scroll down, and comment all the lines by pressing “M-;”.

You can insert the content of evilnc-default-hotkeys into .emacs instead of calling it directly.

The hard way

If you don’t use package manager please insert below code into your ~/.emacs:

;; I suppose you know how to install evil.
;; If NOT, check http://www.emacswiki.org/emacs/Evil
(require 'evil)
(evil-mode 1)

;; You need download ONLY evil-nerd-commenter.el and place it somewhere,
;; say "somewhere"" is actually "~/.emacs.d/my-dot-el-directory".
;; Now tell Emacs where to find evil-nerd-commenter.el
(add-to-list 'load-path "~/.emacs.d/my-dot-el-directory")

;; ",," is the default value of evilnc-hotkey-comment-operator,
;; so below line is NOT needed.
;; But please put it *ABOVE* the (require 'evil-nerd-commenter)!!!
;; Please note if you use evil-leader, (setq evilnc-hotkey-comment-operator "\\\\")
;; does NOT work! You need set up the hotkey for command "evilnc-comment-operator" directly.
;; Check the end of README for sample setup
(setq evilnc-hotkey-comment-operator ",,")
(require 'evil-nerd-commenter)
(evilnc-default-hotkeys)

Usage

Available commands and hotkeys

Here are available commands. They are not dependent on evil-mode:

evilnc-comment-or-uncomment-lines (RECOMMENDED)

comment/uncomment lines. This command supports negative arguments.

The hotkey is “,ci” in evil-mode and “M-;” (M means Alt key) in emacs normal mode.

This may be the only command and hotkey you need to learn!

evilnc-quick-comment-or-uncomment-to-the-line (RECOMMENDED)

comment/uncomment from current line to the user-specified line. You can input the rightest digit(s) to specify the line number if you want to type less.

For example, say current line number is 497. “C-u 3 M-x evilnc-quick-comment-or-uncomment-to-the-line” will comment to the line 503 because the rightest digit of “503” is 3.

The hotkey is “,cl” or “,ll” in evil-mode and “C-c l” (C means Ctrl key) in emacs normal mode.

evilnc-comment-or-uncomment-paragraphs (RECOMMENDED)

comment/uncomment paragraphs which separated by empty lines

evilnc-copy-and-comment-lines

copy and paste lines, then comment out original lines. This command supports negative arguments.

The hotkey is “,cc” in evil-mode and “C-c c” in emacs normal mode.

evilnc-toggle-comment-empty-lines

toggle the flag to comment/uncomment empty lines.

The hotkey is “,cv” in evil-mode.

evilnc-copy-to-line

Copy from the current line to the user-specified line.

It’s for non-evil user only.

You need assign hotkey for it.

For example:

(global-set-key (kbd "C-c C-t C-l") 'evilnc-copy-to-line)

evilnc-toggle-invert-comment-line-by-line

Toggle flag evilnc-invert-comment-line-by-line.

When the flag is true, the command “evilnc-comment-or-uncomment-lines”, “evilnc-comment-or-uncomment-to-the-line”, and “evilnc-comment-or-uncomment-paragraphs” will be influenced. They will invert each line’s comment status instead comment the whole thing.

evilnc-kill-to-line

Kill from the current line to the user-specified line.

It’s for non-evil user only.

You need assign hotkey for it.

For example:

(global-set-key (kbd "C-c C-t C-l") 'evilnc-kill-to-line)

evilnc-comment-or-uncomment-to-the-line

Comment to the specified line. evilnc-quick-comment-or-uncomment-to-the-line may be better alternative.

Examples

Comment lines

“C-u NUM M-x evilnc-comment-or-uncomment-lines”, comment/uncomment next NUM lines.

Comment region

Select a region and “M-x evilnc-comment-or-uncomment-lines”. The region will be automatically expanded to contain whole lines. Then we comment/uncomment the region.

Comment to the line number

“C-u 56 M-x evilnc-comment-or-uncomment-to-the-line”, comment/uncomment from current line to line 56.

Copy and comment

“C-u 2 M-x evilnc-copy-and-comment-lines”, copy 2 lines and paste them below the original line. Then comment out original lines. The focus will be moved to the new lines.

Comment paragraph

“C-u 2 M-x evilnc-comment-or-uncomment-paragraphs”, comment out two paragraphs. This is useful if you have large hunk of data to be commented out as below:

var myJson={
  "key1":"v1",
  "key2":"v2",
  "key3":"v3"
}

Invert comment

Say there are two lines of javascript code,

if(flag==true){ doSomething(); }
//if(flag==false){ doSomething(); }

The first line is production code. The second line is your debug code. You want to invert the comment status of these two lines (for example, comment out first line and uncomment the second line) for debug purpose.

All you need to is “M-x evilnc-toggle-invert-comment-line-by-line” then “C-u 2 evilnc-comment-or-uncomment-lines”. The first command turn on some flag, so the behavior of (un)commenting is different.

Evil usage

If you use Evil, you can use text objects and motions.

“evilnc-hotkey-comment-operator” acts much like the delete/change operator. Takes a motion or text object and comments it out, yanking its content in the process.

By default we assign the hotkey “,,” to evilnc-hotkey-comment-operator. If you prefer backslash key, check the next section “Tip 4” in next section.

Here is sample elisp code:

(setq evilnc-hotkey-comment-operator ",,")

Example 1: “,,,” to comment out the current line.

Example 2: “,,9j” to comment out the next 9 lines.

Example 3: “,,99G” to comment from the current line to line 99.

Example 4: “,,a(” to comment out the current s-expression, or “,,i(” to only comment out the s-expression’s content. Similarly for blocks “,,a{“, etc.

Example 5: “,,ao” to comment out the current symbol, or “,,aW” to comment out the current WORD. Could be useful when commenting out function parameters, for instance.

Example 6: “,,w” comment to the beginning of the next word, “,,e” to the end of the next word, “,,b” to the beginning of the previous word.

Example 7: “,,it”, comment the region inside html tags (all html major modes are supported , including web-mode)

Tips

Tip 1, Yank in evil-mode

You can yank to line 99 using hotkey “y99G” or “y99gg”. That’s the awesome feature from evil-mode. Please read vim manual on “text objects and motions” for more details.

Tip 2, change comment style

For example, use double slashes (//) instead of slash-stars (* … *) in c-mode.

Insert below code into your ~/.emacs:

(add-hook 'c-mode-common-hook
  (lambda ()
    ;; Preferred comment style
    (setq comment-start "// "
          comment-end "")))

Thanks for Andrew Pennebaker (aka mcandre) providing this tip.

Tip 3, comment code snippet

Please check my project evil-matchit. You can press “vi%” to select a region between tags and press “M-/” to comment the region.

Most popular programming languages are supported;

Tip 4, use evil-leader if you use the backslash as the leader key in evil-mode

(global-set-key (kbd "M-;") 'evilnc-comment-or-uncomment-lines)
(global-set-key (kbd "C-c l") 'evilnc-quick-comment-or-uncomment-to-the-line)
(global-set-key (kbd "C-c c") 'evilnc-copy-and-comment-lines)
(global-set-key (kbd "C-c p") 'evilnc-comment-or-uncomment-paragraphs)

(require 'evil-leader)
(global-evil-leader-mode)
(evil-leader/set-key
  "ci" 'evilnc-comment-or-uncomment-lines
  "cl" 'evilnc-quick-comment-or-uncomment-to-the-line
  "ll" 'evilnc-quick-comment-or-uncomment-to-the-line
  "cc" 'evilnc-copy-and-comment-lines
  "cp" 'evilnc-comment-or-uncomment-paragraphs
  "cr" 'comment-or-uncomment-region
  "cv" 'evilnc-toggle-invert-comment-line-by-line
  "\\" 'evilnc-comment-operator
  )

Tip 5, (un)comment Lisp code

  • Make sure Evil installed
  • Press “,,a(”

Tip 6, work around Emacs 24.4 issue

At first, latest version (after 2014-10-30) is OK.

If you still got some problem, run below command in shell,

find ~/.emacs.d -type f -iwholename '*/evil-nerd-commenter*.elc' | xargs rm

Credits

Contact me

Report bug at https://github.com/redguardtoo/evil-nerd-commenter.

evil-nerd-commenter's People

Contributors

cruegge avatar darth10 avatar lalopmak avatar mswift42 avatar ryuslash avatar thebb avatar tuhdo avatar untf avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.