How to survive the transition from tabbed-based to buffer-based coding (Vim) - vim

I recently changed from notepad++ to Vim. In notepad++ I used to be aware of my open files by
seeing them as tabs, so when I wanted to close or change them I just pressed Shift-Tab or Ctrl-W. In Vim there are also tabs, but when I use them I feel like I'm just going back to my notepad++ way of managing my files. Is there a good way of list, manage, switch and delete buffers other than splitting them?

Yep. I recommend a buffer explorer plugin as well as learning buffer commands. I use this plugin.
http://www.vim.org/scripts/script.php?script_id=42
The buffer paradigm is quite elegant once you are used to it. Less visual clutter. but you are free with vim to find your own thing

You can use these commands:
ls - list all open buffers
bp, bn - switch to the previous or next buffer
b number - switch to the buffer with that number
b text - switch to the buffer whose name includes the string text

I've tried several setups for Vim. My previous was one where I tried to use tabs instead of buffers. It was not very satisfying.
Now I've returned to buffers and for navigating files and buffers I use only the NERDTree and fuzzyfinder (I guess ex Textmate users may prefer fuzzyfinder_textmate) plugins, both are great.
For locating files I use either NERDTree bound to n or fuzzyfinder's File mode. For navigating open buffers I use fuzzfinder's buffer mode solely, bound to b.
Recently I also discovered that I could switch to the previously open buffer with Ctrl-6 (I think maybe that is Ctrl-^ on most keyboard).

There are so many ways to deal with buffers in vim.
CTRL-^ to switch between buffers.
:q is the same as Ctrl-W
For further details see http://vim.wikia.com/wiki/Easier_buffer_switching

BufferExplorerLight
" quick buffer selection including unlisted
nnoremap <leader>b :buffers!<cr>:buffer<space>
Alternatively
nnoremap <leader>b :buffer<space> <c-d>
But really you have to try FuzzyFinder!

Related

VIM command to explore buffers

I often use the command :Explore to switch to another file. I also use a lot the command :buffer to switch between previously opened files, but it is not always convenient when a lot of files are opened.
Is there a way to display a list of all opened files (buffers) in the current window, in a "explore" way, without using plugin?
:help :ls is the closest you can get with basic built-in tools.
I would recommend creating a normal map like this in your ~/vimrc file.
" list buffers and jump to a chosen one
nnoremap <Leader>b :ls<CR>:b<Space>
It triggers your <Leader> plus b to execute two commands at once, first it shows all open buffers, then it allows you to type the buffer number to open it. It wort reading :h leader.

Vim NERDTree. How to prohibit duplicate files in tabs?

I use NERDTree with the setting:
""""
" NerdTree
"
Bundle 'scrooloose/nerdtree'
Bundle 'jistr/vim-nerdtree-tabs'
map <F2> :NERDTreeTabsToggle<CR>
I can open any number of tabs with the same file by pressing 't'. For example:
|foo.txt|bar.txt|foo.txt|foo.txt|
How to prevent the opening of duplicate files? I want to open an existing buffer by pressing 't'.
I found the solution here https://github.com/scrooloose/nerdtree/issues/439
Grab the latest version and stick this in
~/.vim/nerdtree_plugin/override_tab_mapping.vim
https://gist.github.com/scrooloose/0495cade24f1f2ebb602
Thanks #moeabdol
From what I understand NerdTree does not have such a behavior. I believe however what you are looking for is either :tab drop like #Ben mentioned or using :sb to switch buffers with the following setting: set swb=useopen,usetabe.
Personally I would suggest you use NerdTree for more of a File Explorer and less of a file/buffer manager. By leveraging Vim's buffer commands you can easily switch between buffers. Additionally by using Vim's buffer commands you can avoid the "one-to-one: file-to-tab relationship trap" that so many new vimmers get stuck on.
Aside about NerdTree
NerdTree is very helpful to explore a complex or unfamiliar file structure, but it comes at the cost of taking up screen real estate and disrupting buffer and window/split workflows. See Oil and vinegar - split windows and the project drawer for more. Using a nice fuzzyfinder plugin like CtrlP often takes the place of NerdTree for many people.
I have a nice post about NerdTree that might be of value: Files, Buffers, and Splits Oh My!
Aside about tabs
Vim's tabs are not like most text editors tab. They are more like viewports into a group of windows/splits. Additionally, Vim is buffer centric, not tab centric like most editors. Therefore using features like the quickfix list is often easier without tabs (See :h 'switchbuf if you must use tabs). Vim's tabs often get in the way of using a splits as there are better window and buffer navigation commands available. I personally have many files open (sometimes 100+) use no tabs and use on average 1-2 splits without any issue. Bottom line: read the following posts:
Why do Vim experts prefer buffers over tabs?
Use buffers effectively
Best practices with Vim mappings
Supply a mode. So :map becomes :nmap
Unless using a <Plug> or <SID> mapping you should probably be using :noremap
By following these 2 rules your mapping will become:
nnoremap <f2> :NERDTreeTabsToggle<cr>
to open a new buffer, just press o

Make vim commands work on initial window like NERDTree, MiniBufExplorer and CtrlP does

I have NERDTree and MiniBufExplorer open at the launch of vim, so I have three windows. Whenever I use the aforementioned plugins the files/buffers are presented in the correct window, which is the initial one. However whenever I use a command such as :e ~/.vimrc the command works on the window which contains the cursor. This means I have to always remember to move the cursor over to the window used for editing. I was wondering if there was a way to have commands work on that window regardless of where the cursor is, or if file buffers would automatically present themselves in that window?
I was looking at a way to have the cursor move over to the right window when : (or some other key I could use) is pressed, but I couldn't figure out a way as there is no way to identify windows (or is there?).
Files, Buffers, and Splits Oh My!
You are asking Vim to change how every single file and buffer command to change to accommodate your plugin choice and workflow. You are going against the vim way here and it will hinder your use of Vim's splits.
First things first is to realize that Vim has no concept of Project drawers, only splits/windows. This means that NerdTree and other plugins go to great lengths to emulate Project Drawer behavior and ultimately fail.
Lets look at problems with using NerdTree and MiniBufExplorer as always open windows:
NerdTree:
Wasted space. How often do you look at your file structure? 10% of the time? Less?
Splits - open up a few split now switch to the bottom right most split via <c-w>b. Open up a file via NerdTree in this window. Did you use <c-w>t to go to the top left most window? Feels like quite a nuance to use so many window commands just to open a file
NerdTree doesn't play well when rearranging splits. Create some splits then do <c-w>J or <c-w>H. See how it messed up your layout
MiniBufExplorer
Scale - MiniBufExplorer just doesn't scale with the number of buffers. I have opened up over a hundred buffers without issue. I can not imagine the waste of space this would cause with MiniBufExplorer
There is little to be gained by seeing all your open buffers all the time. You only need to see them when you are switching to a different buffer
Switching buffers - You can switch buffers just as easily by mapping the :bnext and :bprev commands
More on switching buffers - Moving to the MiniBufExplorer window is tedious and annoy if that is how you want to switch buffers
Rearranging windows - Same as NerdTree
MiniBufExplorer is akin to using Vim's tabs for each file in vim see: Use buffer effectively
The Vim Way
As laid out in the Vimcast post, Oil and vinegar - split windows and the project drawer, Vim prefers to just open a file explorer when you need it then switch away from it when it isn't needed. You can user NerdTree in this fashion too, just forget the alway on file explorer bit. There are other ways of opening files in vim:
Use file completion, via <tab>, with commands like :e and :sp
Use <c-d> instead of <tab> to get a list of completions
:e and :sp commands take globs. e.g. :e *.c and :e foo/**/bar.c
:find and setup 'path' and 'suffix' options
Ctags or cscope to jump to tags
gf will go to a file under the cursor
Look into fuzzy finders like CtrlP or Command-T
Create project specific navigation via Projectile (Rails is a good example of this)
There are plenty of ways to switch buffers in Vim:
:b and :sb take buffer numbers but also names that will complete and glob
Use :ls to see a list of your buffers then use :b to switch directly
<c-6> will go the the previous buffer
Map :bnext and :bprev example [b and ]b are Unimpaired.vim mappings
set hidden make switching buffers easier. Don't worry vim will let you know if you have unwritten buffer before exiting
Once again look into fuzzy finder plugsin like CtrlP and Command-T to switch buffers
Vim is split happy. Make sure you use splits as effectively as you can. There are many split commmands, see :h opening-window. Better yet read the whole :h window help file, there are many treasure in there.
The core of a solution can be found in the NERDTree source via s:Opener._firstUsableWindow, which I then edited some to get the id and include modified buffers:
" Returns the first window ID containing a file buffer
"
" Iterates through window numbers until the last (winnr('$')),
" Skipping special buffer types & preview windows
function! FirstFileWindowID()
let i = 1
while i <= winnr('$')
let bnum = winbufnr(i)
if bnum !=# -1 && getbufvar(bnum, '&buftype') ==# ''
\ && !getwinvar(i, '&previewwindow')
" TODO I don't know what excluding &hidden does in the original,
" but may be desirable for correctness
return win_getid(i)
endif
let i += 1
endwhile
return -1
endfunction
This can then be used with win_execute to target recent file buffers, gaining tab-like behavior with buffers:
"tab movement (ctrl-n for next tab, ctrl-p for previous)
map <c-n> :call win_execute(FirstFileWindowID(), 'bnext')<CR>
map <c-p> :call win_execute(FirstFileWindowID(), 'bprev')<CR>
I use tmux for all my pane management, so while I'm sure leveraging the full range of vim's tab / window / buffer paradigm is best for many, I personally don't use it to is fullest extent.

Can Vim NerdTree be used as a buffer explorer

I have been using vim NerdTree for sometime now. One thought came to my mind and i am not sure if it is possible so seeking the help of experts.
Can Nerdtree show the list of open buffers say something like :NerdtreeBuffers
I know there is BufferExplorer but i dont like it for the simple fact that i have to pre-define if i want the buffer in a tab/split/vsplit
Nerdtree is awesome ... i just need one command and then from the list i can open it however i want.
No, NERDTree doesn't have any buffer list/exploration properties. I like Buffergator (GitHub) for quick buffer listing & selection. By default, it operates similarly to NERDTree, opening a left vertical split and allowing you to navigate to a buffer and hit enter to jump to it. It also supports opening buffers in vertical/horizontal splits, and other basic functionality. CtrlP is also handy. It does fuzzy find on files, but can also limit itself to open buffers.

Edit multiple files without splitting window

I am new to VIM.
It seems like, in order for copy and paste functionality to work between different files, one must open the files in the same VIM instance. Doing this, however, splits the terminal screen into 2. Opening a new file, splits it into 3, etc.
what happens if i have to open, say, 10+ files? how do developers who use VIM deal with this issue?
You can open files in the same vim instance and in the same window (without spliting it). That's what is called buffers. The view you see is current buffer, but the others buffers are still opened.
Here you have an introduction on how to work in vim with buffers.
Or type :help buffers in vim.
You can use some plugin to work more user friendly with vim buffers. I recommend you bufexplorer or minibufexpl.
You can as well use tabs, but I have been always more confortable with buffers, but it's just my case.
Use tabedit instead of split to open more files.
Open all your txt files in tabs:
$ vim -p *.txt
Use gtgT to switch between tabs.
Or you can put these key-mappings in your .vimrc:
nmap <C-H> gT
nmap <C-L> gt
nmap <leader>t :tabnew<CR>
Most users use splits only for simultaneous viewing of two files, when it is needed. You can open as many files as you want in different buffers, while only displaying one buffer on screen.
:e File1.txt
:e File2.txt and so on ...
and then switch through buffers with :bprevious and :bnext (and a variety of other commands). That is really the Vim's way of work.
There are many plugins for manipulating and navigating buffers.
One tab per file is really the wrong way to go ...
You will find that VIM is a very flexible and customizable tool, so there are probably several approaches to this. Personally, I like to only have one buffer open at the time (I rarely need to split up) and use the Minibufexpl plugin to keep track of how many buffers are open and switch more efficiently between them.
If your only requirement is to copy paste between files. You can do this between different instances of vim if you set clipboard=unnamed
This causes vim to use the system clipboard instead of it's own internal buffer. so you can <C-c> from firefox and then p into vim, y from one vim p into another etc.
See http://vim.wikia.com/wiki/VimTip21
If you like having the files open in the same window I would use one of the other answers here and use either buffers (instead of splits) or tabs. I personally often have 10 or more buffers open in a single gvim window and 2 split windows that I use to view the buffer that I switch between using :bn and :bp.

Resources