LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-24-2019, 08:12 AM   #1
Myphre
LQ Newbie
 
Registered: Oct 2018
Posts: 21

Rep: Reputation: Disabled
Question Vim Macro - Newbie here


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.


Example:
Original code:
Code:
  
        <a href="index.html" class="col-3">
            <div class="verticalCenter">
                <img id="logo" src="logo.webp">
            </div>
        </a>
What I need to make:

Code:
  
        a (href="index.html" class="col-3")
            div (class="verticalCenter")
                img (id="logo" src="logo.webp")
thanks for the help

Last edited by Myphre; 11-24-2019 at 08:14 AM.
 
Old 11-24-2019, 08:18 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,981

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
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
 
Old 11-24-2019, 08:25 AM   #3
Myphre
LQ Newbie
 
Registered: Oct 2018
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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 :/
 
Old 11-24-2019, 08:37 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,981

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
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)/
 
Old 11-24-2019, 09:37 AM   #5
Myphre
LQ Newbie
 
Registered: Oct 2018
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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 ?

Im reading now this site: http://vimregex.com/
 
Old 11-24-2019, 09:42 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,981

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
\( 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.
 
Old 11-24-2019, 11:41 AM   #7
Myphre
LQ Newbie
 
Registered: Oct 2018
Posts: 21

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
\( 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
 
Old 11-24-2019, 12:58 PM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,981

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
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
and that will just replay the key sequence as you specified.
https://vim.fandom.com/wiki/Macros

Last edited by pan64; 11-24-2019 at 01:08 PM.
 
1 members found this post helpful.
Old 11-24-2019, 07:22 PM   #9
Myphre
LQ Newbie
 
Registered: Oct 2018
Posts: 21

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by pan64 View Post
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
and that will just replay the key sequence as you specified.
https://vim.fandom.com/wiki/Macros
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:
Code:
sed -i 's/<\/div>//' index.html
 
Old 11-25-2019, 01:12 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,981

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
\w is exactly one character,
\w* zero or more chars
\w\w* is one ore more chars, same as:
\w+ one or more chars, but + is sometimes not understood


< and > are used as is, do not need to escape them (so \ is not required before >)
 
Old 12-04-2019, 07:38 AM   #11
Geist
Member
 
Registered: Jul 2013
Distribution: Slackware 14 / current
Posts: 442

Rep: Reputation: 196Reputation: 196
HTML with regex is generally awful.

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.

Last edited by Geist; 12-04-2019 at 07:42 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Fedora shows 'man vim' but when execute 'vim' got "bash: vim: command not found..." ? flash_os Linux - Newbie 19 01-03-2015 11:56 PM
Switching from vim to vim -g from inside vim iDragoon Linux - Software 4 05-15-2009 11:46 AM
Editor comparison: vim VS vim-lite, Cleaning vim Ruler2112 *BSD 4 04-13-2009 04:26 PM
is it possible to create vim macro to..... hedpe Linux - Software 1 06-16-2005 08:41 PM
#defining a macro inside a macro? saravkrish Programming 1 05-24-2005 09:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration