ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi,
I need to edit some HTML pages and i want to create a macro in vim to make that. But i cant make properly the macro. Im lost in how to make the commands and make the macro work.
What I need is to:
(1) Locate and erase the "<";
(2) advanced one word;
(3) put "(" ;
(4) go to the next ">" and substitute this for ")";
(5) go to the next line
The commands i have seen that i can use are:
:s/<// "search for "<" and erase him
w "go to the next word
press i "enter in edition mode
insert "(" "insert the "("
press ESC " exit edition mode
:s/>/)/ " go to the next ">" and substitute this for an ")"
j "go to the next line
I have tried to just start the macro and put those commands but i receive the error " E465: :winsize requires two number arguments" and cant figure out what is happening.
I need too erase all the things that are between the "</ >", including the "</ >".
I was seeing that i can use vim scripts to make an if condition but i dont know either how to make and run the script.
you need to solve it without using insert mode, try to solve 1-2-3 with one single: s/<(wordspace)/$1(/
but obviously you need to fine tune it
There is a place where I can find some examples and what each command does? I have seeing a lot of sites that try to explain, some say about pressing " i " and " Esc " while you are recording but any of them appear to not solve my problem. Others use expressions like: 03wdei^R=2012-^R"^M^[0j that appear to use the " i " and " Esc " (^[). But, anyway, i cant properly run my macro :/
again, forget that i and Esc, try to solve it using a regexp
word is \w\w* space is \s, so this is the correct command for 1-2-3:
:s/<\(\w\w*\s\)/\1(/
you can obviously add replacing > to ) too, and you will have s single command to process a line
again, forget that i and Esc, try to solve it using a regexp
word is \w\w* space is \s, so this is the correct command for 1-2-3:
:s/<\(\w\w*\s\)/\1(/
you can obviously add replacing > to ) too, and you will have s single command to process a line
Code:
:s/<\(\w\w*\s\)\(.*\)>/\1(\2)/
There is a place where i can find more info? Im used to with some common commands of VIM but this next step to macros appear to put a lot of new stuffs and a new pattern of thinking.
This command you put appear to work but i cant understand it properly and it erase the final of the line, so the formatting are getting messy.
As i could understand:
:s/ - search
\( - escape character and new command ?
\w - escape character, go to next word ?
\w* - escape character, all the worlds?
\s\) - escape character search ?
\( and \) are grouping, the first one is group 1, the second is group 2.
\w is a word character (mor or less: a-z)
\s is a space character (space or tab).
\1 and \2 are backreferences to the groups in the search expression
* means any number of \w (or whatever located just before *)
it is more or less identical to sed, so you need to check how sed works.
\( and \) are grouping, the first one is group 1, the second is group 2.
\w is a word character (mor or less: a-z)
\s is a space character (space or tab).
\1 and \2 are backreferences to the groups in the search expression
* means any number of \w (or whatever located just before *)
it is more or less identical to sed, so you need to check how sed works.
Those examples I have understood: its like the same as another programming languages. What i'm not is how macros in vim work, more exactly how i need to think to make them.
For the first time I'm reading about sed, what appear to be the basic of how the macros work in vim - as i understood.
Ok, but the way to think to make sed( or macro in vim) work appear to be different from what I'm used to.
In the case of the command :s/, i read in the Gnu manual for sed:
:s/regexp/replacement/flags.
In the code you sent me:
:s
/Regexp: <\(\w\w*\s\)\(.*\)>
/Replacement: \1(\2)
/Flags:
I cant understand yet the syntax or how it works, how to read this.
In the Regexp I'm reading something like : expression "<", the first group ( one word, all the words - why? in thesis I just need to search the ">" - , and space (?) ) and the second group (match everything), expression ">".
In the Replacement: Use first group - Why the "(" ?, isnt for groups? - the second group?
??? How the "(" and ")" are being put?
I have tried to make the macro, after the end, jump to the next line. But i have no success at all
I don't understand what you are talking about. :s in vi works like sed. Both understand [almost] the same syntax. So
Code:
: go to command mode
s/ substitute command
< first letter is a literal < in the search expression
() is a group, but need to be escaped, therefore \( is the beginning of the [first] group
\w a single word char
\w* zero or more of \w
\s whitespace character
\) closing ) of the first group
\(.*\) second group, contains anything ( until > )
> a literal >
/ replacement separator (= delete what was found and use this instead)
\1 put first group
( put a (
\2 put second group
) put a )
/ end of replacement string
The macro you trying to write is just a key sequence you want to repeat.
so you will type:
Code:
#to set macro "a"
qa:s/<\(\w\w*\s\)\(.*\)>/\1(\2)/q
# to call macro "a"
@a
I don't understand what you are talking about. :s in vi works like sed. Both understand [almost] the same syntax. So
Code:
: go to command mode
s/ substitute command
< first letter is a literal < in the search expression
() is a group, but need to be escaped, therefore \( is the beginning of the [first] group
\w a single word char
\w* zero or more of \w
\s whitespace character
\) closing ) of the first group
\(.*\) second group, contains anything ( until > )
> a literal >
/ replacement separator (= delete what was found and use this instead)
\1 put first group
( put a (
\2 put second group
) put a )
/ end of replacement string
The macro you trying to write is just a key sequence you want to repeat.
so you will type:
Code:
#to set macro "a"
qa:s/<\(\w\w*\s\)\(.*\)>/\1(\2)/q
# to call macro "a"
@a
Thanks for the help and sorry for something, English isn't my mother language and I read better than write in it.
I think I'm getting. I have write something weird: what i wasn't understanding was why you used the "\w" and "\w*" ( in the first group). I'm making some tests here and seeing that doesn't make any difference - apparently.
I'm not using vim now, I'm using the sed command like this site describe: https://unix.stackexchange.com/quest...tern-using-sed With vim macro the code wont go to the next line and with this i doesn't need to go the next line, the command does all I need.
But I'm not getting how to delete all the "</word>". I write this code:
Code:
sed -i 's/<\/\(\w\)\>//' index.html
And this too: sed -i 's/<\/\(\w\)>//' index.html
As I have understood, I'm referencing to the "</" with a word inside and that ends with ">". Like the "</div>".
But wont work as I was expecting: the code only delete the codes with "</word" and not those "</word>" - I'm running manually for each type, because the code below work:
I would go with a script here, really, not a macro, since some tags are not terminated with a </tag>, if they all were then a macro would be relatively easy,
it would be something like (I'm writing down a key sequence now, convert text in brackets to keys):
Code:
0
f<
w
"tyiw
w
"gyiw
/\/[control+r]t[enter]
dd
[control+o]
F<
x
/[control+r]g[enter]
i([escape]
f>
r)
Which you could record as a macro and would do those cases, otherwise you need a lookup for the edge cases, etc.
It's messy.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.