How to replace spaces with A[ in emacs - regex

I have an input file like this
line 1
line 2
line 3
There are four spaces before all the lines I want to replace.
I want the end result to be:
A[line 1
A[line 2
A[line 3
(Its funny, so editor doesn't want to display line by line)
I tried M-x replace-regex ^[]+ -> A\[, but get error invalid regex "Unmatched [ or [^"
I tried M-x replace-regex ^[]+ -> A[, but get same error.
Replacing the [ is a problem. How to fix this?

Try
M-x query-replace-regex RET ^ RET A[ RET
^^^
notice the 4 spaces here
In your examples you are missing a space inside the character class: ^[ ]+
Prettier would probably be: ^[[:space:]]+ for every kind of white-space.

Rectangles give you another way to deal with these issues. Cua-mode provides an even nicer system. To use it, first add the following to your .emacs:
(setq cua-enable-cua-keys nil)
(cua-mode)
Then, with point at the * in the following:
* line 1
line 2
line 3
Hit C-enter, and use the arrow keys (or C-n and C-f) to move point to:
line 1
line 2
*line 3
The blanks will all be highlighted. Type A[ to insert the characters. Then hit C-w to delete the spaces.

M-x query-replace-regex RET ^ + RET A[ RET
as it's a single kind of characters to replace, brackets or class are not needed

Related

Remove Multiple Periods Up To Bracket From String

Would like to know how to create an Emacs macro that will
Find the first instance of multiple periods in string
Set mark
Move to the first closed bracket in string
Remove all chars between mark and closed bracket
Here is an example string. I'd like to go from this:
* [This is Chapter 1.......................................................... 1-83](chapter1.md)
To this:
* [This is Chapter 1](chapter1.md)
Can anyone assist?
Thanks
Heres the hacky way I accomplished. I'm sure there is a cleaner way.
Start with cursor at the beg of line
M-x start-kbd-macro
C-s RET .. to search for first instance of ".." in the string
C-SPACE to set mark
C-s ] to search for first instance of "]" in the string
DEL to remove everything marked
BKSP BKSP to remove the final two ".."
DWN ARROW to get to next line
C-a to get to beg of line
M-x end-kbd-macro
I know its lame, but it worked!! I have ~100 pages of docs to do this to! Need to figure out how to reliably perform this on the entire doc next.

Is it possible to write comments in Xtend-templates?

Is it possible to write comments inside an Xtend template? (for example in order to quickly comment out an IF-statement or anything)
Yes, that's possible. Use the toggle-comment action in Eclipse or type the prefix ««« manually, e.g as in ««« my comment in a template
You can use ««« for single line comments like Sebastian Zarnekow mentioned.
A drawback of this commenting style is that it also comments out the newline character at the end of this line. Sometimes that's exactly what you want, but sometimes it's not.
For example: The following code snippet ...
val x = '''
line 1
line 2 ««« my comment
line 3
line 4
'''
println(x)
... will print following output ...
line 1
line 2 line 3
line 4
Another way to comment is as if you would insert an expression and inside the expression («») you use a plain old java comment. : «/* comment */»
That way you can go on with you template in the same line, span multiple rows and you avoid the deleted newline character.
PS: You can insert the guillemets this way:
« with ALT holding down and then 1 7 4 on the num block
» with ALT holding down and then 1 7 5 on the num block
or you map a good key combination to the two chars in your IDE, e.g. CTRL+< and CTRL+>
comment in xtend-templates: 【Ctrl + /】

add character before first word in line

I want to add a minus sign "-" infront of the first word in a line on the editor VIM. The lines contains spaces for indentation. The indentation shall not be touched. E.g
As Is
list point 1
sub list point 2
and so on...
I want
- list point 1
- sub list point 2
- and so on...
I can find the first word, but i struggle with replacing it in the correct way.
^\s*\w
in Vim
/^\s*\w
But in the replacement I always remove the complete found part....
:s/^\s*\w/- \w/
Which leads to
- ist point 1
- ub list point 2
- nd so on...
Use & which is replaced with the matched string:
:%s/\w/- &
I'm late to the party but:
:%norm! I- <CR>
And another one with :s:
:%s/^\s*/&- /
An alternative to falsetrue's answer: You can capture the first word character and print it out along with the leading -:
%s/\(\w\)/- \1/
:normal cmd may help too:
:%norm! wi-
note that after - there is a space.

Math in Vim search-and-replace

I have a file with times (minutes and seconds), which looks approximately as follows:
02:53 rest of line 1...
03:10 rest of line 2...
05:34 rest of line 3...
05:35 rest of line 4...
10:02 rest of line 5...
...
I would like to replace the time by its equivalent in seconds. Ideally, I would like to run some magical command like this:
:%s/^\(\d\d\):\(\d\d\) \(.*\)/(=\1*60 + \2) \3/g
...where the (=\1*60 + \2) is the magical part. I know I can insert results of evaluation with the special register =, but is there a way to do this in the subst part of a regex?
Something like this?
:%s/^\(\d\d\):\(\d\d\)/\=submatch(1)*60+submatch(2)/
When the replacement starts with a \= the replacment is interpreted as an expression.
:h sub-replace-expression is copied below
Substitute with an expression *sub-replace-expression*
*sub-replace-\=*
When the substitute string starts with "\=" the remainder is interpreted as an
expression. This does not work recursively: a substitute() function inside
the expression cannot use "\=" for the substitute string.
The special meaning for characters as mentioned at |sub-replace-special| does
not apply except for "<CR>", "\<CR>" and "\\". Thus in the result of the
expression you need to use two backslashes to get one, put a backslash before a
<CR> you want to insert, and use a <CR> without a backslash where you want to
break the line.
For convenience a <NL> character is also used as a line break. Prepend a
backslash to get a real <NL> character (which will be a NUL in the file).
When the result is a |List| then the items are joined with separating line
breaks. Thus each item becomes a line, except that they can contain line
breaks themselves.
The whole matched text can be accessed with "submatch(0)". The text matched
with the first pair of () with "submatch(1)". Likewise for further
sub-matches in ().
Use submatch() to refer to a grouped part in the substitution place:
:%s/\v^(\d{2}):(\d{2})>/\=submatch(1) * 60 + submatch(2)/
With your example yields:
173 rest of line 1...
190 rest of line 2...
334 rest of line 3...
335 rest of line 4...
602 rest of line 5...
Hopefully it would be helpful to someone else, but i had similar problem where i wanted to replace "id" with a different number, in-fact any other number
{"id":1,"first_name":"Ruperto","last_name":"Bonifayipio","gender":"Male","ssn":"318-69-4987"},
used expression
%s/\v(\d+),/\=submatch(1)*1111/g
which results into following new value
{"id":1111,"first_name":"Ruperto","last_name":"Bonifayipio","gender":"Male","ssn":"318-69-4987"},

Remove the first character of each line and append using Vim

I have a data file as follows.
1,14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065
1,13.2,1.78,2.14,11.2,100,2.65,2.76,.26,1.28,4.38,1.05,3.4,1050
1,13.16,2.36,2.67,18.6,101,2.8,3.24,.3,2.81,5.68,1.03,3.17,1185
1,14.37,1.95,2.5,16.8,113,3.85,3.49,.24,2.18,7.8,.86,3.45,1480
1,13.24,2.59,2.87,21,118,2.8,2.69,.39,1.82,4.32,1.04,2.93,735
Using vim, I want to reomve the 1's from each of the lines and append them to the end. The resultant file would look like this:
14.23,1.71,2.43,15.6,127,2.8,3.06,.28,2.29,5.64,1.04,3.92,1065,1
13.2,1.78,2.14,11.2,100,2.65,2.76,.26,1.28,4.38,1.05,3.4,1050,1
13.16,2.36,2.67,18.6,101,2.8,3.24,.3,2.81,5.68,1.03,3.17,1185,1
14.37,1.95,2.5,16.8,113,3.85,3.49,.24,2.18,7.8,.86,3.45,1480,1
13.24,2.59,2.87,21,118,2.8,2.69,.39,1.82,4.32,1.04,2.93,735,1
I was looking for an elegant way to do this.
Actually I tried it like
:%s/$/,/g
And then
:%s/$/^./g
But I could not make it to work.
EDIT : Well, actually I made one mistake in my question. In the data-file, the first character is not always 1, they are mixture of 1, 2 and 3. So, from all the answers from this questions, I came up with the solution --
:%s/^\([1-3]\),\(.*\)/\2,\1/g
and it is working now.
A regular expression that doesn't care which number, its digits, or separator you've used. That is, this would work for lines that have both 1 as their first number, or 114:
:%s/\([0-9]*\)\(.\)\(.*\)/\3\2\1/
Explanation:
:%s// - Substitute every line (%)
\(<something>\) - Extract and store to \n
[0-9]* - A number 0 or more times
. - Every char, in this case,
.* - Every char 0 or more times
\3\2\1 - Replace what is captured with \(\)
So: Cut up 1 , <the rest> to \1, \2 and \3 respectively, and reorder them.
This
:%s/^1,//
:%s/$/,1/
could be somewhat simpler to understand.
:%s/^1,\(.*\)/\1,1/
This will do the replacement on each line in the file. The \1 replaces everything captured by the (.*)
:%s/1,\(.*$\)/\1,1/gc
.........................
You could also solve this one using a macro. First, think about how to delete the 1, from the start of a line and append it to the end:
0 go the the start of the line
df, delete everything to and including the first ,
A,<ESC> append a comma to the end of the line
p paste the thing you deleted with df,
x delete the trailing comma
So, to sum it up, the following will convert a single line:
0df,A,<ESC>px
Now if you'd like to apply this set of modifications to all the lines, you will first need to record them:
qj start recording into the 'j' register
0df,A,<ESC>px convert a single line
j go to the next line
q stop recording
Finally, you can execute the macro anytime you want using #j, or convert your entire file with 99#j (using a higher number than 99 if you have more than 99 lines).
Here's the complete version:
qj0df,A,<ESC>pxjq99#j
This one might be easier to understand than the other solutions if you're not used to regular expressions!