Try something like

:0/^a/,/^\(a\)\@!/sort

It means loosely:

: - perform an ex command
0 - from before the beginning of the file
/^keyword_a/ - find a line beginning with 'keyword_a'
, - and operate until
/ - we find
^ - the beginning of a line
\(keyword_a\)\@! - where 'keyword_a' doesn't match
/sort - with the sort command

Hi Ben,

...got some problems here...may be due to a not-so-exact description
of what I need...

I will try to explain... ;)

The file in its original version looks like this ( there is additional
text following keyword_[ab], which I left out here, but makes the
final
     sort /<pattern>/
necessary).

something
different
fnord
foo
baz
gnu
keyword_a
noordszki
looping
tatdat
drool
socket
pipe
Unix
tools
cryptic
other-pipe
other-Unix
other-tools
other-cryptic
keyword_a
keyword_a
keyword_a
keyword_b
keyword_b
keyword_a
keyword_a
keyword_a
keyword_b
keyword_a
keyword_a
keyword_a
keyword_b
keyword_a
keyword_b
keyword_b

after the file is loaded, my script does a

:gg
:/keyword_a
:.,$sort

this way the file becomes

something
different
fnord
foo
baz
gnu
keyword_a
noordszki
looping
tatdat
drool
socket
pipe
Unix
tools
cryptic
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_b
keyword_b
keyword_b
keyword_b
keyword_b
keyword_b

Is the 'keyword_a' between 'gnu' and 'noordszki' accidental? Otherwise
your sort has not worked, because it should have sorted from that
keyword_a all the way to the bottom. This would give you

something
different
fnord
foo
baz
gnu
Unix
cryptic
drool
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_a
keyword_b
keyword_b
keyword_b
keyword_b
keyword_b
keyword_b
looping
noordszki
pipe
socket
tatdat
tools

:/^keyword_a/,/^\(keyword_a\)\@!/sort /<pattern>/

This line triggers an error saying: E493 Backward range given.

What this means is that your script found 'keyword_a' (/^keyword_a/) but
when searching for a line without 'keyword_a' (/^\(keyword_a\\@!/) it
reached the end of the file, so had to wrap around and continue the
search from the top. That meant the first position (with keyword_a) was
after the second position (without keyword_a).

This shouldn't have happened with keyword_a, but it would probably
happen with keyword_b with your file above, as keyword_b is in the last
line in the file, so it would not find a line without it unless it
wrapped around.

I looked into the help, which says that start and end of the range are
swapped and -- if the command is preceeded with :silent the start and
end will be automagically swapped.
I did this.

Now no error is given anymore but the the script does not work --
the sort of the above line ignores its /<pattern>/.

What is probably happening is that your last sort (the keyword_b one),
since it is swapping the backwards range, is actually sorting
*everything else* in the file, except what you want it to sort! This
makes it appear as if it has sorted keyword_a ignoring the pattern, but
it is actually the keyword_b sort gone wrong.

Try this instead:

:/^keyword_b/,/^\(keyword_b\)\@!\|\%$/sort /<pattern>/

The \|\%$ means 'or end of the file'.

Note also, that you probably want to put at least a - between your : and
your / for keyword_a, because otherwise it will omit the first line of
keyword_a from your range, since that is where your cursor is after your
earlier :sort.

:-/^keyword_a/,/^\(keyword_a\)\@!\|\%$/sort /<pattern>/

I have simply no glue :) what is happening here...

Thank you very much in advance for any enlightment! :)

Hopefully I am correct about what is happening here. :-)

Good luck!

Ben.



--
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

Reply via email to