Vim Tips Wiki
Register
No edit summary
 
m (Like moved to List buffers sorted by name: Page moved by JohnBot to improve title)

Revision as of 10:10, 18 October 2007

Previous TipNext Tip

Tip: #976 - List buffers sorted by name

Created: August 20, 2005 17:18 Complexity: intermediate Author: Suresh Govindachar Version: 6.0 Karma: 9/5 Imported from: Tip#976

REQUIRES: perl

WARNING: Overwrites contents of the un-named register

Tested on 6.3



I assume you are familiar with the built-in command :ls.

The output of :ls is sorted by buffer number.

The following command results in a user defined command named :Ls

(all user defined commands need to start with a capital letter).

The output of :Ls is the same as the output of :ls except that the

output is sorted by buffer name.


Here's the command -- written in many lines to improve readability.

I have it as a single line in my vimrc:


command! Ls redir @"

Comments

OOPS! add the following line:

for (keys %list){delete $list{$_}}; 
before the ending <cr> 


Suresh Govindachar , August 20, 2005 17:36


Well, the preceding works, but here's another form that's equivalent:

command! Ls redir --AT--" | silent ls | redir END | echo "\n" | 
perl {my $foo=VIM::Eval('--AT--"'); 
my %list=(); 
while($foo =~ m/(.*?line\s+\d+)/g) 
{$val = $1; $val =~ m/"([^"]+)"/; $list{$1} = $val;} 
VIM::Msg("\n"); 
for $boo (sort keys %list) 
{VIM::Msg("$list{$boo}\n");} 
} 
<cr> 




Suresh Govindachar , August 20, 2005 18:26


Enhancement: Ignore case of buffer name while sorting on Windows


command! Ls redir --AT--" | silent ls | redir END | echo "\n" |

\ perl { 
\ my $foo=VIM::Eval('--AT--"'); 
\ my %list=(); 
\ my $boo; 
\ while($foo =~ m/(.*?line\s+\d+)/g) 
\ { 
\ $boo = $1; 
\ $boo =~ m/"([^"]+)"/; 
\ $list{$1} = $boo; 
\ } 
\ VIM::Msg("\n"); 
\ if ($^O =~ /mswin/i) 
\ { 
\ for $boo (sort { lc($a) cmp lc($b) } keys %list) 
\ { 
\ VIM::Msg("$list{$boo}\n"); 
\ } 
\ } 
\ else 
\ { 
\ for $boo (sort keys %list) 
\ { 
\ VIM::Msg("$list{$boo}\n"); 
\ } 
\ } 
\ } 
\ <cr> 



Suresh Govindachar , August 21, 2005 8:26


Sorry for the incremental improvements. I wrote the first version quickly to get the job done, and have been reviewing it and improving it gradually. Next time I have an idea, I will wait till it matures (few days maybe) before releasing.

Here's the present version:

command! -bang Ls redir --AT--" | silent ls<bang> | redir END | echo " " |

\ perl { 
\ my $msg=VIM::Eval('--AT--"'); 
\ my %list=(); 
\ my $key, $value; 
\ while($msg =~ m/(.*?line\s+\d+)/g) 
\ { 
\ $value = $1; 
\ $value =~ m/"([^"]+)"/; 
\ $key = $1; 
\ ($^O =~ /mswin/i) and $key = lc($key); 
\ $list{$key} = $value; 
\ } 
\ my $msg = ; 
\ for $key (sort keys %list) 
\ { 
\ $msg .= "$list{$key}\n"; 
\ } 
\ VIM::Msg($msg); 
\ } 
\ <cr> 


Suresh Govindachar , August 21, 2005 11:37


I dont get the point of so much script to do so little?

Occam's Phaser , August 24, 2005 17:14


I find it very little script to do lots of work:

get output of ls 
extract name of buffers 
sort (takes lots of work to do this, but done easily in perl) by buffer name while ignoring case if on Windows 
show the results 

and

handle the ! modifier 

While there are several plugins (such as [/scripts/script.php?script_id=159 vimscript #159]) to help work with buffers, I prefer just using :b <fragment> and :ls. This tip's :Ls makes eyeballing the buffer of interest from within the list of buffers easy, thereby further eliminating the need for plugins, at least for my way of using this great editor, Vim.




anon , August 24, 2005 21:24