Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display number of current buffer

Tags:

I have a simple requirement: I want to display the number of the buffer I'm currently editing. I don't want to type :buffers (or equivalent) but have this number show up in my status bar thingy along with the file name, current column information etc.

Is there any way to do this? Inbuilt help isn't very instructive on this point.

like image 481
Daniel Avatar asked Apr 05 '11 06:04

Daniel


People also ask

What is a buffer in Vim?

A buffer is an area of Vim's memory used to hold text read from a file. In addition, an empty buffer with no associated file can be created to allow the entry of text. The :e filename command can edit an existing file or a new file.

How do you open a buffer?

You can enter the buffer number you want to jump/edit and press the Ctrl-W ^ or Ctrl-W Ctrl-^ keys. This will open the specified buffer in a new window.

What is vi editor buffer?

The vi editor allows you to copy text from your file into temporary buffers for use in other places in the file during your current vi work session. Each buffer acts like temporary memory, or, as some programs call it, a "clipboard" where you can temporarily store information.

How do I save a buffer in Vim?

I edit it, then I use ':w' to save it and ':bd' to close the buffer and move on to the next one. This ':w:bd' to save and close the buffer feels long to me, and I suspect there's a more Vim ninja way of doing it.


2 Answers

:h statusline shows every bit of help required.

I have the following in my .vimrc

" Status Line {           set laststatus=2                             " always show statusbar           set statusline=           set statusline+=%-10.3n\                     " buffer number           set statusline+=%f\                          " filename            set statusline+=%h%m%r%w                     " status flags           set statusline+=\[%{strlen(&ft)?&ft:'none'}] " file type           set statusline+=%=                           " right align remainder           set statusline+=0x%-8B                       " character value           set statusline+=%-14(%l,%c%V%)               " line, character           set statusline+=%<%P                         " file position   "}   
like image 111
N 1.1 Avatar answered Sep 30 '22 20:09

N 1.1


To get the answer without configuring anything:

:echo bufnr('%')

like image 42
Benjamin Atkin Avatar answered Sep 30 '22 22:09

Benjamin Atkin