> suppose I have a few line that goes like this:
> str_list="foo bar   man act"
> 
> how can I sort all the words within the quote “ ... " so that it
> becomes "act bar foo man"? It is okay to eliminate extra whitespace so
> that it ends up with only one space behind each word.

This is a common pattern of "decorate, sort, undecorate".  In 
this case the decoration is putting each word on its own line. 
If it's long, you can readily do this with

1) issue

   :s/\(\<\w\+\)\s*/\1\r/g

this should yield

   str_list
   ="foo
   bar
   man
   act
   "

which you can hand-tweak to add one extra newline before "foo", 
yielding

   str_list
   ="
   foo
   bar
   man
   act
   "

The aim in the first step is just to get each word on its own 
line.  If your string is short, it may be faster to do it by 
hand.  But if it's 20+ words in the string, using the regexp can 
be faster.

2) highlight the range of words in the string and use

   :'<,'>sort

to sort them

3) highlight the whole shebang and press J to rejoin them.

4) Clean up the extra space at the beginning and end of your 
string (and optionally around the "=")

---------------

After typing the above, an alternative method (assuming you have 
Vim7.0+) occurred to me, so you might also try

   vi"       visualize-inner-quotation
   c         change
   ^R=       control+r = (expression register)
   join(sort(split(@")), ' ')
             the expression to split the deleted content
             on whitespace, sort it, and join it back
             together with a single space
   <enter>   accept the expression
   <esc>     leave insert-mode

If it's something you do frequently, it's easily mappable:

   :nnoremap <f4> vi"c<c-r>=join(sort(split(@")), ' ')<cr><esc>
   :vnoremap <f4> c<c-r>=join(sort(split(@")), ' ')<cr><esc>


Just a couple ideas -- use whichever works better for your needs 
(or whichever you remember).

-tim





--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

Reply via email to