What do people recommend for easier manipulation of buffers in vim?
Using ls and b1, bn and bp commands is good but maybe there is a better way.
Is lusty explorer the best option?
I am using vim 7.3.
You should test all of them and see which one is the best according to your tastes and requirements.
I've used LustyExplorer for a while and loved it until I tried CtrlP which I find faster and more intuitive. I have :CtrlPBuffer mapped to ,b and see no reason to complain: it's both elegant, fast and intuitive.
You don't have to rely on plugins, though: :b <tab> allows you to tab through a list of all available buffers. You can do :b pattern<Tab> to get a smaller list.
Unite.vim is a new plugin and is what I switched to from CtrlP.
This is a good starting point if you want to explore what it can do.
I use minibufexpl.vim. I guess its main advantage is that it takes up very little space.
FuzzyFinder is another excellent add-on for buffer/file navigation:
http://www.vim.org/scripts/script.php?script_id=1984
Whichever plugin you choose for this, it's worth investing some time to find out all the ways it can help you.
If you are fine with having vim compiled with ruby support and have dev toolchain installed on the system (make, gcc, maybe something else — Gentoo users like me already have all of this) then Command-T is a good choice. To use it for switching buffers you should map something to :CommandTBuffer, I have
nnoremap ,b :CommandTBuffer<CR>
I used many plugins before, including minibufexpl and Bufexplorer, but there was something in all of them that used to annoy me.
Now I use young plugin Buffet, and I would recommend it because it seems to be really the best one for me: it is really fast and easy to use.
Personally i would like to switch my buffers by Ctrl+Tab and Shift+Ctrl+Tab, and buffers should be ordered in most-recently-used order.
Here is my buffet's config to achieve <C-Tab> and <S-C-Tab> switching:
noremap <silent> <C-Tab> :Bufferlistsw<CR>
noremap <silent> <C-S-Tab> :Bufferlistsw<CR>kk
if !has('gui')
map <S-q> :Bufferlistsw<CR>
endif
augroup BuffetAdd
if !exists("g:BuffetAdded")
let g:BuffetAdded = 1
au BufWinEnter buflisttempbuffer* map <buffer> <Tab> <CR>
au BufWinEnter buflisttempbuffer* map <buffer> <C-Tab> j
au BufWinEnter buflisttempbuffer* map <buffer> <C-S-Tab> k
" in console Vim we can't use <C-Tab> mappings (almost always),
" so this is temporary solution: <S-q>
if !has('gui')
au BufWinEnter buflisttempbuffer* map <buffer> <S-q> j
au BufWinEnter buflisttempbuffer* map <buffer> q <CR>
endif
" workaround Surround plugin issue in Buffet's window:
" disable "ds" mapping in the Buffet window (to make "d" work fast)
au BufEnter buflisttempbuffer* nunmap ds
au BufLeave buflisttempbuffer* nmap ds <Plug>Dsurround
endif
augroup END
Just one issue: Vim does not allow you to map release of some key, so, you need to press Tab again to really switch to buffer.
Anyway, if you don't need <C-Tab> switching, Buffet plugin works nice without it.
Update June 2019
BufExplorer is my unequivocal first-choice for buffer management.
" Buffer explorer
" ,be to open, q to close, d to delete buffer
Plug 'jlanzarotta/bufexplorer'
Highly rate the above plugin. It's simple and effective. Further details in the readme.
If you're looking for some "extras" in addition to the above (optional), I do also use:
" Close buffers but keep splits
Plug 'moll/vim-bbye'
and:
Plug '/usr/local/opt/fzf'
Plug 'junegunn/fzf.vim'
" this setting for quick search across buffers
nmap <silent> <leader>b :Buffers<cr>
Related
I've found some older posts here that discuss the difficulty with mapping the <C-j> shortcut in vim, i.e.
map <C-j> <C-w>j
due to latex-suite's usage of <C-j> to jump forward in the text. I'm in a similar situation, but I would like to keep latex-suite's normal shortcuts for when editing tex files, and only use the new mapping when editing non-tex files. I'm a little confused by why this isn't the default behavior - latex-suite's <C-j> shortcut shows up as a shortcut in the :map output even when I don't have a tex file loaded and other shortcuts (like `a mapping to \alpha) don't work. Am I understanding correctly that unlike most of latex-suite's shortcuts, this <C-j> shortcut from latex-suite gets loaded regardless of filetype for some reason? And if so, how do I make it so that it only gets loaded for tex files, so that I can use other <C-j> mappings for non-tex files?
The only sane solution would be for the maintainers of that plugin to move all their filetype-specific mappings to an ftplugin, where they belong. The way they currently implement them is a silly mix of good practices (<Plug>) and bad practices (global filetype-specific mappings), all in a single script under plugin/. This is very bad.
What they currently do:
" in plugin/imaps.vim
inoremap <silent> <Plug>IMAP_JumpForward <C-\><C-N>:call IMAP_Jumpfunc('', 0)<CR>
if !hasmapto('<Plug>IMAP_JumpForward', 'i')
imap <C-J> <Plug>IMAP_JumpForward
endif
The first mapping is okay-ish: plugin authors should use virtual :help <Plug> mappings as much as possible in order to allow users to write their own mappings easily. IMAP_JumpForward should be in parentheses, though.
The :help hasmapto() guard is pointless.
plugin/imaps.vim is a "global plugin". As such, whatever it does that is not explicitly scoped to a buffer or a window is done for every buffer and window. That <C-j> mapping only makes sense…
in the context of tex files, when using that latex-suite plugin,
if you actually use that imaps.vim plugin elsewhere.
What they should do:
" in plugin/imaps.vim
inoremap <silent> <Plug>(IMAP_JumpForward) <C-\><C-N>:call IMAP_Jumpfunc('', 0)<CR>
" in ftplugin/**/<somefile>.vim
imap <buffer> <C-J> <Plug>(IMAP_JumpForward)
Frankly, the way that thing is designed should raise all kinds of red flags. I don't do (la)tex at all but https://github.com/lervag/vimtex seems more competently done.
I am using vim-airline plugin which works pretty good however, the only way to switch between tabs is to user :bp or :bn which is shortcuts for :bprevious or :bnext. The problem with this is that if I am on first tab and want to switch to the last tab i.e. 10th tab then I have to type :bn ten times to get there. How can I switch directly? Maybe something pressing arrow keys would be beneficial.
Your problem is that you installed a plugin designed to obfuscate Vim's regular commands and workflows without even knowing those regular ways. The shortcut you took lead you to a dead-end so your only reasonable solution is to go back to the main road and learn Vim properly, without training wheels and crutches.
So… from your question, it seems you are using Airline's so-called "smarter tabline" which displays your open buffers in a fake tabline.
If it was a real tabline, they would actually be tab pages and you would move between them with their own set of commands.
But they are buffers and yes, you are supposed to move between them with these commands:
:bnext
:bprevious
:bfirst
:blast
:b10
:b <buffer-name>
which can all be mapped for your convenience, of course.
But… that plugin doesn't show buffer numbers, so you can't use :b10 to jump reliably to the tenth buffer in your fake "tabline" so that's one less tool in your tool-belt.
And some special buffers, like the quickfix list, can be reached with :bn/:bn without — probably — being listed in your fake "tabline" so that makes your fake "tabline" a rather poor abstraction, even without considering the glaring limitations of tabs in general.
And there's the idiosyncratic behavior of that fake "tabline" which becomes a semi-real "tabline" when you actually use tab pages.
Conflating two very different — and powerful in their own ways — concepts into a single bastardized one is not really a good idea.
I suggest you disable that option and use buffers and tab pages as they are meant to be used.
Reference:
:help buffers
:help tab-page
Agree with #romainl but you can always map your +tab to :bn or :bp for your ease.
" Tab navigation like Firefox.
nnoremap <C-S-tab> :bprevious<CR>
nnoremap <C-tab> :bnext<CR>
In addition to Rafi's answer, put this in your .vimrc to get direct access to a buffer / airline tab.
nmap <leader>1 :bfirst<CR>
nmap <leader>2 :bfirst<CR>:bn<CR>
nmap <leader>3 :bfirst<CR>:2bn<CR>
nmap <leader>4 :bfirst<CR>:3bn<CR>
...
Alternatively, you can double down on airline with
let g:airline#extensions#tabline#buffer_idx_mode = 1
nmap <leader>1 <Plug>AirlineSelectTab1
nmap <leader>2 <Plug>AirlineSelectTab2
if you are using vim-airline, I'm strongly suggested that to using AirlineSelectTab command, you can see it via :help airline, below is the configuration.
let g:airline#extensions#tabline#buffer_idx_mode = 1
nmap <leader>1 <Plug>AirlineSelectTab1
nmap <leader>2 <Plug>AirlineSelectTab2
nmap <leader>3 <Plug>AirlineSelectTab3
nmap <leader>4 <Plug>AirlineSelectTab4
nmap <leader>5 <Plug>AirlineSelectTab5
nmap <leader>6 <Plug>AirlineSelectTab6
nmap <leader>7 <Plug>AirlineSelectTab7
nmap <leader>8 <Plug>AirlineSelectTab8
nmap <leader>9 <Plug>AirlineSelectTab9
nmap <leader>0 <Plug>AirlineSelectTab0
nmap <leader>- <Plug>AirlineSelectPrevTab
nmap <leader>+ <Plug>AirlineSelectNextTab
If you are using tabs instead of buffers
" Tab navigation (works only in gvim, not in console)
nnoremap <C-tab> gt
nnoremap <C-S-tab> gT
I have configured vim to switch between tabs using Ctrl + arrow keys.
Ctrl + ← will switch to tab that is on the left of current tab.
Ctrl + → will switch to tab that is on the right of current tab.
Ctrl + ↑ will switch to first tab.
Ctrl + ↓ will switch to last tab.
To achieve above behaviour update your vimrc with following lines:
nnoremap <C-Left> :tabprevious<CR>
nnoremap <C-Right> :tabnext<CR>
nnoremap <C-Up> :tabfirst<CR>
nnoremap <C-Down> :tablast<CR>
This works with vim-airline plugin as well.
Reference: Using vim tab pages
I have map <F8> : w <bar> !clang -o %< % && ./%< <CR> inside my .vimrc, however I want to map F8 to run python codes as well. How is that possible? (obviously I don't want clang to run python, I want to have a condition or something to redirect what command F8 maps to based on the language that is already specified e.g. via :setf python etc.)
You're looking for filetype-specific mappings. Put the Python variant into ~/.vim/after/ftplugin/python.vim, and add the <buffer> keyword to the :map command:
nnoremap <buffer> <F8> : w <bar> !python % <CR>
This requires :filetype plugin on, but you probably already have that. Similarly, you can move your original mapping to ftplugin/c.vim, or keep that as a global fallback for all filetypes.
Additional tips
You should use :noremap; it makes the mapping immune to remapping and recursion.
Better specify the concrete modes this applies to, in this case normal mode via :n[nore]map.
Very doable. Here's an example where I have the same key mapped for tidying different types of file:
autocmd FileType perl nnoremap <buffer> <F12> mz:%!perltidy<CR>`z
autocmd FileType javascript nnoremap <buffer> <F12> :call JsBeautify()<CR>
It's been a while since I added this to my vimrc, but if memory and a quick google serves me well, this watched for the event of the fileype being perl or javascript, and then runs the nnoremap command for whichever event has occurred. I'm sure there are many other ways to accomplish it!
I normally work with more than 5 files at a time. I use buffers to open different files.
I use commands such as :buf file1, :buf file2 etc.
Is there a faster way to move to different files?
What I use:
Go to the previous buffer: :b# or :bp
Go to the next buffer: :bn
If you know your file is loaded in buffer 5: :b5
To get a list of buffers: :buffers or shorter: :ls
And have a short look on :he buffer
And the wiki entry on Easier Buffer Switching on the Vim Wiki: http://vim.wikia.com/wiki/Easier_buffer_switching
SO already has a question regarding yours: How do you prefer to switch between buffers in Vim?
A few mappings can make your life a lot easier.
This one lists your buffers and prompts you for a number:
nnoremap gb :buffers<CR>:buffer<Space>
This one lists your buffers in the "wildmenu". Depends on the 'wildcharm' option as well as 'wildmenu' and 'wildmode':
nnoremap <leader>b :buffer <C-z>
These ones allow you to cycle between all your buffers without much thinking:
nnoremap <PageUp> :bprevious<CR>
nnoremap <PageDown> :bnext<CR>
Also, don't forget <C-^> which allows you to alternate between two buffers.
Below I describe some excerpts from sections of my .vimrc. It includes mapping the leader key, setting wilds tab completion, and finally my buffer nav key choices (all mostly inspired by folks on the interweb, including romainl). Edit: Then I ramble on about my shortcuts for windows and tabs.
" easier default keys {{{1
let mapleader=','
nnoremap <leader>2 :#"<CR>
The leader key is a prefix key for mostly user-defined key commands (some plugins also use it). The default is \, but many people suggest the easier to reach ,.
The second line there is a command to # execute from the " clipboard, in case you'd like to quickly try out various key bindings (without relying on :so %). (My nmeumonic is that Shift-2 is #.)
" wilds {{{1
set wildmenu wildmode=list:full
set wildcharm=<C-z>
set wildignore+=*~ wildignorecase
For built-in completion, wildmenu is probably the part that shows up yellow on your Vim when using tab completion on command-line. wildmode is set to a comma-separated list, each coming up in turn on each tab completion (that is, my list is simply one element, list:full). list shows rows and columns of candidates. full's meaning includes maintaining existence of the wildmenu. wildcharm is the way to include Tab presses in your macros. The *~ is for my use in :edit and :find commands.
" nav keys {{{1
" windows, buffers and tabs {{{2
" buffers {{{3
nnoremap <leader>bb :b <C-z><S-Tab>
nnoremap <leader>bh :ls!<CR>:b<Space>
nnoremap <leader>bw :ls!<CR>:bw<Space>
nnoremap <leader>bt :TSelectBuffer<CR>
nnoremap <leader>be :BufExplorer<CR>
nnoremap <leader>bs :BufExplorerHorizontalSplit<CR>
nnoremap <leader>bv :BufExplorerVerticalSplit<CR>
nnoremap <leader>3 :e#<CR>
nmap <C-n> :bn<cr>
nmap <C-p> :bp<cr>
The ,3 is for switching between the "two" last buffers (Easier to reach than built-in Ctrl-6). Nmeuonic is Shift- 3 is #, and # is the register symbol for last buffer. (See :marks.)
,bh is to select from hidden buffers (!).
,bw is to bwipeout buffers by number or name. For instance, you can wipeout several while looking at the list, with ,bw 1 3 4 8 10 <CR>. Note that wipeout is more destructive than :bdelete. They have their pros and cons. For instance, :bdelete leaves the buffer in the hidden list, while :bwipeout removes global marks (see :help marks, and the description of uppercase marks).
I haven't settled on these keybindings, I would sort of prefer that my ,bb was simply ,b (simply defining while leaving the others defined makes Vim pause to see if you'll enter more).
Those shortcuts for :BufExplorer are actually the defaults for that plugin, but I have it written out so I can change them if I want to start using ,b without a hang.
You didn't ask for this:
If you still find Vim buffers a little awkward to use, try to combine the functionality with tabs and windows (until you get more comfortable?).
" windows {{{3
" window nav
nnoremap <leader>w <C-w>
nnoremap <M-h> <C-w>h
nnoremap <M-j> <C-w>j
nnoremap <M-k> <C-w>k
nnoremap <M-l> <C-w>l
" resize window
nnoremap <C-h> <C-w><
nnoremap <C-j> <C-w>+
nnoremap <C-k> <C-w>-
nnoremap <C-l> <C-w>>
Notice how nice ,w is for a prefix. Also, I reserve Ctrl key for resizing, because Alt (M-) is hard to realize in all environments, and I don't have a better way to resize. I'm fine using ,w to switch windows.
" tabs {{{3
nnoremap <leader>t :tab
nnoremap <M-n> :tabn<cr>
nnoremap <M-p> :tabp<cr>
nnoremap <C-Tab> :tabn<cr>
nnoremap <C-S-Tab> :tabp<cr>
nnoremap tn :tabe<CR>
nnoremap te :tabe<Space><C-z><S-Tab>
nnoremap tf :tabf<Space>
nnoremap tc :tabc<CR>
nnoremap to :tabo<CR>
nnoremap tm :tabm<CR>
nnoremap ts :tabs<CR>
nnoremap th :tabr<CR>
nnoremap tj :tabn<CR>
nnoremap tk :tabp<CR>
nnoremap tl :tabl<CR>
" or, it may make more sense to use
" nnoremap th :tabp<CR>
" nnoremap tj :tabl<CR>
" nnoremap tk :tabr<CR>
" nnoremap tl :tabn<CR>
In summary of my window and tabs keys, I can navigate both of them with Alt, which is actually pretty easy to reach. In other words:
" (modifier) key choice explanation {{{3
"
" KEYS CTRL ALT
" hjkl resize windows switch windows
" np switch buffer switch tab
"
" (resize windows is hard to do otherwise, so we use ctrl which works across
" more environments. i can use ',w' for windowcmds o.w.. alt is comfortable
" enough for fast and gui nav in tabs and windows. we use np for navs that
" are more linear, hjkl for navs that are more planar.)
"
This way, if the Alt is working, you can actually hold it down while you find your "open" buffer pretty quickly, amongst the tabs and windows.
Once the buffers are already open, you can just type :b partial_filename to switch
So if :ls shows that i have my ~./vimrc open, then I can just type :b vimr or :b rc to switch to that buffer
There are many ways to solve. The best is the best that WORKS for YOU. You have lots of fuzzy match plugins that help you navigate. The 2 things that impress me most are
1) CtrlP or Unite's fuzzy buffer search
2) LustyExplorer and/or LustyJuggler
And the simplest :
:map <F5> :ls<CR>:e #
Pressing F5 lists all buffer, just type number.
i just tried to install snipmate. supertab is already working.
it doesnt work. when i press tab even after a snippet keyword the completion is triggered.
i tried to remap the key (to c-m) but even then completion is triggered instaed of snipmate. (before the remap c-m didnt do a thing).
i did this mapping in vimrc. the doc tells me to do it in after/plugin/snipmate.vim but this script isnt loaded (according to :scriptnames).
any idea?
snipmate plugin hasn't been updated for a long long time. thats why i started a friendly fork and i'm merging and coordinating further development of this (at least for me) essential plugin.
more here: https://github.com/garbas/vim-snipmate/blob/master/README.md
I had a very similar problem running an old version of snipmate from vim.org
The guy who writes it doesn't always update the vim.org page, but he does work on it fairly regularly on github.
Grab the latest copy from the download link here and see if that helps you.
What did you remap to <C-m>? <Tab> or your plugins function calls? Did you try SnipMate without SuperTab?
Both plugins have their main functions hardwired to <Tab> which makes their use a bit unpredictable. I've tried it but I didn't like it.
Instead, maybe you can keep SnipMate (if it works on its own) and remap omnicompletion shortcuts to something easier. I have these in my .vimrc:
" change the mapleader from \ to ,
let mapleader=","
" omnicompletion : words
inoremap <leader>, <C-x><C-o>
" omnicompletion : filenames
inoremap <leader>: <C-x><C-f>
" omnicompletion : lines
inoremap <leader>= <C-x><C-l>
Also autocomplpop.vim works very well.
Use SuperTab 2.0 version.
Do not use the latest version 2.1, it doesn't work with each other.
I managed to remap SnippMate's TAB key to a different key, in my case c-j, and it worked OK:
ino <silent> <c-j><c-r>=TriggerSnippet()<cr>
snor <silent> <c-j> <esc>i<right><c-r>=TriggerSnippet()<cr>
I did the change in after/plugin/snipmate.vim. In my case, :scriptnames listed the file as loaded.
add it to .vimrc, when you use Tab, then show the list of snippets.
au BufRead,BufNewFile *.py set expandtab
au BufRead,BufNewFile *.sh set expandtab
au BufRead,BufNewFile *.go set expandtab