Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prepend # at the head of multiple lines with emacs?

Tags:

emacs

I need to comment out some blocks of code in Python. How can I do that with emacs? How can I prepend # character at the start of each line of a block?

like image 343
prosseek Avatar asked Aug 19 '10 21:08

prosseek


People also ask

How do you use prepend and append?

The prepend() method inserts specified content at the beginning of the selected elements. Tip: To insert content at the end of the selected elements, use the append() method.

How do you prepend in Python?

To prepend to a list in Python, use the list. insert() method with index set to 0, which adds an element at the beginning of the list.

How do I append my first child?

To insert element as a first child using jQuery, use the prepend() method. The prepend( content ) method prepends content to the inside of every matched element.

What is prepend in Java?

To prepend an element to a linked list is to insert that element in front of the first element of the list. The prepended list element becomes the new head of the list. Program.


2 Answers

You can use the comment-region command using

M-x comment-region 

Edit: And as suggested by @Gilles in comment you can use M-; which according to the help is

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'.

which is probably easier on the long run. :-) Remember that this is "mode-dependant", so you need to set python-mode before you comment using M-x python-mode

Or if you want to prefix every line with any type of character, select the text you want to comment and type

C-x r t 

and type the character you want to prefix with. Remember that the caret must be on the first column on the last line that you select, or your text would be replaced.

You select text by pressing C-space and moving your caret around btw.

like image 111
Patrick Avatar answered Oct 07 '22 03:10

Patrick


Here's a link that describes how to do this with arbitrary characters in rectangle mode.

This is handy because it only does this for the selected region of text.

like image 32
yurisich Avatar answered Oct 07 '22 02:10

yurisich