from the old vi faq

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

from the old vi faq

Eli the Bearded
http://www.faqs.org/faqs/editor-faq/vi/part2/
------ start quote 8< ------
6.1 - Silly vi tricks
  Note: Also check out the Silly macros down below. Many macros and
tricks are interchangeable.

  xp     This will delete the character under the cursor, and put it
afterwards.  In other words, it swaps the location of two characters.
 
  ddp    Similar to xp, but swapping lines.
 
  yyp    duplicate a line

  uu     Undo and redo last change.  (This will take you to the last
modification to the file without changing anything.)  You can also use
this to compare the changes to a line.  Make the changes to the line,
press U to undo the changes to the current line, and then press u to
toggle between the two versions.

  :g/.*/m0
         This will reverse the order of the lines in the current file.
m0 is the ex command to move the line to line 0.

  :v/./d or :g/^$/d
         Removes all blank lines.

  :g/^[ <ctrl-v><tab>]*$/d
         Removes all lines that only have whitespace.

  :v/./$s/$/<ctrl-v><enter>./|'';/./-1j|$d
         Replaces multiple blank lines with just one blank line.
------ >8 end quote  ------

All of that works in vim (save "uu" with modern undo settings), except
for the last one. I happen to have true vi handy ("Version 4.0 (gritter)
3/25/05"), as well as nvi ("Version (1.81.6-2013-11-20nb3)"), elvis
("2.2.0"), vim 7.4, and vim 8.1.

The replace multiple blank lines works in true vi and nvi, but not in
elvis or either vim. The trick works by the :v/./ selecting a group of
lines, $s editing the last line in that group to be a blank line and a
line with a dot, then |'' returning to the first line in the group, /./-1j
joining all but the last line of the group (so as not to join the new
dot line), and finally |$d deleting that last dot line. It's a
seriously complicated "trick", with lots of subtle compatibility tests
built right in.

Is this a known incompatiblity in vim? I don't recall seeing it
documented. And I sought out that FAQ precisely for that trick since I
recalled it existed, but not what it was.

Elijah
------
tends to use :g/^/m0 for reversing lines (even works in ed)

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: from the old vi faq

Erik Christiansen
On 30.09.18 03:15, Eli the Bearded wrote:
> http://www.faqs.org/faqs/editor-faq/vi/part2/
> ------ start quote 8< ------
> 6.1 - Silly vi tricks

Seems to be a typo there? For decades some have used them as _Standard_
Vi(m) Operations, especially xp, as mistyping is more common than the
need for the others.

>   Note: Also check out the Silly macros down below. Many macros and
> tricks are interchangeable.
>
>   xp     This will delete the character under the cursor, and put it
> afterwards.  In other words, it swaps the location of two characters.
>  
>   ddp    Similar to xp, but swapping lines.
>  
>   yyp    duplicate a line
>
>   uu     Undo and redo last change.  (This will take you to the last
> modification to the file without changing anything.)  You can also use
> this to compare the changes to a line.  Make the changes to the line,
> press U to undo the changes to the current line, and then press u to
> toggle between the two versions.

Must have encountered that, way back, because my fingers use u^r these days.

What I'd group with the above is "deep", to swap two words with the
cursor on the space preceding the first. (Most used while rephrasing a
sentence in a post, I find.)

What makes a *nix environment fun is the diversity of ways to skin any
cat, and I tend to us tac to reverse line order, as it's quicker than
looking up a vim way to do it - at least in my case. (And I don't think
of vim as another emacs.)

Erik

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: from the old vi faq

Mikolaj Machowski
In reply to this post by Eli the Bearded
On Sunday, September 30, 2018 at 9:15:36 AM UTC+2, Eli the Bearded wrote:
>
>   :v/./$s/$/<ctrl-v><enter>./|'';/./-1j|$d
>          Replaces multiple blank lines with just one blank line.

What's wrong with:

    :%s/\n\n\+/\r\r/

?

m.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: from the old vi faq

Bram Moolenaar
In reply to this post by Eli the Bearded

Eli the Bearded wrote:

> http://www.faqs.org/faqs/editor-faq/vi/part2/
> ------ start quote 8< ------
> 6.1 - Silly vi tricks
>   Note: Also check out the Silly macros down below. Many macros and
> tricks are interchangeable.
>
>   xp     This will delete the character under the cursor, and put it
> afterwards.  In other words, it swaps the location of two characters.
>  
>   ddp    Similar to xp, but swapping lines.
>  
>   yyp    duplicate a line
>
>   uu     Undo and redo last change.  (This will take you to the last
> modification to the file without changing anything.)  You can also use
> this to compare the changes to a line.  Make the changes to the line,
> press U to undo the changes to the current line, and then press u to
> toggle between the two versions.
>
>   :g/.*/m0
>          This will reverse the order of the lines in the current file.
> m0 is the ex command to move the line to line 0.
>
>   :v/./d or :g/^$/d
>          Removes all blank lines.
>
>   :g/^[ <ctrl-v><tab>]*$/d
>          Removes all lines that only have whitespace.
>
>   :v/./$s/$/<ctrl-v><enter>./|'';/./-1j|$d
>          Replaces multiple blank lines with just one blank line.
> ------ >8 end quote  ------
>
> All of that works in vim (save "uu" with modern undo settings), except
> for the last one. I happen to have true vi handy ("Version 4.0 (gritter)
> 3/25/05"), as well as nvi ("Version (1.81.6-2013-11-20nb3)"), elvis
> ("2.2.0"), vim 7.4, and vim 8.1.
>
> The replace multiple blank lines works in true vi and nvi, but not in
> elvis or either vim. The trick works by the :v/./ selecting a group of
> lines, $s editing the last line in that group to be a blank line and a
> line with a dot, then |'' returning to the first line in the group, /./-1j
> joining all but the last line of the group (so as not to join the new
> dot line), and finally |$d deleting that last dot line. It's a
> seriously complicated "trick", with lots of subtle compatibility tests
> built right in.
>
> Is this a known incompatiblity in vim? I don't recall seeing it
> documented. And I sought out that FAQ precisely for that trick since I
> recalled it existed, but not what it was.

AFAIK this builds upon a bug in Vi.  In Nvi Keith Bostic decided to keep
it like that (perhaps because of this example).  I decided that it's
really an unintentional bug and did not replicate it.

Using "$" after :v does not refer to "the last in a a group", since :v
works on a per-line base, it marks every line not matching the pattern.
So in Vim the "$" refers to the last line, and changes that.

There are much simpler ways to collapse multiple blank lines.

--
hundred-and-one symptoms of being an internet addict:
122. You ask if the Netaholics Anonymous t-shirt you ordered can be
     sent to you via e-mail.

 /// Bram Moolenaar -- [hidden email] -- http://www.Moolenaar.net   \\\
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: from the old vi faq

Tony Mechelynck
In reply to this post by Mikolaj Machowski
On Sun, Sep 30, 2018 at 5:17 PM Mikolaj Machowski <[hidden email]> wrote:

>
> On Sunday, September 30, 2018 at 9:15:36 AM UTC+2, Eli the Bearded wrote:
> >
> >   :v/./$s/$/<ctrl-v><enter>./|'';/./-1j|$d
> >          Replaces multiple blank lines with just one blank line.
>
> What's wrong with:
>
>     :%s/\n\n\+/\r\r/
>
> ?
>
> m.

or with :%s/^\_s*\n/\r

(to replace any number of consecutive lines containing only whitespace
by just one empty line) ?

Variation: the same, plus suppress end-of-line whitespace:

:%s/\_s*\n/\r

(both of these use the fact that * means "as many as possible")

Best regards,
Tony.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: from the old vi faq - squeezing blank/empty lines

Sven Guckes-3
* 2018-09-30 Eli the Bearded:
> :v/./$s/$/<ctrl-v><enter>./|'';/./-1j|$d
> Replaces multiple blank lines with just one blank line.

* 2018-09-30 Mikolaj Machowski <[hidden email]>:
> What's wrong with: :%s/\n\n\+/\r\r/

* Tony Mechelynck <[hidden email]> [2018-10-01 15:06]:
> or with :%s/^\_s*\n/\r
> Variation: the same, plus suppress end-of-line whitespace:
> :%s/\_s*\n/\r
> (both of these use the fact that * means "as many as possible")

i always used this one:
  :g/^$/,/./-j

works for me :)

Sven

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: from the old vi faq - squeezing blank/empty lines

Salman Halim
On Fri, Oct 5, 2018, 20:24 Sven Guckes <[hidden email]> wrote:
* 2018-09-30 Eli the Bearded:
> :v/./$s/$/<ctrl-v><enter>./|'';/./-1j|$d
> Replaces multiple blank lines with just one blank line.

* 2018-09-30 Mikolaj Machowski <[hidden email]>:
> What's wrong with: :%s/\n\n\+/\r\r/

* Tony Mechelynck <[hidden email]> [2018-10-01 15:06]:
> or with :%s/^\_s*\n/\r
> Variation: the same, plus suppress end-of-line whitespace:
> :%s/\_s*\n/\r
> (both of these use the fact that * means "as many as possible")

i always used this one:
  :g/^$/,/./-j

works for me :)

Sven

Likewise (with a user command called DeleteMultipleLines, which seems much slower to type, admittedly). However, the versions Tony and Mikolaj espoused are probably more obviously understood, especially by those who didn't cut their teeth in the original vi world.

Regards,

Salman

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: from the old vi faq - squeezing blank/empty lines

meine van essen
>      * 2018-09-30 Eli the Bearded:
>      > :v/./$s/$/<ctrl-v><enter>./|'';/./-1j|$d
>      > Replaces multiple blank lines with just one blank line.
>      * 2018-09-30 Mikolaj Machowski <[2][hidden email]>:
>      > What's wrong with: :%s/\n\n\+/\r\r/
>      * Tony Mechelynck <[3][hidden email]> [2018-10-01
>      15:06]:
>      > or with :%s/^\_s*\n/\r
>      > Variation: the same, plus suppress end-of-line whitespace:
>      > :%s/\_s*\n/\r
>      > (both of these use the fact that * means "as many as possible")
>      i always used this one:
>      Â  :g/^$/,/./-j

nice to see that vim makes it possible to do the same thing in different
ways

nice to see here the possibilities shared, so that we can learn from
eachother

TNX

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.