10

I was recently aware of the filtering ! technique, and I love it. Unfortunately, I am not able to use it in a visual-block mode. Hope some experts here would help me out.

Basically, I want to in-place sort a column such as the third below (just a MWE). And I don't want to disturb the other columns when I sort.

I tried to filter (!) the visually (block-mode) selected third column through sort utility. But the sort rather extends beyond selection i.e. all of the rows are swapped, not just the selection, that too sorting based on the first column!

1.  LARGEST         40
2.  SECOND-LARGEST  168     
3.  MID             2.8     
4.  MID             540     
5.  MID             14.84   
6.  MID             40      
7.  SECOND-LEAST    64      
8.  LEAST           8

I selected last column using visual-block, and said !sort --reverse which shows up as

'<,'>!sort --reverse

And the result is strange opposed to what I expected. The result has the entire rows sorted/swapped, not just one column, and that too, based on the first column irrespective of my third-column selection!

 8.  LEAST           8    
 7.  SECOND-LEAST    64      
 6.  MID             40      
 5.  MID             14.84   
 4.  MID             540     
 3.  MID             2.8     
 2.  SECOND-LARGEST  168     
 1.  LARGEST         40   

Unless impossible otherwise, I would like a pure vim+sort solution, rather than the relatively advanced tools such as sed/awk. It is more importantly to know myself, how limited the visual-block mode is, compared to say, visual-mode.

  • This doesn't solve your problem, but note that there's a vim internal sort command available, so you don't need to invoke the external !sort for doing a regular sort. – Dalker Oct 10 '16 at 4:17
  • 2
    Filters only work on lines. – romainl Oct 10 '16 at 5:30
  • @Dalker Yeah, thanks. But I already tried that, which gave a similar result. – Loves Probability Oct 10 '16 at 5:45
  • @romainl Is that it? No rays of hope? I rarely find that happening with Vim!! But nonetheless, Is it not strange to say "Filters only work on lines" after you selected something else and no signs of a problem until the output? If it doesn't pop a warning/error, I would even argue that it is a BUG. – Loves Probability Oct 10 '16 at 5:46
  • A bug? Haha no. The behavior is not only documented (:help filter) but also standardized (sorry, I don't know how to deep-link that POS). You can't complain if you don't RFTM. – romainl Oct 10 '16 at 6:26
9

I think your best hope is the vis.vim plugin. This plugin provides a command B which allows to apply a command to a block.

Here after installing the plugin, you'd select your block and then use:

:'<,'>B !sort

Note that the command can be anything, so instead of !sort you could do a lot of other processing on the block like saving it to another file (:'<,'>B w foo.txt) or applying a substitution (:'<,'>B s/foo/bar/g)

  • 1
    @LovesProbability, If you're trying to sort the third column without altering their first two columns, this thread could be helpful. – Jair López Oct 12 '16 at 8:35
  • @JairLópez With no offense, it is really funny how my question is copied by someone to somewhere else, where he was asked to go back to the original source (i.e. this page along side of a rephrased-copy of this answer), and finally you ask me to go find an answer there. – Loves Probability Oct 14 '16 at 3:30
  • Thank you for your comment, I did it because (1) both your question and this answer were interesting; (2) maybe you're not alone and there might be more people interested in; (3) this is a great opportunity to help someone to understand how $ is used in blockwise visual mode. – Jair López Oct 14 '16 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.