[comp.editors] Vi map help needed

gene@ponder.csci.unt.edu (Gene De Lisa) (03/03/91)

I'd like to remove those control-M cookies at the end on lines
that you get from CPM to UNIX files. I usually type
:%s/control-vcontrol-M$//
to can them. When I try to map any key to this sequence I get
an error; something about a regular expression.
Any hints?

-- 
more direct:

vortech!gene@Central.Sun.COM

dattier@ddsw1.MCS.COM (David W. Tamkin) (03/05/91)

gene@ponder.csci.unt.edu (Gene De Lisa) wrote in
<1991Mar2.210747.26919@solo.csci.unt.edu>:

| I'd like to remove those control-M cookies at the end on lines
| that you get from CPM to UNIX files. I usually type
| :%s/control-vcontrol-M$//
| to can them. When I try to map any key to this sequence I get
| an error; something about a regular expression.

"No previous regular expression."  A mapping is scanned and rescanned, so you
may need extra quoting sometimes, more so than you would need when you simply
type the command.  When you map an ex command to be called during visual
mode, it gets scanned at least twice: once on definition, once on execution.

So your search string gets rescanned down to nothingness, and a null search
string is taken to mean "reuse the last search string."  If you've just
entered vi and haven't yet done any previous commands (/, ?, :s, :g, for
example) that set search strings, you're telling vi to look for something
undefined.  Hence, "no previous regular expression."

| Any hints?

Believe it or not, it's [change K to the key you want to map]

:map K :%s/ctrl-Vctrl-Vctrl-Vctrl-Vctrl-Vctrl-M//ctrl-Vctrl-Mctrl-M

That's five ctrl-V's (you could use seven; the effect would be the same) and
a ctrl-M in the search string, nothing in the replacement string, one ctrl-V
and one ctrl-M after the replacement string to include the CR that the :s
command will need, and finally another CR to make the :map command take. 
You'll see

:map K :%s/^V^V^M//^M  (three ^V's if you typed seven)

when you type it and

K	K	:%s/^V^M//^M

when you review your mappings with the ":map" command.

David Tamkin  Box 7002  Des Plaines IL  60018-7002  708 518 6769  312 693 0591
dattier@ddsw1.mcs.com   MCI Mail: 426-1818  CIS: 73720,1570  GEnie: D.W.TAMKIN

"Parker Lewis Can't Lose" mailing list:   write flamingo-request@ddsw1.mcs.com

gast@maui.cs.ucla.edu (David Gast) (03/05/91)

In article <1991Mar04.162508.352@ddsw1.MCS.COM> dattier@ddsw1.MCS.COM (David W. Tamkin) writes:
>gene@ponder.csci.unt.edu (Gene De Lisa) wrote in

>| I'd like to remove those control-M cookies at the end on lines

>Believe it or not, it's [change K to the key you want to map]

>:map K :%s/ctrl-Vctrl-Vctrl-Vctrl-Vctrl-Vctrl-M//ctrl-Vctrl-Mctrl-M

I don't follow this map at all.  Why the two // in the middle?  And why
the stuff after the two slashes?

>That's five ctrl-V's (you could use seven; the effect would be the same) and
>a ctrl-M in the search string, nothing in the replacement string, one ctrl-V
>and one ctrl-M after the replacement string to include the CR that the :s
>command will need, and finally another CR to make the :map command take. 
>You'll see

Well, I don't know about your vi, but this works fine for me:

	:map X :%s/^V^M$//    (where ^ indicates a control key)

Further, I do not think you want to put too many ^Vs in or they will be
taken literally, not as escape characters.

I usually just use this little shell script, however:  (I call it stripm)

  ex - $1 <<!
  %s/^M$//
  w! $1
  q
  !

It is practically indestructable.  You probably don't need w! instead of w,
but it does not hurt.  Similarly, you probably don't need the q, but neither
of them hurts.

David Gast
gast@cs.ucla.edu
{uunet,ucbvax,rutgers}!{ucla-cs,cs.ucla.edu}!gast

dattier@vpnet.chi.il.us (David W. Tamkin) (03/06/91)

gast@maui.cs.ucla.edu (David Gast) wrote in <1991Mar5.052326.12313@cs.ucla.edu>:

| In article <1991Mar04.162508.352@ddsw1.MCS.COM> dattier@ddsw1.MCS.COM
| (David W. Tamkin) writes:

| >Believe it or not, it's [change K to the key you want to map]

| >:map K :%s/ctrl-Vctrl-Vctrl-Vctrl-Vctrl-Vctrl-M//ctrl-Vctrl-Mctrl-M

| I don't follow this map at all.  Why the two // in the middle?  And why
| the stuff after the two slashes?

The two slashes in the middle are the null replacement string.  You used it
yourself in your own example.  Note that I was saying to press ctrl-V five
times, not to get five ctrl-V's into the mapping (by pressing it eleven
times, the last one to get the CR taken literally).

I explained the stuff after the slashes in the article to which you were
following up.

| Well, I don't know about your vi, but this works fine for me:

| 	:map X :%s/^V^M$//    (where ^ indicates a control key)

Oh!  Look!  You have two slashes together!

It didn't work for me.  It wasn't working for the original asker either.

Also, even if it did work, typing X (I wouldn't use X myself because the
start-up definition of X is too useful for me to map away) would cause the
cursor to go the bottom of the screen, print out the command, and sit there
waiting for me to press CR.  That's why I included ctrl-V ctrl-M at the end
of the mapping.  The final ctrl-M, *as*I*said*before*, was just the CR one
presses at the end of the :map command; it wasn't part of the definition.

| Further, I do not think you want to put too many ^Vs in or they will be
| taken literally, not as escape characters.

If your vi or your Unix is such that the above mapping would work, then yes,
the one I suggested would fail, for some of the ^V's would be taken lite-
rally.  But vi, apparently, != vi.  Too many for you is more than three; too
many for me is more than seven.  The original asker tried it with one ^V and
got told "no previous regular expression," so I think his or her system would
require five or seven as those I use do.  One or three wouldn't do the trick.
It depends on how many times the definition gets scanned.  It's frequent to
need fewer escapes when you type a command then when you map a key to it and
not impossible, so I understand, to need more escapes when you include a
mapping in your .exrc than when you type the :map command during an edit.

| I usually just use this little shell script, however:  (I call it stripm)
| 
|   ex - $1 <<!
|   %s/^M$//

Oh, look, two slashes together!

|   w! $1
|   q
|   !
| 
| It is practically indestructable.  You probably don't need w! instead of w,
| but it does not hurt.  Similarly, you probably don't need the q, but neither
| of them hurts.

You do need a better spell-checker, however, Mr. Gast.

David Tamkin  PO Box 7002  Des Plaines IL  60018-7002  dattier@vpnet.chi.il.us
GEnie:D.W.TAMKIN  CIS:73720,1570  MCIMail:426-1818  708 518 6769  312 693 0591

fitz@mml0.meche.rpi.edu (Brian Fitzgerald) (03/08/91)

Gene De Lisa writes:
>I'd like to remove those control-M cookies at the end on lines
>that you get from CPM to UNIX files. I usually type
>:%s/control-vcontrol-M$//

A common approach to this problem is to use tr.  For example,
tr -d '\015' < cpmfile > unixfile

I realize that this does not directly answer the original question of
how to do this with an interactive editor.  Depending on the job to be
done, sometimes the filters (tr, grep, sed, awk and friends) are the
best way to go.

Brian Fitzgerald

cliff@demon.co.uk (Cliff Stanford) (03/09/91)

On a similar problem, can anyone *please* tell me why the following
does not work?
map! ^F ^V^M	{			/* ^V^[?(^V^MyB``pa */ ^V^[yypr}O
It is supposed to take the following line type
int xyzzy()
	and make:
int xyzzy()
	{	/* xyzzy */
	
	}	/* xyzzy */
what it does is to make:
int xyzzy()
	{	/* a */

	}	/* a */

	Thanks,
		Cliff.
-- 
Cliff Stanford				Email:	cliff@demon.co.uk (Work)
Demon Systems Limited				cms@demon.co.uk   (Home)
42 Hendon Lane				Phone:	081-349 0063	  (Office)
London	N3 1TT	England				0860 375870	  (Mobile)

gast@lanai.cs.ucla.edu (03/09/91)

In article <1991Mar06.004617.19066@vpnet.chi.il.us> dattier@vpnet.chi.il.us (David W. Tamkin) writes:
>gast@maui.cs.ucla.edu (David Gast) wrote in <1991Mar5.052326.12313@cs.ucla.edu>:

>| In article <1991Mar04.162508.352@ddsw1.MCS.COM> dattier@ddsw1.MCS.COM
>| (David W. Tamkin) writes:

>| >:map K :%s/ctrl-Vctrl-Vctrl-Vctrl-Vctrl-Vctrl-M//ctrl-Vctrl-Mctrl-M

I confess that I did not read the above as carefully as I should have.
I saw the typical ^ used as control key later in your posting and then
saw a lot of letters after the slashes and wondered what the hay?
I also, although I don't know why, interpreted some of the V's
as \/.  After rereading your original posting I see you were just using
user friendly notation--that is, the notation used by the original sender.
I was not wondering about the purpose of two slashes together; I couldn't
see why they would be in the middle of the :s command rather than at or
near the end.  I see now that it is only the unusual (but user friendly)
notation that threw me off.

>Also, even if it did work, typing X (I wouldn't use X myself because the
>start-up definition of X is too useful for me to map away) would cause the
>cursor to go the bottom of the screen, print out the command, and sit there
>waiting for me to press CR.

I used X because it frequently means pick any value you want.  Forgive me.
Anyway, I hardly ever use X when I am typing because hx (or dh) is faster
to type than shift-X.  There might be a use for X, however.  You are
correct that the implicit ^M at the end of my macro was not shown--I think
it was because of the way I transfered the macro from one window to the
other with X (or is that hx?  :-) .)  I know that I did not type a return
to execute it at any rate.  In actuality, I would probably bind this map
to a function key.

>| Further, I do not think you want to put too many ^Vs in
or they will be
>| taken literally, not as escape characters.

>If your vi or your Unix is such that the above mapping would work, then yes,
>the one I suggested would fail, for some of the ^V's would be taken lite-
>rally.

I interpreted your posting to say that the user could use 5 or 7 ^V's and
it would not make a difference.  My point was that it probably would.  (I
don't know the precise implementation being used).

Anyway, here is the interesting part and the reason for my posting.  I
tried the exact same map last night via modem and I got all sorts of odd
error messages, but sometimes it worked.  I also tried it today.  Sometimes
weird error messages; sometimes it worked; still others it worked but
provided a weird error message as well.  To make matters odder, one session
and the same map frequently gave more than one response with the same input
data.

My conclusion is that there seems to be some bug in vi for this problem
and I would recommend that you use ex (or tr or sed or some other command)
unless you are sure you have a non-buggy vi.  And I would suggest that if
possible you make this filter part of the software you use for getting
CPM files.

(A couple days ago we had a system crash and it appears that one of the
versions of vi we had was lost.  My previous posting used that vi, this
posting uses the other vi.)

David

weimer@garden.kodak.COM (Gary Weimer (588-0953)) (03/11/91)

In article <1991Mar08.181732.2458@demon.co.uk>, cliff@demon.co.uk (Cliff
Stanford) writes:
|> On a similar problem, can anyone *please* tell me why the following
|> does not work?
|> map! ^F ^V^M	{			/* ^V^[?(^V^MyB``pa */ ^V^[yypr}O
|> It is supposed to take the following line type
|> int xyzzy()
|> 	and make:
|> int xyzzy()
|> 	{	/* xyzzy */
|> 	
|> 	}	/* xyzzy */
|> what it does is to make:
|> int xyzzy()
|> 	{	/* a */
|> 
|> 	}	/* a */

It works fine in my vi (SunOS 4.1.1). You might want to try using named
buffers, i.e.:

map! ^F ^V^M	{			/* ^V^[?(^V^M"ayB``"apA */ ^V^[yypr}O

weimer@ssd.kodak.com ( Gary Weimer )

dattier@vpnet.chi.il.us (David W. Tamkin) (03/12/91)

gast@lanai.cs.ucla.edu wrote in <1991Mar9.053949.9736@cs.ucla.edu>:

| I used X because it frequently means pick any value you want.  Forgive me.

Yes, I understood, but remember, I was aiming to explain it to someone who
might have been a beginner.

| I interpreted your posting to say that the user could use 5 or 7 ^V's and
| it would not make a difference.  My point was that it probably would.  (I
| don't know the precise implementation being used).

I tried it on two sites I call: on both, five or seven ^V presses (two or
three caret+V pairs on the screen) worked and one or three gave the "no
previous regular expression" error that the original poster complained about.

As for my unexpectedly user-friendly notation, I was carrying through the
original poster's method.  Moreover, to show ^M, no matter how well people
know that it means ctrl-M and not caret+M, always leaves the question of
whether you meant to press ctrl-M or to press ctrl-V ctrl-M so that a ctrl-M
is entered.  (The same goes for ^V and ^[.)

Yes, :%!tr -d '\015' is a better way to solve the problem (assuming that the
only CR's in the file will be the ones in the EOL's).  So are dtou and lef,
if you have them (or either).

David Tamkin  PO Box 7002  Des Plaines IL  60018-7002  dattier@vpnet.chi.il.us
GEnie:D.W.TAMKIN  CIS:73720,1570  MCIMail:426-1818  708 518 6769  312 693 0591