This program can be used independently WITHOUT evil-mode!
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, 24.5
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”.
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.
The code snippet embedded in org file will automatically be detected and correct comment syntax will be used!
evil-nerd-commenter is already uploaded to http://melpa.org. The best way to install is Emacs package manager.
You need only one line into ~/.emacs,
(evilnc-default-hotkeys)
Install evil-leader according to its README.
Insert following code into you ~/.emacs,
;; Emacs key bindings
(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)
;; Vim key bindings
(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 ; if you prefer backslash key
)
Here are available commands. They are NOT dependent on evil-mode:
If you use evil-leader, the “,” in all evil-mode hotkeys is replaced with “".
comment/uncomment lines. This command supports negative arguments.
The hotkey is “,ci” in evil-mode and “M-;” in all modes. “M” means ALT key.
If a region selected, the region is expand to make sure the region contain whole lines. Then we comment/uncomment the expanded region. NUM is ignored.
If the region is insided of ONE line, we comment/uncomment that region. In this case, CORRECT comment syntax will be used for C++/Java/Javascript.
This may be the only command you need to learn!
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.
comment/uncomment paragraphs which separated by empty 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.
Comment to the specified line. evilnc-quick-comment-or-uncomment-to-the-line may be better alternative.
toggle the flag to comment/uncomment empty lines.
The hotkey is “,cv” in evil-mode.
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)
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.
Please note this command may NOT work on complex evil text object.
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)
If a line is snippet wrapped HTML tags in HTML template, only the HTML syntax is used to comment out the line by default.
But if you `(setq evilnc-comment-both-snippet-html t)`, snippet will be commented out with its own syntax at first. Then the wrapped html tag will be comment out using HTML syntax. This flag has effect on all above commands. Web-mode should be enabled to use this flag.
“C-u NUM M-x evilnc-comment-or-uncomment-lines”, comment/uncomment next NUM lines.
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.
“C-u 56 M-x evilnc-comment-or-uncomment-to-the-line”, comment/uncomment from current line to line 56.
“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.
“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"
}
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.
If you use Evil, you can use text objects and motions. But if you only deals with lines, I suggest using evilnc-comment-or-uncomment-lines instead.
“evilnc-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.
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)
You can yank to line 99 using hotkey “y99G” or “y99gg”. That’s the feature from evil-mode.
Please read vim manual on “text objects and motions”.
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.
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;
- Make sure Evil installed
- Press “,,a(”
- Lally Oppenheimer (AKA lalopmak) added the support for text-object in Evil
- Tom Willemse (AKA ryuslash) provided the fix to make Emacs 24.4 work
- Eivind Fonn (AKA TheBB) fixed the web-mode issue #45
Report bug at https://github.com/redguardtoo/evil-nerd-commenter.