How to list out previous command arguments input to minibuffer in Emacs? - 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.

Related

How can I confirm individual changes in a project-wide find and replace?

When I'm in project search (Shift + CMD + F) I can search the whole project for a specific word or expression. I can also enter a word I would like to replace it with.
If I click on one of the search results, it shows me the current version and the replacement version side by side. However, I can't find a way to confirm the replacement instance by instance! I only see a button to replace ALL instances, which is usually not what I want to do.
The reason is that certain words might be used in different context throughout the project, so usually not all search results shall be replaced.
How can I confirm the replacement instance by instance?
The replace one is on the each item that showing when mouse moved on each item.
As an alternative approach, you can press CMD + D repeatedly to select successive instances of a match, and CMD + SHIFT + D to skip any instance you do not want to include, and in this way case by case include each match in your find \ replace.
Also useful is CMD + U to undo the last selection action, in case you screw up during this process so you don't need to start over. Note, your shortcuts may be vary per operating system, but the procedure is the same.

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

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.

Switch to original buffer after chasing tags in Emacs

I use M-. to jump to definitions of class/functions. Sometimes there are multiple classes with the same tag, so I need to use C-u M-. to jump to multiple files, hence multiple buffers. Now my question is, how do I go back to the original buffer quickly? I know C-x b, but you need to type in the buffer name, or it just give you by default the last buffer you visited, is there anyway to go further? For example, go to the previous buffer of the last buffer?
I believe that M-. calls find-tag by default. You should be able to go back up the stack of locations with M-* (pop-tag-mark).
From C-h f find-tag:
A marker representing the point when this command is invoked is pushed
onto a ring and may be popped back to with M-*. Contrast this with the
ring of marks gone to by the command.
Icicles multi-command icicle-find-tag, bound to M-. in Icicle mode, combines all of what vanilla Emacs commands M-. (find-tag), M-, (tags-loop-continue), tags-apropos, and list-tags do. And it does more.
You can complete against any tags, cycle (in different orders) among a subset of tags matching an additional pattern, and so on, visiting multiple tags in a single command invocation. You choose the tags you want to visit, in any order --- you need not visit each one in sequence.
You first enter (using RET) a regexp that all tags you are interested in must match (it could be vacuous, to get all tags).
After that, you can type a pattern that a subset of the tags and or their source files must match.
That is, by default you can complete against multi-completion candidates that are composed of the tag itself and its source file name.
You can choose candidates to visit using C-mouse-2 in *Completions* or by cycling among their names using down and up and then using C-RET to visit.
You can return to your original location using M-* (icicle-pop-tag-mark). You can also return to it by just using C-g to finish your M-. invocation.
More information here.
I use winner-mode for this (and other similar situations).
Add (winner-mode 1) to your init file, and then when you wish to return to the window configuration that you were in before jumping to the tags, you just type:
C-c<left> to call winner-undo (repeating as many times as necessary)
If you had visited multiple tags in another buffer, this will get you back to your original buffer (or the previous buffer, at any rate) in a single step, rather than stepping back through the individual tags one by one.
If the tags have taken you through multiple buffers, then you'll need to type C-c<left> once for each buffer (or C-c<left>C-xzzz... if you'd gone on quite a long detour :)

How can I modify emacs' Search and Replace to perform a more complicated task?

total Emacs noob here. So right now I'm working on a fairly big LaTeX project in Emacs in which there are couple of places where I need to index some words, using the makeidx package. Because I also wanted indexed words to be bold, I created my own command \ind{} which would make the argument go bold and indexed. But right now I'm dissatisifed with this command so I'd like to change every instance of \ind{whatever} in my text by \textbf{whatever}\index{whatever by default}.
The thing is I know exactly what I want :
Go through the text, look for any instance of \ind{ and replace by \textbf{ using search-and-replace
Save the argument of \ind ("whatever" in this case) in memory
Ask me the user what should the argument of \index be. By default (by striking enter), it should be the first argument, but I can also change my mind and enter something different ("whatever by default" in this case). If there's no input (only a space " " for example) stop the program.
Write down \index{, the new argument and }.
Go to next occurance in the text.
But, alas!, I know not how to achieve this, so I need someone's help. If it should take too much time to explain how to do such a thing, would you please send me some tutorial about writing my own functions?
I hope I'm being clear, and thanks for your patience!
This approach seems vaguely unorthodox to me, but it works and seems sufficient for a one-off job...
In the replacement text for replace-regexp and query-replace-regexp (C-M-%), one newer escape sequence is \,(...), where ... can be any Lisp expression. There's a Lisp function read-from-minibuffer which reads arbitrary text typed by the user, with an optional default. Therefore:
C-M-%: Start query-replace-regexp.
\\ind{\([^}]+?\)}: The pattern to search for.
\\textbf{\1}\\index{\,(read-from-minibuffer "index content? " \1)}: The replacement text. The user will be prompted for the text to put in the braces following the \index{} element, using the original text between the braces following the \ind{} element as a default.
Note that when using query-replace-regexp, you'll have to confirm each choice by typing y after each. Use M-x replace-regexp if you want to avoid this step.
Vlad give you the LaTeX answer to your problem. An Emacs solution is the key-macro: start with
C-x (
to define a new macro, then do one step of your change, say:
C-s \ind{
<left>ex
Then copy and paste the argument in the \textbf macro... You have to be careful to move in a way that will be repeatable. Once the standard modification is done, you let the cursor after the whatever by default and end the definition by
C-x )
now C-x e will call the macro you just define, letting your cursor at the correct place to change the part you want to change You can also repeat the e to call the macro several time at once.
Why not just redefine the \ind so that it can get an optional argument?
For example:
\newcommand{\ind}[2][]{%
\def\first{#1}%
\ifx\first\empty
\textbf{#2}\index{#2}%
\else
\textbf{#2}\index{#1}%
\fi
}
This way you can use \ind{whatever} or \ind[whatever-else]{whatever}.

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