Is there a way to comment out a large chunk of code? - emacs

I'm trying to comment out a large piece of code in a program but it's too difficult to do it manually. Is there a program to do it for me? (making such program doesn't seem hard anyway)
Also, I'm using emacs. Does emacs have such a function? (or an emacs Lisp file at least?)

In emacs, you can use M-x comment-region and M-x uncomment-region.

The comment-region method bound to key M-;. You can select region using marker and then use M-; to comment/uncomment.

You can mark at the beginning of the region that you want to comment out, move to the end of the region, then do C-c C-c or M-x comment-region.

I bind comment-region to C-M-;. And remember that C-u uncomments the commented region.
E.g. C-u 2 C-M-; comments the region with ;; (in Lisp). Then C-u C-M-; uncomments the region. (The region must be active in both cases.)

Related

How to uncomment blocks code in emacs

I am using emacs with the major mode "Java/L Abbrev" activated. When I type M-x comment-region or M-x uncomment-region the desired effects happen in the editor. But I am getting tired of typing this out every time.
I have found that I can type C-c C-c and comment a region. I want to find a similiar way to uncomment a region. I go to the emacs docs:
https://www.gnu.org/software/emacs/manual/html_node/ccmode/Comment-Commands.html
And it says to give the C-c C-c command a negative argument to uncomment lines. How do I do this? or is there a better way?
Please try M-;, which is bound by default to comment-dwim. I think this should do what you want.
comment-dwim (DWIM stands for "Do What I Mean") is bound to M-; by default and works differently depending on whether or not the region is active (and sometimes what mode you're in)
From the emacs help page for comment-dwim:
comment-dwim is an interactive compiled Lisp function in
‘newcomment.el’.
It is bound to M-;.
(comment-dwim ARG)
Call the comment command you want (Do What I Mean).
If the region is active and ‘transient-mark-mode’ is on, call
‘comment-region’ (unless it only consists of comments, in which
case it calls ‘uncomment-region’).
Else, if the current line is empty, call ‘comment-insert-comment-function’
if it is defined, otherwise insert a comment and indent it.
Else if a prefix ARG is specified, call ‘comment-kill’.
Else, call ‘comment-indent’.
You can configure ‘comment-style’ to change the way regions are commented.
Your question is how to use C-c C-c to uncomment the region.
#AaronHarris answered your question about using a negative prefix arg.
But I think you misread the doc of comment-region (which CC mode binds to C-c C-). It does not uncomment the region. It deletes a certain number of comment characters.
To uncomment the region you use C-u - a plain prefix arg (no explicit number) to uncomment the region. C-h f comment-region says:
comment-region is an interactive compiled Lisp function in
newcomment.el.
It is bound to menu-bar edit region comment-region.
(comment-region BEG END &optional ARG)
Comment or uncomment each line in the region.
With just C-u prefix arg, uncomment each line in region BEG .. END.
Numeric prefix ARG means use ARG comment characters.
If ARG is negative, delete that many comment characters instead.
The strings used as comment starts are built from comment-start
and comment-padding; the strings used as comment ends are built
from comment-end and comment-padding.
By default, the comment-start markers are inserted at the
current indentation of the region, and comments are terminated on
each line (even for syntaxes in which newline does not end the
comment and blank lines do not get comments). This can be
changed with comment-style.
So the answer is to use C-u C-c C-c.
And FWIW, comment-region is much better than M-; (comment-dwim) for commenting and uncommenting the region. It lets you nest and unnest comment blocks any number of levels.
TLDR: Use C-- C-c C-c; i.e., prefix your command with "control-hyphen"
To give a negative argument to a command, you need to call either the negative-argument command or the universal-argument command, supplying a negative argument. (Try C-h f for more information on these.)
The negative-argument command is bound to keys C--, M--, and C-M--, so all of these will work as prefixes; generally, you'll use the one that's most convenient to type for any given command.
The universal-argument command is bound to C-u and accepts its argument immediately after that, so you can also do C-u -, optionally followed by zero or more digits (e.g., C-u - 5 3 9); that one is overkill here, but good to know about.
Finally, here is the section of the Emacs manual that discusses this topic.
Why not mapping the uncomment-region command to a key? It's not really what you were asking for (previous answer are better for this) but it's a way to stop typing M-x uncomment-region every time
like this (with the key binding you want)
(global-set-key (kbd "C-c C-u") 'uncomment-region)
Documentation about key binding can be found here:
Commands for Binding Keys
Customizing Key Bindings
Here is a map of command:
Map of command
You can use C-h k (M-x describe-key) to show what command is bind to a particular key (so you're sure to not erase it) and C-h f (M-x describe-function) will show you a description of the function + its binding.
First, select the region
To comment, use
ALT + x comment-region
To un-comment, use
ALT + x uncomment-region
Credits: https://www.reddit.com/r/emacs/comments/1kklgl/command_to_uncomment_entire_comment_block/

How to force Org-mode to open a link in another frame?

In Org-mode it is possible to have links and top open links. As listed by http://orgmode.org/orgcard.txt in Org-mode C-u C-c C-o or mouse-3 forces links to open in another window. How can I do the corresponding for frames, that is, how can I force a link to open in another frame?
What I want is for C-c C-o to work as per default but C-u C-c C-o to force the link to be opened in another frame.
(For the distinction of windows and frames see http://www.gnu.org/software/emacs/manual/html_node/emacs/Frames.html.)
I am running Org-mode 7.6 in 23.3.1.
I just tested and you can get it to work by wrapping org-open-at-point in a (let ) as a custom function.
In this case I'm just prefixing the current org-link-frame-setup with your desired find-file-other-frame to ensure that if you use the command on another link type it will not fail.
(defun zin/org-open-other-frame ()
"Jump to bookmark in another frame. See `bookmark-jump' for more."
(interactive)
(let ((org-link-frame-setup (acons 'file 'find-file-other-frame org-link-frame-setup)))
(org-open-at-point)))
I suspect you will need to bind it to a key sequence other than C-u C-c C-o, unless Emacs will permit you to bind it to that sequence specifically.
Have a look at the variable org-link-frame-setup (M-x customize-variable RET org-link-frame-setup). The docstring should explain the approach.
I just tested this in a non-customized emacs:
emacs -q
GNU Emacs 24.0.92.1 (i386-mingw-nt5.1.2600) of 2011-11-30 on MARVIN
Org-mode version 7.7
When running C-c C-o and C-u C-c C-o on a link similar to the following:
file:~/Dropbox/org/test.org::*Test
I end up with a new frame being opened in both cases. C-u C-u C-c C-o opens the test.org file in my active emacsclient session. When changing the link to .../org/test.txt it still opens in a new frame, however it is unable to create the new file (I'm assuming I don't have .txt properly associated on my end, which is possible).
Testing with [mailto:test#test.org][test-mail]] also results in new frames by default.
Edit: The value for org-link-frame-setup is as follows (it is also the default since it is from emacs -q):
Its value is ((vm . vm-visit-folder-other-frame)
(gnus . org-gnus-no-new-news)
(file . find-file-other-window)
(wl . wl-other-frame))

How can I reload .emacs after changing it?

How can I get Emacs to reload all my definitions that I have updated in .emacs without restarting Emacs?
You can use the command load-file (M-x load-file, and then press Return twice to accept the default filename, which is the current file being edited).
You can also just move the point to the end of any sexp and press C-x, C-e to execute just that sexp. Usually it's not necessary to reload the whole file if you're just changing a line or two.
There is the very convenient
M-x eval-buffer
It immediately evaluates all code in the buffer. It's the quickest method if your .emacs file is idempotent.
You can usually just re-evaluate the changed region. Mark the region of ~/.emacs that you've changed, and then use M-x eval-region RET. This is often safer than re-evaluating the entire file since it's easy to write a .emacs file that doesn't work quite right after being loaded twice.
If you've got your .emacs file open in the currently active buffer:
M-x eval-buffer
Solution
M-: (load user-init-file)
Notes
you type it in Eval: prompt (including the parentheses)
user-init-file is a variable holding the ~/.emacs value (pointing to the configuration file path) by default
(load) is shorter, older, and non-interactive version of (load-file); it is not an Emacs command (to be typed in M-x), but a mere Elisp function
Conclusion
M-: > M-x
M-x load-file
~/.emacs
Others already answered your question as stated, but I find that I usually want to execute the lines that I just wrote.
For that, Ctrl + Alt + X in the Elisp part works just fine.
The following should do it...
M-x load-file
I suggest that you don't do this, initially. Instead, start a new Emacs session and test whatever changes you made to see if they work correctly. The reason to do it this way is to avoid leaving you in a state where you have an inoperable .emacs file, which fails to load or fails to load cleanly. If you do all of your editing in the original session, and all of your testing in a new session, you'll always have something reliable to comment out offending code.
When you are finally happy with your changes, then go ahead and use one of the other answers to reload. My personal preference is to eval just the section you've added/changed, and to do that just highlight the region of added/changed code and call M-x eval-region. Doing that minimizes the code that's evaluated, minimizing any unintentional side-effects, as luapyad points out.
Keyboard shortcut:
(defun reload-init-file ()
(interactive)
(load-file user-init-file))
(global-set-key (kbd "C-c C-l") 'reload-init-file) ; Reload .emacs file
C-x C-e ;; current line
M-x eval-region ;; region
M-x eval-buffer ;; whole buffer
M-x load-file ~/.emacs.d/init.el
Define it in your init file and call by M-x reload-user-init-file
(defun reload-user-init-file()
(interactive)
(load-file user-init-file))
I'm currently on Ubuntu 15.04 (Vivid Vervet); I like to define a key for this.
[M-insert] translates to Alt + Ins on my keyboard.
Put this in your .emacs file:
(global-set-key [M-insert] '(lambda() (interactive) (load-file "~/.emacs")))
Besides commands like M-x eval-buffer or M-x load-file, you can restart a fresh Emacs instance from the command line:
emacs -q --load "init.el"
Usage example: Company backends in GNU Emacs
Here is a quick and easy way to quick test your config. You can also use C-x C-e at the end of specific lisp to execute certain function individually.
C-x C-e runs the command eval-last-sexp (found in global-map), which
is an interactive compiled Lisp function.
It is bound to C-x C-e.
(eval-last-sexp EVAL-LAST-SEXP-ARG-INTERNAL)
Evaluate sexp before point; print value in the echo area.
Interactively, with prefix argument, print output into current buffer.
Normally, this function truncates long output according to the value
of the variables ‘eval-expression-print-length’ and
‘eval-expression-print-level’. With a prefix argument of zero,
however, there is no such truncation. Such a prefix argument also
causes integers to be printed in several additional formats (octal,
hexadecimal, and character).
If ‘eval-expression-debug-on-error’ is non-nil, which is the default,
this command arranges for all errors to enter the debugger.
Although M-x eval-buffer will work, you may run into problems with toggles and other similar things. A better approach might be to "mark" or highlight what’s new in your .emacs file (or even scratch buffer if you're just messing around) and then M-x eval-region.
You can set a key binding for Emacs like this:
;; Reload Emacs configuration
(defun reload-init-file ()
(interactive)
(load-file "~/.emacs"))
(global-set-key (kbd "C-c r") 'reload-init-file)
If you happen to have a shell opened inside Emacs, you can also do:
. ~/.emacs
It may save a few key strokes.

Emacs copy with regex

I have a text file. Can Emacs select text based on regex and put it in kill-ring, so I can copy it somewhere else? Something like regex-kill-ring-save?
inspired by the already given comments (the Charles answer doesn't work as I would want it), I added a new function to the isearch/isearch-regexp mode map which puts only the matching string into the kill ring (whereas Charles proposal kills from current point to end of matching string):
(defun hack-isearch-kill ()
"Push current matching string into kill ring."
(interactive)
(kill-new (buffer-substring (point) isearch-other-end))
(isearch-done))
(define-key isearch-mode-map (kbd "M-w") 'hack-isearch-kill)
The nice thing about the isearch/isearch-regexp approach (which you can enable with C-s and C-M-s respectively) is that you can see your search string growing and you can copy it with M-w as soon as you are satisfied (and go back to where you have been before with C-u C-Space).
This works for me with Emacs 23.1. Don't know if it will work in all situations. Anyway I hope you find it useful :)
UPDATE: going through the emacswiki I stumbled over KillISearchMatch which suggests more or less the same (plus some more tips ...).
Cheers,
Daniel
I'm not sure if there is such a function already, but what you can do it with a keyboard macro:
Start recording a kbd macro: C-x (
Search for your regexp with search-forward-regexp
Move to the beginning of your match (the text you want to kill) with the various emacs navigation commands, e.g. search or backward-word etc.
Mark: C-spc
Move to the end of your match
Kill the text: C-w
You can then name the keyboard macro with M-x name-last-kbd-macro so that you can execute the macro with a name rather than with C-x e.
If you want to save the macro for future sessions, you can open your .emacs and insert the macro into the buffer with M-x insert-kbd-macro. After than you can bind a key to the macro just like you bind keys to normal emacs functions, e.g. (global-set-key "\C-c m" 'funky-macro-macro).
More about emacs keyboard macros
Isearch+ does this already. It optionally sets the region around the search target. You can use C-SPC C-SPC or M-= C-SPC at any time during Isearch to toggle this.
isearchp-deactivate-region-flag is a variable defined in isearch+.el.
Its value is t
Documentation:
Non-nil means isearching deactivates the region.
See also option isearchp-restrict-to-region-flag.
You can toggle this option using M-= C-SPC during Isearch.
You can customize this variable.

Unable to clear a beginning of a line by C-u in Emacs

I run the following command in Emacs unsuccessfully
C-u
How can you clear the beginning of a line in Emacs?
You can also accomplish this by using a prefix arg for kill-ine (usually C-k). From the Emacs help (C-h k C-k):
With zero argument, kills the text before point on the current line.
So, you can do
C-u 0 C-k
or even better
C-0 C-k
You can set the mark, go to the beginning, kill till mark:
C-Spc C-a C-w
Try this:
M-0 C-k
Delete from beginning of line to point
C-u works in Bash "Emacs Mode", but not actually in Emacs. Here's what I usually do:
C-a C-k
But this is really only good if you want to kill the whole line. Svante's advice will clear from the beginning of the line to where your cursor was, as you asked for.
I don't know, where you read about C-u, but it is bound to the universal argument in Emacs.
If you want to kill the whole line, call kill-whole-line which is bound C-S-backspace. No matter at what column is the cursor, it will kill the whole line from beginning to end.
I have a small function bound to a key-chord:
(defun kill-start-of-line ()
"kill from point to start of line"
(interactive)
(kill-line 0))
(define-key global-map "\M-#" 'kill-start-of-line)
M-# is usually Alt-Shift-3, not a new DotNet language
I'm sure I saw this somewhere else, but didn't save the original reference.