Search for multiline in an xml file with emacs
m-x list-matching-lines
<step>D</step>
<alter>0</alter>
<octave>5</octave>
It lists all occurances in the occur window.
But if I search a multi partline it fails.
m-x list-matching-lines
<step*
<alter*
<octave*
Searched 1 buffer; no matches for `<step*^J <alter*^J <octave*'
The search only with
m-x list-matching-lines
<step*
works also perfekt.
The outcome with above command looks like:
177: <stem default-y="-57">down</stem>
182: <step>A</step>
190: <stem default-y="-60">down</stem>
200: <step>D</step>
208: <stem>down</stem>
216: <step>D</step>
224: <stem>down</stem>
I appreciate some help, thanks.
<step* will match <ste and <step and <steppppppppp etc. Nothing else. It's a regular expression, not a shell glob pattern. So it certainly isn't going to match <step>D</step>.
Use .* to match zero or more non-newline characters; not just *
Read about regular expressions in the manual:
C-hig (emacs) Regexps RET
Found something in the green.
m-x list-matching-lines
<step...*
+ C-qC-jRET will bring the searched line plus the next one in the *Occur* window.
and
m-x list-matching-lines
<step...*
...*
+ C-qC-jRET brings the desired two lines etc.
Related
The right regex I copied:
\(.\)
What I pasted after M-x isearch-forward-regexp:
\\(\.\\)
Is there a way to solve that?
isearch-yank-pop causes the yanked text to be regexp-quoted if a regexp search is in progress. The assumption is that whatever you are yanking is text to find verbatim, and not part of a regexp.
The solution is to edit the search pattern (M-e), and then yank the text into the minibuffer (C-y).
What #phils said. (Yanking in vanilla Isearch during regexp search applies regexp-quote.)
If you use library Isearch+ then:
Whether that automatic regexp-quote-ing is done or not is controlled by option isearchp-regexp-quote-yank-flag.
You can toggle that option value anytime during isearch using C-` (command isearchp-toggle-regexp-quote-yank).
Is there a module to do multiline search in Emacs ?
I have used grep, pt and now ag, and they are great. But sometimes when exploring a codebase you search for things that are on multiple lines (and therefore get nothing), but reducing the search to fewer words yields a lot of results.
Is there a way to get multiline search in Emacs in a whole project ?
Of course there is. There are multiple ways.
You don't say what kind of search you are trying to do: single file? multiple buffers? regexp? Do you need a fixed list of search hits or do you want incremental search?
Here's one simple answer:
In a Dired buffer, mark some files you want to search, then hit A.
Type a regexp to match. Use C-q C-j to enter a newline char to match. Hit RET to enter the regexp.
That searches through the marked files, stopping at each search hit in turn. Use M-, to go to the next hit, etc.
For example, to search for doc strings of variable definitions, search with this regexp or similar:
Search marked files (regexp): (defvar.*
.*"Non-nil
There's a newline char after the first .*, which you enter using C-q C-j.
There are lots of other ways to search in Emacs. The best place to start is the Search and Replace category of Emacs Wiki. There you can find ways to search broken down by main characteristics and described.
all
I am new to emacs, I have a simple problem, when searching, emacs provides search by word, but when replace, how can I just replace the extract word, not a substring? I have searched on the internet, someone said to add \bfoo\b to match foo only, won't match foos but it doesn't work. Thanks.
For string-based search/replace, can run query-replace-word by typing: C-u M-%.
If you want regular expression search, then, indeed, you can enclose your regexp in either \b which matches word boundaries, or even \< and \> for beginning and end of word respectively. Make sure to use M-x replace-regexp in that case, not just M-x query-replace.
In Emacs - is there a way I can search an extended command by regex right in the minibuffer? That is - I want to filter commands by regexp and then choose the one I need using IDO or Icicles.
When using ido you can turn on regexp matching by pressing C-t. I use smex and ido together and it works beautifully.
While they are not regexps, the default completion mechanism accepts a * to stand for "anything", so you can do M-x foo*bar ? and it will list all the commands whose name looks like "...foo...bar...".
I've always suspected that Icicles provdies that.
M-x <command-regexp> [PageUp/PageDown]
to browse the commands mathching regexp, and
M-x <command-regexp> [Shift-Tab]
to see the list of commands matching regexp. More here.
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...