• Stars
    star
    387
  • Rank 107,381 (Top 3 %)
  • Language
    Emacs Lisp
  • License
    GNU General Publi...
  • Created over 11 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Comment/uncomment lines efficiently. Like Nerd Commenter in Vim

evil-nerd-commenter

https://github.com/redguardtoo/evil-nerd-commenter/actions/workflows/test.yml/badge.svg https://elpa.nongnu.org/nongnu/evil-nerd-commenter.svg http://melpa.org/packages/evil-nerd-commenter-badge.svg http://stable.melpa.org/packages/evil-nerd-commenter-badge.svg

This program can be used WITHOUT evil-mode!

A Nerd Commenter emulation, help you comment code efficiently. For example, you can press “99,ci” to comment out 99 lines.

I recommend using it with Evil though Evil is optional.

Tested on Emacs 25, 26, 27, 28

Why?

A simple use case on the efficiency

The old way to comment out 9 lines is C-space M-9 C-n M-; (“M-;” is the default key binding of comment-dwim.

With this package’s help, you can press “M-9 M-;” or “,,9j” or “9,ci” instead. It’s much faster because you donot need mark any text first!

demo:

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

It fixes Emacs bug for you

Long-term support is provided for ANY programming language. Here is an example to fix a bug in autoconf.el.

Perfect integration with org-mode

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

Install

This package is already uploaded to http://melpa.org. The best way to install is Emacs package manager.

Setup

Please note NO key bindings are setup automatically. You need use following ways to setup key bindings.

Please note v3.2.1 is the last version supporting Emacs 24.3.

Use recommended key bindings

Insert (evilnc-default-hotkeys) into ~/.emacs to use key bindings preset for both evil and non-evil mode. This is recommended way.

Use (evilnc-default-hotkeys t) to use key binding only for non-evil mode if you want to define key bindings in evil-mode by yourself.

Use (evilnc-default-hotkeys nil t) to use key binding only for evil mode if you want to define key bindings in Emacs mode by yourself.

You can also use third party package general.el instead of calling evilnc-default-hotkeys.

Assign key bindings manually

Manual setup is necessary for certain major modes (matlab-mode, for example)

Here is the minimum setup,

(defun matlab-mode-hook-config ()
  (local-set-key (kbd "M-;") 'evilnc-comment-or-uncomment-lines))
(add-hook 'matlab-mode-hook 'matlab-mode-hook-config)

Usage

Commands and hotkeys

Here are available commands which are NOT dependent on evil-mode:

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

Comment/uncomment lines. This command supports negative arguments.

The hotkey is “,cl” 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 inside 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!

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

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

comment/uncomment paragraphs which is 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-comment-and-kill-ring-save

Comment lines and insert original lines into kill-ring.

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

Comment to the specified line.

evilnc-comment-or-uncomment-html-tag

Comment or uncomment current html tag or selected region.

It supports html and jsx without any set up. It’s not dependent on any third party package.

Please note you don’t need force the whole line selection (pressing V) in evil-mode. This command is smart to select whole lines if needed.

Comment or uncomment html tag(s).

If no region is selected, current tag under focus is automatically selected. In this case, only one tag is selected.

If user manually selects region, the region could cross multiple sibling tags and automatically expands to include complete tags. So user only need press v key in evil-mode to select multiple tags.

Or you can use evilnc-comment-or-uncomment-html-paragraphs to comment/uncomment paragraphs containing html tags.

Paragraph is text separated by empty lines.

Sample to combine evilnc-comment-or-uncomment-html-paragraphs and evilnc-comment-or-uncomment-paragraphs:

(defun my-current-line-html-p (paragraph-region)
  (let* ((line (buffer-substring-no-properties (line-beginning-position)
                                               (line-end-position)))
         (re (format "^[ \t]*\\(%s\\)?[ \t]*</?[a-zA-Z]+"
                     (regexp-quote (evilnc-html-comment-start)))))
    ;; current paragraph does contain html tag
    (if (and (>= (point) (car paragraph-region))
             (string-match-p re line))
        t)))

(defun my-evilnc-comment-or-uncomment-paragraphs (&optional num)
  "Comment or uncomment NUM paragraphs which might contain html tags."
  (interactive "p")
  (unless (featurep 'evil-nerd-commenter) (require 'evil-nerd-commenter))
  (let* ((paragraph-region (evilnc--get-one-paragraph-region))
         (html-p (or (save-excursion
                       (sgml-skip-tag-backward 1)
                       (my-current-line-html-p paragraph-region))
                     (save-excursion
                       (sgml-skip-tag-forward 1)
                       (my-current-line-html-p paragraph-region)))))
    (if html-p (evilnc-comment-or-uncomment-html-paragraphs num)
      (evilnc-comment-or-uncomment-paragraphs num))))

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.

Please note this command may NOT work on complex evil text object.

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-both-snippet-html

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.

evilnc-comment-box

Comment out N lines, putting it inside a box. N could be negative. If N is nil, comment out current paragraph. This command uses builtin API comment-box.

The hotkey is “,cs” in evil-mode

Use imenu to list and jump to comments in current file

Please setup `imenu-create-index-function’ to `evilnc-imenu-create-index-function’.

Setup on using counsel-imenu to list comments in current buffer,

(defun counsel-imenu-comments ()
  (interactive)
  (let* ((imenu-create-index-function 'evilnc-imenu-create-index-function))
    (unless (featurep 'counsel) (require 'counsel))
    (counsel-imenu)))

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. But if you only deals with lines, I suggest using evilnc-comment-or-uncomment-lines instead.

commenter text object “c”

We defined commenter text object “c” which can have multi-lines.

Press vac to select outer object (comment with limiters).

Press vic to select inner object (comment without limiter).

The comment text object is created automatically in evilnc-default-hotkeys.

You can assign other key instead of “c” to the text object by changing evilnc-comment-text-object.

(setq evilnc-comment-text-object "c")
(evilnc-default-hotkeys)

You can also manually created the comment text object using below code,

(setq evilnc-comment-text-object "a")
(define-key evil-inner-text-objects-map evilnc-comment-text-object 'evilnc-inner-commenter)
(define-key evil-outer-text-objects-map evilnc-comment-text-object 'evilnc-outer-commenter)

evilnc-comment-operator

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)

evilnc-copy-and-comment-operator

evilnc-copy-and-comment-operator is another evil-mode operator. Instead of commenting out the text in the operator-range, it inserts an copy of the text in the range and comments out that copy. Its hot key is “,.”. For example, “,.,” to comment out the current line.

evilnc-yank-and-comment-operator

Operator to comment or uncomment the text and yank the original text at the same time.

Tips

Yank in evil-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”.

Change comment style

For example, if you prefer 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.

Comment code snippet

Please install 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.

Comment and uncomment Lisp code

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

Choose the style of copy and comment

You can set up evilnc-original-above-comment-when-copy-and-comment to decide which style to use when evilnc-copy-and-comment-lines or evilnc-copy-and-comment-operator,

  • Place the commented out text above original text
  • Or place the original text above commented out text

Customize comment style

Most commands call evilnc-comment-or-uncomment-region-function.

You can modify this variable to customize the comment style.

(with-eval-after-load 'evil-nerd-commenter
  (defun my-comment-or-uncomment-region (beg end)
    (let* ((comment-start "aaa")
           (comment-end "bbb"))
      (evilnc-comment-or-uncomment-region-internal beg end)))
  (setq evilnc-comment-or-uncomment-region-function
        'my-comment-or-uncomment-region))

Credits

Contact me

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

More Repositories

1

mastering-emacs-in-one-year-guide

Be great at emacs in one year
6,330
star
2

emacs.d

Fast and robust Emacs setup.
Emacs Lisp
2,375
star
3

find-file-in-project

Quick access to project files in Emacs
Emacs Lisp
426
star
4

elpa-mirror

Create local emacs package repository. 15 seconds to install 115 packages.
Emacs Lisp
304
star
5

evil-matchit

Vim matchit ported into Emacs
Emacs Lisp
283
star
6

cpputils-cmake

Easy real time C++ syntax check and intellisense if you use CMake
Emacs Lisp
192
star
7

counsel-etags

Fast, energy-saving, and powerful code navigation solution
Emacs Lisp
173
star
8

wucuo

Fastest solution to spell check camel case code or plain text
Emacs Lisp
119
star
9

eacl

eacl - Emacs auto complete lines by grepping project
Emacs Lisp
97
star
10

vc-msg

Show commit message of current line in Emacs
Emacs Lisp
75
star
11

cliphist

Paste from clipboard manager into Emacs
Emacs Lisp
73
star
12

js-comint

js-comint will send the code from Emacs into node.js or rhino
Emacs Lisp
67
star
13

company-ctags

Fastest Emacs auto-completion using Company and Ctags
Emacs Lisp
52
star
14

pyim-tsinghua-dict

用清华大学开放中文词库数据建立的pyim 输入法]词库. 已基于词频统计信息DF值(Document Frequency)优化
Python
47
star
15

mybigword

Use Zipf frequency of each word to extract English big words
Emacs Lisp
36
star
16

org2nikola

export org into html used by static blog generator like https://github.com/getnikola/nikola
Emacs Lisp
26
star
17

myelpa

Mirror of Emacs packages I'm using
23
star
18

vscode-setup

21
star
19

gmail2bbdb

convert gmail contacts to BBDB file, easy to use, robust, no dependency
Emacs Lisp
20
star
20

vscode-matchit

Jump between matching HTML tags and brackets smartly in VS Code. It's ported from Vim matchit by Benji Fisher
TypeScript
19
star
21

find-by-pinyin-dired

Find file by first Pinyin characters of Chinese Hanzi. 输入拼音首字母定位对应的中文目录/文件
Emacs Lisp
18
star
22

imenu-extra

Add extra items into lsp-mode/js2-mode imenu items
Emacs Lisp
15
star
23

redguardtoo.github.io

my blog
HTML
14
star
24

NinjaWebCoder

Use keyboard to copy code from stackoverflow
HTML
14
star
25

pyim2fcitx

Conver pyim dictionary to fcitx dictionary
Python
12
star
26

dianyou

Search/Analyze mails in Gnus
Emacs Lisp
12
star
27

test-git-mergetool

toy project to test git mergetool
12
star
28

lazyflymake

Lightweight syntax checker for Emacs, alternative of `flymake-mode'
Emacs Lisp
11
star
29

evil-mark-replace

replace string in marked region effectively
Emacs Lisp
9
star
30

shellcop

Analyze errors reported in Emacs builtin shell
Emacs Lisp
9
star
31

counsel-bbdb

Quick search&input email from BBDB based on ivy
Emacs Lisp
9
star
32

js2hl

Highlight/rename things using `js2-mode' parser
Emacs Lisp
8
star
33

shenshou

download subtitles from opensubtitles.org
Emacs Lisp
8
star
34

spell-check-code-in-ci

Free and powerful solution to spell check code at Continuous integration server
Makefile
7
star
35

fastdef

Insert terminology faster
Emacs Lisp
6
star
36

root-dotfiles

my dotfiles as root at Linux host
Shell
6
star
37

jqcuo

Powerful and simple solution to spell check code in command line
Emacs Lisp
6
star
38

jest-coverage-for-files-outside-of-root

jest coverage for files outside of root
JavaScript
5
star
39

diff-lisp

Diff files&strings in pure Emacs Lisp.
Emacs Lisp
5
star
40

find-and-ctags

Use `find' and `ctags' for code navigation
Emacs Lisp
5
star
41

portable-percol

portable percol (https://github.com/mooz/percol)
Python
5
star
42

zhfreq

processing chinese text using word frequency data
Emacs Lisp
4
star
43

AX88772C_Source

my usb to net adapter
C
4
star
44

my-emacs.d-snapshot

Emacs Lisp
4
star
45

test-git-blame

A project to test git blame
JavaScript
3
star
46

roblox-mode

Emacs major mode to edit Roblox Studio rbxlx files
Emacs Lisp
3
star
47

org-mime

org html export for text/html MIME emails
3
star
48

perl-recordmydesktop

perl-recordmydesktop.pl is the frontend for recordMyDesktop screencast tool
Prolog
3
star
49

test-git-log

JavaScript
1
star
50

asus-pce-n53-11n-n600

ASUS PCE-N53 wireless patched driver and gentoo installation mini-guide
C
1
star
51

ms-frontend-sandbox

sandbox for interviewers
JavaScript
1
star
52

helloworld

hello world
C
1
star
53

wxwidgets-help

Look up wxWidgets API in Emacs by using local html manual
Emacs Lisp
1
star