Last active
October 7, 2015 08:27
-
-
Save Raimondi/3134716 to your computer and use it in GitHub Desktop.
Define sequencial mappings for the first n listed buffers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| " SeqBufNumMaps([style[, limit]]) | |
| " Creates sequencial mappings to switch among listed buffers. | |
| " It takes two optional arguments | |
| " - lhs : The LHS of the mapping, use '%d' to place the number. e.g.: | |
| " This will create mappings with weechat-style jumps '<Esc>j%d' (as | |
| " suggested by ccxCZ), like <Esc>j1, >Esc>j2, etc. | |
| " The default is '<leader>%d' => <leader>1, <leader>2, etc. | |
| " The default value will be used if the argument is not provided, it | |
| " is empty or it does not have the string '%d' inside. | |
| " - limit: The maximum number of mappings to create. Default is 10. | |
| function! SeqBufNumMaps(...) | |
| let limit = get(a:, '2', 10) | |
| let lhs = !a:0 || match(a:1, '%d') == -1 ? '<leader>%d' : a:1 | |
| " How many leading zeroes do we need? | |
| let leading0s = float2nr(ceil(log10(limit))) | |
| let lhs = substitute(lhs, '%d', '%0' . leading0s . 'd', '') | |
| " Clean-up | |
| for n in range(1,get(s:, 'map_number', 1)) | |
| exe printf('silent! nunmap ' . lhs, n) | |
| endfor | |
| " Get the listed buffers. | |
| let buflist = filter(range(1,bufnr('$')), 'buflisted(v:val)') | |
| let len = len(buflist) | |
| " Store this number for clean-up. | |
| let s:map_number = len | |
| " Adjust the limit if needed. | |
| let limit = limit <= len && limit > 0 ? limit : len | |
| let command = 'nnoremap ' . lhs . ' :b %d<CR>' | |
| " Define mappings. | |
| for n in range(1, limit) | |
| exec printf(command, n, buflist[n - 1]) | |
| endfor | |
| endfunction | |
| autocmd! BufCreate,BufWinEnter,BufNew,WinLeave * call SeqBufNumMaps() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment