Emacs: move any "//" pattern to specific column - emacs

I used to start all my comments from column 60.
Is there a trick in Emacs with which for the current line, anything after (and including) // is pushed to column 60?
Example
cmp A, B // comparing against a reference
becomes
cmp A, B <-- extra spaces added until column 60 ---> // comparing against a reference

Set comment-column to 60, and hit M-;.
Note that M-; runs comment-dwim which tries to do what you mean. Notably, if the region is active, it will comment out the entire region. If this gets in your way, the command that does what you ask for above is comment-indent, which you could bind to a suitable key.

Set the comment-column as said by legoscia, select a region and call M-x align-regexp RET // RET. That will align your comments on the same column.
For the doc: C-h f align-regexp.

Related

How to read ranges in the syntax table of Emacs?

E.g. Below syntax table is from text-mode (invoked by C-h s).
The parent syntax table is:
C-# .. C-h . which means: punctuation
TAB .. C-j which means: whitespace
C-k . which means: punctuation
C-l .. RET which means: whitespace
C-n .. C-_ . which means: punctuation
By intuition I guess the .. means a list range. However I could not find confirmation in the GNU emacs manual. May I confirm about it here?
In addition, may I also confirm that the range is sorted by the ASCII code number? i.e. C-# .. C-h corresponds to ASCII code 0-10.
My apology if this looks too basic. I wish I could find related document from the reference manual but it is not
I can't definitively confirm this, but based on observation I agree 100% with your assessment.
The instances of A .. Z and a .. z and 0 .. 9 make for good supporting evidence.
Clearly the control characters are being rendered in a readable format, and sometimes the selected option isn't the ideal choice.
e.g. C-l .. RET would be clearer as the equivalent C-l .. C-m; and similarly if TAB .. C-j appeared as C-i .. C-j
The section on syntax table internals points out that they are implemented as char tables, which are "much like a vector [but] indexed by character codes". As such, syntax tables are based upon sequences of character codes, and the code which displays them undoubtedly iterates through the table, ascertaining the start and finish codes for every range of characters with an identical syntax, and then using the .. format to display each range.

Putting # at the start of every line in a selected block, and removing

I am typing some code using emacs ,for example:
pi = 3.14
radius = 5
area = pi*radius**2
print area
and I want to comment out all 4 lines like so;
#pi = 3.14
#radius = 5
#area = pi*radius**2
#print area
Someone suggested to select the block, then ctrl+x r t # enter , but this is what I got;
#i = 3.14
#adius = 5
#rea = pi*radius**2
#rint area
It has replaced the first entry with a # . This would be fine if it could be reversed but the reversal method crtl+x r k just deletes everything.
I found something that works though which is a bit long drawn out. First select the position of the first # and type ctrl+x ( crtl+a # ctrl+n ctrl+x ). This will have put a # at the start of the first line. If you want to repeat this for Z number of lines now type crtl+u Z crt+x e. This will place a # at the start of the following Z lines.
Is there an easier way?
The C-x C-r t trick replaces the text in the rectangle bounded by point and mark, so this would actually work if you select from the first column of the first row, and then put point on the first column of the last row, i.e.:
<mark>pi = 3.14
radius = 5
area = pi*radius**2
<point>print area
Then it will "replace" the empty string at the beginning of each line with #.
Another way to do this is to mark the block you want to comment out and hit M-; (or M-x comment-region). It's supposed to do the right thing for the programming language you're currently using.
As noted by lawlist in the comments, multiple-cursors is a rather addictive tool that can be useful in situations like this.
Do not use C-x C-r t here. Use one of these instead:
comment-region (I bind it to C-x C-;)
comment-dwim (M-;)
string-insert-rectangle -- if you have Emacs 24.4 or later (or a recent dev snapshot)

How to list out previous command arguments input to minibuffer in Emacs?

Often I need to do replacement with text. I am looking for a way to avoid repeatedly input replacement text.
For example, firstly, I replaced a with b in text;
Secondly, I replaced c with d.
Thirdly, I need to replace a with b again. However, Emacs only store last replacement as default argument.
What is the way to list previous replacement argument, i.e. a to b?
The responses you give to M-% (and other commands that get input from the minibuffer) are kept in the history. Hit the "Up" key to see them.
As the search texts and the replacement texts are kept in the same history, in your case above the history would be a, b, c, d. So, when prompted for the search text, you'd need to hit "Up" four times to come back to a. The history would then change to a, b, c, d, a, so to get back to b as replacement text you'd again need to hit "Up" four times.
You can use M-p instead of "Up" if you prefer.
If you want to see what the previous minibuffer values were without invoking another command you can directly inspect the minibuffer-history variable: C-h v minibuffer-history. This will list all of the values together with the description of this variable.

Deleting the first 'x' characters from every line in a region with Emacs

I want to delete the first x characters for every line in a region.
Is there any key binding available to do this without using regex?
The best way to do this is to use the "rectangle" family of commands. For example, mark the beginning of the region. Go to the end of the region and place the point at column X. Run the command kill-rectangle using C-x r k.
Of course, this is not limited to deleting characters at the beginning of lines.
If the mark is on column 0, put the point on column x and use kill-rectange:
C-x r k runs the command kill-rectangle, which is an interactive
autoloaded Lisp function in `rect.el'.
It is bound to C-x r k.
(kill-rectangle START END &optional FILL)
Delete the region-rectangle and save it as the last killed one.
When called from a program the rectangle's corners are START and END.
You might prefer to use `delete-extract-rectangle' from a program.
One command that I really love for these types of jobs is multiple cursor's edit lines:
http://www.youtube.com/watch?v=jNa3axo40qM
It is overkill compared to kill-rectangle (the best solution to the original problem) but it is an amazing tool in the toolbox. Definitely worth taking a look at it.
Select the required rectangle using rectangle command
M-x rectangle-mark-mode
Then use command
M-x kill-region

Swapping 2 columns with Emacs

I have 2 columns, separated by comma. How can I swap those columns with Emacs?
I have the following:
column 1,column2
x1,x2
y1,y2
f1,f2
and I want it like this:
column2,column 1
x2,x1
y2,y1
f2,f1
Use M-x query-replace-regexp and then:
\(.+\),\(.+\)
as replace regexp and
\2,\1
for replacement.
In Emacs, you need to escape grouping parentheses with \. So, above regexp would be usually written as
(.+),(.+)
which means that you want everything before comma in first group and everything after comma in second group.
\2,\1
means: write second group, then comma, then first group.
While you can apply techniques given by other people, you can also use the org-mode tables.
Once you convert the data into org-mode table, it is very easy to swap the columns by simple keystrokes. You can have M-x org-mode, select the region then do M-x org-table-convert-region, and then M- on the right most column. I am not sure, how to export the data as CSV, but that should be very easy for you with replace-regexp. This can be helpful: http://www.gnu.org/software/emacs/manual/html_node/org/Tables.html#Tables
Similar to the answer given by #darioo, type the following into the top of your buffer:
(query-replace-regexp "\\(.*?\\),\\(.*\\)" "\\2,\\1")
Then, put your cursor at the end of this line and press ctrl-x, ctrl-e.
You will have an interactive search-and-replace for which you press the space bar to make the change, and press ctrl-g to quit. If you press ! (exclamation mark) then the search will cease being interactive and take place on all matching text.
If you want to reverse the changes then press M-x (usually ESC followed by x) and type undo and press enter.
Emacs has a rectangular selection mode, see for example: http://emacs-fu.blogspot.com/2008/12/working-with-rectangular-selections.html
Even better, if you enable cua-mode, entering Ctrl-Enter will put you in rectangle selection mode that is very easy to use.
http://trey-jackson.blogspot.com/2008/10/emacs-tip-26-cua-mode-specifically.html
Use a macro !
Go to the first line of the buffer
Start recording a macro (F3)
Move to the beginning of the line (^a)
Search for comma (^s ,)
Transpose (M-t)
Move cursor down one line
Stop recording macro (F4)
Select the rest of the lines and:
M-x apply-macro-to-region-lines
UPDATE: This doesn't work properly if you have multiple words in a column. Looking for a more general solution...