Display number of current buffer
Asked Answered
C

4

28

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.

Camisole answered 5/4, 2011 at 6:14 Comment(0)
S
39

: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  
"}  
Subassembly answered 5/4, 2011 at 6:18 Comment(2)
To clarify, the lines with braces in aren't actually vim syntax, right? That's just comments that mimic (for example) C?Steamroller
Yes, those are just comments.Embolectomy
E
38

To get the answer without configuring anything:

:echo bufnr('%')

Electrostatic answered 24/6, 2012 at 4:15 Comment(1)
and I would shorten it by a custom command ``` command Bfno :echo bufnr('%') ```Elegit
C
4

You need %n listed in your statusline setting. Try :help statusline in vim.

Coorg answered 5/4, 2011 at 6:18 Comment(0)
B
1
set statusline=%!bufnr('%')

Also see :help status-line

Blissful answered 5/4, 2011 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.