How to sort numeric and literal columns in Vim


How to sort numeric and literal columns in Vim



Using Vim 6.0. Say I'm editing this file:

sdfsdg dfgdfg  34     12 2      4 45     1 34     5 

How do I sort the second column?




Swap text around equal sign

1:



NERD commenter : How to comment out a range
If you have decent shell available, select your numbers and run the command.
Autoindenting Haskell in Vim
:'<,'>!sort -n -k 2 
If you gonna type this in visual mode, after typing the colon, markers '<,'> will appead automatically, and you'll only have to type the rest of it..
How to delete a word and go into insert mode in Vim?
This type of commands (:[motion]!) is called filtering.


How do you replace a word occurring on consecutive lines?
You can learn more by consulting vim's help:.
vim edit multiple locations simultaneously
:h filter 


Linux/Ubuntu directory location ~/.vim/syntax/


VIM Restore last search pattern

2:


sort all lines on second column N.
:sort /.*\%2v/ 
reference:
http://rayninfo.co.uk/vimtips.html.


3:


For vim7 I would go for:.
:sort n /.*\s/ 
This will sort numbers ignoring text matched by given regexp.

In your case it is second column..


4:


sort by second column.
(visual + !sort)  
sort by third column.
sort -k 3  
or.
:sort /.*\%3v/ 
Or .
select the lines you wish to sort using the Capital V command. 

Then enter !sort -k 3n
or .
the below code tells VIM to skip the first two words in every line and to sort on whatever follows: :%sort /^\S\+\s\+\S\+\s\+/  
or.
i.e. 

sort by 8th line :sort /.*\%55v/ The 'virtual' specification is the absolute column number, which treats spaces and tabs as a single character (in other words, it does NOT count tabs as 8 spaces (or whatever your tab settings are).

sort by the last column.
:%sort /\<\S\+\>$/ r 



88 out of 100 based on 78 user ratings 478 reviews