Viml buffers
From wikinotes
Buffer Info
help filename-modifiers " list of filename-modifiers call expand('%:p') " apply filename-modifier to buffer call fnamemodify('file.txt', ':p') " apply filename-modifier to string echo bufname() " echo buffer name echo bufnr() " echo buffer number echo winbufnr('file.txt') " echo window-number containing buffer named 'file.txt' :ls " list buffers
Custom Buffers
Buffers are any text vim currently has stored in memory. You can use buffers to create your own custom windows from within vim.
Creating Buffers
badd test "" Create a new (hidden) buffer called 'test' b test "" Set current window to buffer 'test' setlocal nomodifiable "" Make a buffer NOT modifiable setlocal textwidth=70 "" Restrict a buffer's allowed charactersInsert Text into Buffer
let var = 'abcdefg' put = var "" Put contents of a variable onto the next line execute ":normal i" . strftime("%c") "" Put contents of a variable at cursor (without newline)Buffer Hotkeys
Buffers can have custom hotkeys associated to them, typically if you want to do anything useful with them, you'll need to perform an operation on some text, which is recognized by regex.
:map <buffer> <cr> :echo 'hello'<CR> "" Not that <buffer> is a word, not the name of the buffer. "" This action is performed on the current buffer.Examples
Simple Menu
function! SimpleMenu() "" Build Buffer badd colourswap_buffer vert sb colourswap_buffer "" Draw Buffer Contents put ='-- Change Colourscheme --' put =' ' put =' itemA' put =' itemB' put =' itemC' call MenuColours() setlocal nomodifiable vert resize 40 " Select MenuItem map <buffer> <cr> "ayy: let sel=@a<CR>: call ExecMenuItem( sel )<CR> " Close Window map <buffer> q :call CloseMenu()<CR> map <buffer> <leader>q :call CloseMenu()<CR> map <buffer> qq :call CloseMenu()<CR> endfunction function! MenuColours() syn match clrMenu_title '^[a-zA-Z_\\\- 0-9]\+$' highlight clrMenu_title cterm=bold ctermfg=070 syn match clrMenu_text ' \+[a-zA-Z_\\\- 0-9]\+$' highlight clrMenu_text ctermfg=102 endfunction function! CloseMenu() q! bd colourswap_buffer "get curent filepath without file if filereadable('colourswap_buffer') call delete('colourswap_buffer') endif endfunction function! ExecMenuItem( sel ) let sel = a:sel " Extract word from yanked line let sel = substitute(sel, '[^a-zA-Z_\\\-]', '', 'g') echo sel endfunction