Vim Tips Wiki
Register
m (Like moved to List buffers sorted by name: Page moved by JohnBot to improve title)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=976
 
|id=976
  +
|previous=975
|title=Like
 
  +
|next=977
|created=August 20, 2005 17:18
+
|created=2005
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Suresh Govindachar
 
|author=Suresh Govindachar
 
|version=6.0
 
|version=6.0
 
|rating=9/5
 
|rating=9/5
  +
|category1=
|text=
 
  +
|category2=
 
 
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 @" | silent ls | redir END | echo "\n" |
 
 
perl $foo=VIM::Eval('@"');
 
 
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");}
 
 
&lt;cr&gt;
 
 
 
 
--Suresh
 
 
 
 
 
 
}}
 
}}
  +
This tip requires Perl and overwrites the unnamed register.
   
 
The output of <code>:ls</code> is sorted by buffer number.
== Comments ==
 
 
OOPS! add the following line:
 
 
for (keys %list){delete $list{$_}};
 
 
before the ending &lt;cr&gt;
 
 
 
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");}
 
}
 
&lt;cr&gt;
 
 
 
 
 
 
 
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");
 
\ }
 
\ }
 
\ }
 
\ &lt;cr&gt;
 
 
 
 
 
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&lt;bang&gt; | 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);
 
\ }
 
\ &lt;cr&gt;
 
 
 
 
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 &#35;159]) to help work with buffers,
 
I prefer just using :b &lt;fragment&gt; 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.
 
   
 
The following command results in a user defined command named <code>:Ls</code> (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 which you can place in your vimrc:
   
  +
<pre>
 
 
command! -bang Ls redir @" | silent ls<bang> | redir END | echo " " |
 
\ perl {
 
\ my $msg=VIM::Eval('@"');
 
\ 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>
  +
</pre>
   
 
==Comments==
anon
 
, August 24, 2005 21:24
 
----
 
<!-- parsed by vimtips.py in 0.622957 seconds-->
 

Latest revision as of 06:01, 13 July 2012

Tip 976 Printable Monobook Previous Next

created 2005 · complexity intermediate · author Suresh Govindachar · version 6.0


This tip requires Perl and overwrites the unnamed register.

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 which you can place in your vimrc:

command! -bang Ls redir @" | silent ls<bang> | redir END | echo " " |
 \ perl {
 \ my $msg=VIM::Eval('@"');
 \ 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>

Comments[]