Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reassign a buffer number in VIM?

Tags:

vim

I had thought vim was all powerful until I wanted to get rid of buffers in the list and reuse them for something else. For example, if I have

1 "f1.h"
2 "f2.h"
3 "f3.h"
4 "f1.cpp"
5 "f2.cpp"
6 "f3.cpp"

and I want to replace f1.h - f3.h with something else, but in the same buffer sequence:

1 "n1.h"
2 "n2.h"
3 "n3.h"
4 "f1.cpp"
5 "f2.cpp"
6 "f3.cpp"

I can't figure out how to do it. I can do "1,3bd" and delete buffers 1-3, or even "1,3bw" and wipe them out, but don't see any way to reuse those numbers without restarting vim. The vim buffer faq at http://vim.wikia.com/wiki/Vim_buffer_FAQ appears to confirm this.

But I don't want to believe that vim really has this limitation, is there really no way to do this from within vim?

like image 457
William Knight Avatar asked Feb 10 '10 17:02

William Knight


1 Answers

You can't reassign buffer numbers in the same session. Your best bet is to look around at vimscript alternatives.

One buffer explorer that handles sorting to most recent used files (and can delete buffers) is Buffer Explorer / Browser.

There are loads of other buffer switching scripts on vim.org that cater to their various authors needs. Or alternatively you could roll your own with some mappings.

I came up with this for Django.

http://code.djangoproject.com/wiki/UsingVimWithDjango#Mappings

It gives me quick two/three keystroke access to 20 standard named files relative to an app directory. You could come up with something for your project on these lines.

My mappings are \1, \2, \3 etc so the number paradigm is still there.

I also mapped \vr for ~/.vimrc and \br for ~/.bashrc.

The important thing it can and should be a very efficient process.

like image 62
michael Avatar answered Oct 16 '22 09:10

michael