NERDTree live-preview (like sublime sidebar) - vim

Sublime's sidebar has a cool feature, where I can just press the arrow keys and get a quick glance of what each file looks like in the editor pane. It doesn't actually open the file -- just shows it in the editor pane.
I want to do the same thing with NERDTree in Vim (or Vinegar/netrw, doesn't really matter). I know NERDTree lets me use go to open the file under the cursor while keeping the tree in focus, but (a) that requires two keystrokes, and (b) it creates a new buffer for every file I "preview" like this, so... not much of a preview really.
Is there a way to make NERDTree or Vim emulate this Sublime feature?

Yes, there is. Vim has a feature called "preview window". You can open a file in the preview window with :pedit <filename>. If you want to plug this into NERDTree, you could create a file in the ~/.vim/nerdtree_plugin/ directory, for example "live_preview_mapping.vim", with the following contents:
if exists("g:loaded_nerdree_live_preview_mapping")
finish
endif
let g:loaded_nerdree_live_preview_mapping = 1
call NERDTreeAddKeyMap({
\ 'key': '<up>',
\ 'callback': 'NERDTreeLivePreview',
\ 'quickhelpText': 'preview',
\ })
function! NERDTreeLivePreview()
" Get the path of the item under the cursor if possible:
let current_file = g:NERDTreeFileNode.GetSelected()
if current_file == {}
return
else
exe 'pedit '.current_file.path.str()
endif
endfunction
The first part is simply a load guard, so the file is sourced only once, just boilerplate. The second part adds a keymap using the NERDTree API for the <up> key that calls the given callback function.
The callback function is the meat of the code, but it should be fairly easy to understand -- it takes the node under the cursor, if there is one, and executes a :pedit with the filename.
You can even do this more easily with a simple filetype-specific mapping, something like this:
autocmd FileType nerdtree nnoremap <buffer> <up> :call NERDTreeLivePreview()<cr>
But the former is the approach recommended by the plugin (see :help NERDTreeAPI). If nothing else, this adds a help entry to the ? key for it, and it keeps nerdtree extensions in one place.
For more info on what you can do with the preview window, try :help preview-window. For instance, you can close it with <c-w>z, but you can map that to whatever you'd like, that's not really related to the NERDTree anymore. If you're unhappy with where the window shows up, consider changing the "pedit" to "botright pedit" or "leftabove pedit" or whatever you want. Check the help for :leftabove and take a look at the related commands below.

NERDTree doesn't offer anything automatic out of the box. I like a preview window that hijacks the last active window and allows for opening the buffer there, or to split with the original buffer. This extension does that, and its source code is pretty short.
https://github.com/numEricL/nerdtree-live-preview

With netrw, to preview a file: with the cursor atop a file, press "p".

Related

How can I explain this behaviour in Vim?

Vim is so awesome. For example, you have a file, called 'test0.html', stored in a folder.
In the same folder, you store the folder 'test' with the files test1.html, and test2.html.
Then you put in test0.html the following content:
include('test/test1.html');
include('test/test2.html');
In vim, you put the cursor on the filenames. You open the files under the corsor with the keys gf. Thats why Vim is so awesome.
I would like to open in a new tab. That's possible with the keys gF.
But what if you want to stay in the same file, but open the file in a background tab, like Chrome does?
So I'm mapping the key.
noremap gf <c-w>gF<c-PageDown>
So, when my cursor is on test1.html, it open with the key gf in a background tab. Wonderful, now I'm a satisfied man.
Then I want to open test2.html under cursor.
Vim jumps to the tab of test1.html, instead stay on test0.html
When I tried to debug this weird behaviour, by only mapping gf to gF, and then do manual CTRL+pagedown, I get the expected behaviour.
My guess is that Vim is much faster with executing the command before he opens the new tab (gF), and so I get to the last tab from the first tab.
Am I correct in my guess explaination?
<c-PageDown> or more commonly used gT will got to the previous tab. <c-w>gF on the other hand will open the file under the cursor in a new tab. That tab will be last tab. So doing a gT will not always make you go back to the previous tab.
You can change your mapping to go back to the previous tab like so:
nnoremap gf :execute "normal! \<lt>c-w>gF" . tabpagenr() . "gt"<cr>
However I would personally suggest you avoid using tabs in such a manner and use buffers instead.
noremap gf :tabe<cfile><CR><c-PageUp>
This is even better. When the file doesn't exist, Vim will create a new one.

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.

How can I execute a command when a buffer is closed in vim?

Is there something like a close-window-hook in vim/vimscript, so that I can call a function every time a window is closed?
I want to use it for the following scenario:
I use an extra scratch window to display information about the file, and when I close the file I want the scratch window to be closed automatically so that vim exits.
If you have any ideas how to achieve that without a hook that will be just as fine.
edit:
I know about :qa[ll], but I am lazy and only want to type :q or ZZ.
edit2.71828183:
I accepted the autocommand answer as it was closest to the original question, but found another solution in using a preview window instead of a split window. A preview window is automatically closed when the last "normal" window is closed..
autocommands are amazing! I'm pretty sure that in this case, BufLeave will do the job, but you might want BufWinLeave? Have a look at :help autocmd-events for a full list of events.
The other bit you'll care about is: you can have buffer-local autocommands! (:help autocmd-buflocal)
You can define one for the current buffer using au BufLeave <buffer> .... My best guess is that you could run this in whatever command creates the scratch window. You should be able to cache the scratch window's buffer number in a global variable when you open the scratch window, then your autocommand could just delete that buffer (:help :bdelete).
au BufLeave <buffer> bdelete g:scratch_buffer
call CreateScratchWindow()
function CreateScratchWindow() {
...
let g:scratch_buffer = bufnr("")
}
There's also a function winbufnr, for getting buffer numbers by window. You can use either one - just make sure that the scratch window/buffer is current when you use it! (The "" means current window/buffer).
Add a mapping.
:nnoremap :q :qa
then, if you only want to use :q one day, you can just
:qa<backspace>
and normally, you can just
:q<enter>
and get
:qa<enter>
for free.
Ok, not really a new question, but I found it looking for, what I now know, BufWinLeave. However, the scenario the author is describing is something I have in my vimrc with NerdTree, e.g. when the last buffer is closed, I want vim to close the NerdTree "buffer" and then quit.
autocmd bufenter * if (winnr("$") == 1 && exists("b:NERDTree") && b:NERDTree.isTabTree()) | q | endif
Replace the NERDTree specific stuff with how the scratch buffer is identified.
You can use
:quitall
or
:qall
to close all windows at once.
add a ! if you want to ignore unsaved changes.
:qall!

Vim file navigation

I'm trying really hard to learn vim after using TextMate for the last few years.
I've started to commit some of the in-file navigation to memory but I'm struggling with navigating between multiple files.
In my workflow it is pretty common that I'm flipping between a handful of files pretty regularly (enough files such that split-pane windows become too small).
I'm currently using NERDTree but find drilling down into directories cumbersome as well as constantly using CTRL+W h/CTRL+W l to hop back and forth.
I think I would do better with tabs I can easily toggle between but maybe I need to use a different workflow.
I'd also like a "Go to File..." shortcut like CMD+T in TextMate. I've found fuzzy_file_finder but it requires vim to be built with Ruby bindings which isn't the case the native installs I've worked on.
While I could rebuild the main reason I want to switch to vim is so I can have one editor environment that I know will easily work across any platform.
An easy way to browse the file system is the command:
:Sex
I'm not making this up :)
I don't find drilling down into subdirectories via plain old :e to be that cumbersome given a decent configuration for tab-completion.
Look into the 'wildmenu' option to have Vim show a list of completions (filenames) in the modeline above the commandline. You can change the 'wildmode' option to further configure the kind of tab-completion Vim will do.
Personally I use :set wildmode=full.
My workflow is like this:
:cd into the toplevel directory of my project.
To open file foo/bar/baz:
Simplest scenario: type :e f<tab>b<tab>b<tab><enter>.
If there are more than one file starting with b in one of those directories you might have to do a <left> or <right> or another <tab> on the keyboard to jump between them (or type a few more letters to disambiguate).
Worst-case scenario there are files and directories that share a name and you need to drill down into the directory. In this case tab-complete the directory name and then type *<tab> to drill down.
Open 2 or 3 windows and open files in all of them as needed.
Once a file is open in a buffer, don't kill the buffer. Leave it open in the background when you open new files. Just :e a new file in the same window.
Then, use :b <tab> to cycle through buffers that are already open in the background. If you type :b foo<tab> it will match only against currently-open files that match foo.
I also use these mappings to make it easier to open new windows and to jump between them because it's something I do so often.
" Window movements; I do this often enough to warrant using up M-arrows on this"
nnoremap <M-Right> <C-W><Right>
nnoremap <M-Left> <C-W><Left>
nnoremap <M-Up> <C-W><Up>
nnoremap <M-Down> <C-W><Down>
" Open window below instead of above"
nnoremap <C-W>N :let sb=&sb<BAR>set sb<BAR>new<BAR>let &sb=sb<CR>
" Vertical equivalent of C-w-n and C-w-N"
nnoremap <C-w>v :vnew<CR>
nnoremap <C-w>V :let spr=&spr<BAR>set nospr<BAR>vnew<BAR>let &spr=spr<CR>
" I open new windows to warrant using up C-M-arrows on this"
nmap <C-M-Up> <C-w>n
nmap <C-M-Down> <C-w>N
nmap <C-M-Right> <C-w>v
nmap <C-M-Left> <C-w>V
It takes me a matter of seconds to open Vim, set up some windows and open a few files in them. Personally I have never found any of the third-party file-browsing scripts to be very useful.
:Sex,:Vex, :Tex and :Ex are all useful commands for ex(ploring) the files on your system if you want to use something different from :e
(where S/V/T are short for Split/Vertical/Tab)
By far the best and fastest plugin I found for file navigation is fzf.vim. You can very quickly fuzzy search all your files, the open buffers and even the files contents.
Since we have a very large codebase at work I specified a couple of directories I use most as the directories fzf searches. Fzf even has a rich git integration. So you can search only tracked files.
This is how the file search looks:
and his is how the content search looks:
If you haven't found them already, you might want to check out:
the original fuzzy finder plugin -- which IIRC doesn't have the Ruby binding issues
the Project plugin -- similar to NERDTree
the buffer explorer plugin -- shows a list of open buffers
Also bear in mind that you can remap key shortcuts in your .vimrc to make them less cumbersome. I do use split windows a lot; I've found the following make dealing with them much easier:
" set your own personal modifier key to something handy
let mapleader = ","
" use ,v to make a new vertical split, ,s for horiz, ,x to close a split
noremap <leader>v <c-w>v<c-w>l
noremap <leader>s <c-w>s<c-w>j
noremap <leader>x <c-w>c
" use ctrl-h/j/k/l to switch between splits
map <c-j> <c-w>j
map <c-k> <c-w>k
map <c-l> <c-w>l
map <c-h> <c-w>h
It is surprising to see that :find command has not been mentioned in any of the answers. I usually work with rails projects where I need to drill down deep into the directory hierarchy. So for a file application.css, located in app/assets/stylesheets/ all I have to type is :find applica and then press tab to autocomplete and enter to open.
I think I would do better with tabs I
can easily toggle between, but maybe I
need to use a different workflow.
Are you aware of Vim's tabs? Not sure if you were referring to Vim's own tabs there, or pining for TextMate's. It's unclear what Vim version you're using but it's had tabs since at least 7.0 (which seems to be installed everywhere I look lately), and they're awesome.
:tabe opens a new one (short for "tab edit", so e.g. :tabe for empty or :tabe path/to/file for opening a file in a new tab,) you can move between adjacent tabs with gt/gT, and there's a bunch of other tab-related nav commands too, as you might expect from Vim (:help tabs)
My workflow for large projects tends to involve a bunch of tabs, each with between 1 and 3 windows depending on what sort of context I need. This works even better if I have a doublewide terminal window since I can vertically split with :vs, so then a single tab can easily show me 4 files at once with plenty of room for each.
Final tip: I sometimes use the "switch buffer" command (:sb <partial_buf_name>) which is sorta-kinda like TM's fuzzy finding, in that it works pretty well but only for already-open buffers. Still great for when I have a dozen or more files open at once. (N.B. I needed to :set switchbuf=usetab in my vimrc for this to work across tabs, but once that's set it's great.)
I find LustyExplorer the best so far.
You can open a file by typing only a part of its name.
You can open a file from the directory of the current file, or the current pwd, or from the buffer list.
If you open a file from the buffer list, you don't have to navigate through the directory tree, though unfortunately for the other two modes, you still have to navigate through the tree. The good thing is you don't have to start from the first character.
If you have:
dir_a/
...
dir_b/
file.js
file.html
and you want to open dir_b/file.html you can generally find it by typing b<Tab>h
Check out the plugin ctrlp as an alternative to the plugin Command-T. It's better than this in that it is a 'native' Vim script and doesnt need Vim to be compiled with ruby (or other support) while it runs very fast and unobtrusive.
If the file that you want is already loaded into a buffer (which is likely if you are flipping between a handful of files regularly) then you can quickly switch to the buffer with the :b[uffer] command.
:b can either take a buffer number as a parameter, or (more usefully) a string that matches part of the path/filename. If there are multiple matches you can use tab to cycle through them.
If you want to split the window and open the buffer in the new window then use :sb name
If you want to open the buffer in a new tab then use :tab b name
You can also use the :ls command to see a list of currently loaded buffers.
I use this function for finding files in subdirectories, I didn't write it though. It's from the vim wiki:
function! Find(name)
let l:list=system("find . -name '".a:name."' | grep -v \".svn/\" | perl -ne 'print \"$.\\t$_\"'")
let l:num=strlen(substitute(l:list, "[^\n]", "", "g"))
if l:num 0
echo "Not a number"
return
endif
if l:inputl:num
echo "Out of range"
return
endif
let l:line=matchstr("\n".l:list, "\n".l:input."\t[^\n]*")
else
let l:line=l:list
endif
let l:line=substitute(l:line, "^[^\t]*\t./", "", "")
execute ":e ".l:line
endfunction
command! -nargs=1 Find :call Find("")
One thing I like is it has support for wildcards (*). It's also well behaved for multiple matches.
In addition to the wildmenu answers, I use BufExplorer plugin, and the following mappings to quickly jump buffers:
nmap <A-1> :b 1<CR>
nmap <A-2> :b 2<CR>
nmap <A-3> :b 3<CR>
...
nmap <A-0> :b 10<CR>
nmap <A-=> \bs
Or you can just replace the buffer explorer with the :ls command, which basically display the same thing.
The last one maps the Alt-= to show the buffer explorer.
I don't use Windows that much, as I generally have one or two windows only.
I also modified the statusline to include the buffer number %2n, so that the I always know which buffer is being edited. See :h statusline
do you know MacVim application? It is VIM polished for OS X, and one of its feature is support for tabs (CMD+T to open new tab), it have drawer, etc... (read this: http://zerokspot.com/weblog/2008/08/03/macvim-as-textmate-replacement/)
Try it!
Regards
Michal
NerdTree is best for file navigation in vim.
The link below is a good introduction to nerdtree.
http://www.catonmat.net/blog/vim-plugins-nerdtree-vim/
I often do:
:e ./
in NORMAL MODE to see the content of the current folder and then navigate to any other folder or file.
Here's something fairly trivial that I stick in my .vimrc. It will load a file from any subdirectory.
" load from an unspecified subdirectory
function! LoadFromSubdirectory(filespec)
let filename = glob("`find . -name '" . a:filespec . "' -print`")
exe ':e '.filename
endfunction
:command -nargs=1 Er :call LoadFromSubdirectory(<f-args>)
So I can just say:
:Er Main.java
and it will search subdirectories, find Main.java and load it. It's not well behaved for multiple files of the same name, but it's quick and simple.
The other tool I use is VTreeExplorer, which provides a tree-based window of files/directories, and doesn't require any special build options from VIM (i.e. it's a vanilla plugin).
My workflow for finding files is the wildmenu, autocwd and :e.
Relevant parts in the .vimrc:
set wildmenu
set wildignore=*.dll,*.o,*.pyc,*.bak,*.exe,*.jpg,*.jpeg,*.png,*.gif
set wildmode=list:full
set autochdir
let g:netrw_list_hide='^\.,.\(pyc\|pyo\|o\)$'
And how to use it:
:e myf^D
That gives all the files in the current directory that start with myf. You can also <Tab> through them. Use return to open :)
^D will always give you all the matches. Because autocmd always goes to the folder of the current file it's easy to work with. If you are in foo/bar.py and you want to go to foo/baz.py you only do :e baz.py and you're there. That also works with buffers (:b foo^D lists all buffers starting with foo)

How to effectively work with multiple files in Vim

I've started using Vim to develop Perl scripts and am starting to find it very powerful.
One thing I like is to be able to open multiple files at once with:
vi main.pl maintenance.pl
and then hop between them with:
:n
:prev
and see which file are open with:
:args
And to add a file, I can say:
:n test.pl
which I expect would then be added to my list of files, but instead it wipes out my current file list and when I type :args I only have test.pl open.
So how can I add and remove files in my args list?
Why not use tabs (introduced in Vim 7)?
You can switch between tabs with :tabn and :tabp,
With :tabe <filepath> you can add a new tab; and with a regular :q or :wq you close a tab.
If you map :tabn and :tabp to your F7/F8 keys you can easily switch between files.
If there are not that many files or you don't have Vim 7 you can also split your screen in multiple files: :sp <filepath>. Then you can switch between splitscreens with Ctrl+W and then an arrow key in the direction you want to move (or instead of arrow keys, w for next and W for previous splitscreen)
Listing
To see a list of current buffers, I use:
:ls
Opening
To open a new file, I use
:e ../myFile.pl
with enhanced tab completion (put set wildmenu in your .vimrc).
Note: you can also use :find which will search a set of paths for you, but you need to customize those paths first.
Switching
To switch between all open files, I use
:b myfile
with enhanced tab completion (still set wildmenu).
Note: :b# chooses the last visited file, so you can use it to switch quickly between two files.
Using windows
Ctrl-W s and Ctrl-W v to split the current window horizontally and vertically. You can also use :split and :vertical split (:sp and :vs)
Ctrl-W w to switch between open windows, and Ctrl-W h (or j or k or l) to navigate through open windows.
Ctrl-W c to close the current window, and Ctrl-W o to close all windows except the current one.
Starting vim with a -o or -O flag opens each file in its own split.
With all these I don't need tabs in Vim, and my fingers find my buffers, not my eyes.
Note: if you want all files to go to the same instance of Vim, start Vim with the --remote-silent option.
:ls
for list of open buffers
:bp previous buffer
:bn next buffer
:bn (n a number) move to n'th buffer
:b <filename-part> with tab-key providing auto-completion (awesome !!)
In some versions of vim, bn and bp are actually bnext and bprevious respectively. Tab auto-complete is helpful in this case.
Or when you are in normal mode, use ^ to switch to the last file you were working on.
Plus, you can save sessions of vim
:mksession! ~/today.ses
The above command saves the current open file buffers and settings to ~/today.ses. You can load that session by using
vim -S ~/today.ses
No hassle remembering where you left off yesterday. ;)
To add to the args list:
:argadd
To delete from the args list:
:argdelete
In your example, you could use :argedit test.pl to add test.pl to the args list and edit the file in one step.
:help args gives much more detail and advanced usage
I use buffer commands - :bn (next buffer), :bp (previous buffer) :buffers (list open buffers) :b<n> (open buffer n) :bd (delete buffer). :e <filename> will just open into a new buffer.
I think you may be using the wrong command for looking at the list of files that you have open.
Try doing an :ls to see the list of files that you have open and you'll see:
1 %a "./checkin.pl" line 1
2 # "./grabakamailogs.pl" line 1
3 "./grabwmlogs.pl" line 0
etc.
You can then bounce through the files by referring to them by the numbers listed, e.g.
:3b
or you can split your screen by entering the number but using sb instead of just b.
As an aside % refers to the file currently visible and # refers to the alternate file.
You can easily toggle between these two files by pressing Ctrl Shift 6
Edit: like :ls you can use :reg to see the current contents of your registers including the 0-9 registers that contain what you've deleted. This is especially useful if you want to reuse some text that you've previously deleted.
Vim (but not the original Vi!) has tabs which I find (in many contexts) superior to buffers. You can say :tabe [filename] to open a file in a new tab. Cycling between tabs is done by clicking on the tab or by the key combinations [n]gt and gT. Graphical Vim even has graphical tabs.
Things like :e and :badd will only accept ONE argument, therefore the following will fail
:e foo.txt bar.txt
:e /foo/bar/*.txt
:badd /foo/bar/*
If you want to add multiple files from within vim, use arga[dd]
:arga foo.txt bar.txt
:arga /foo/bar/*.txt
:argadd /foo/bar/*
Many answers here! What I use without reinventing the wheel - the most famous plugins (that are not going to die any time soon and are used by many people) to be ultra fast and geeky.
ctrlpvim/ctrlp.vim - to find file by name fuzzy search by its location or just its name
jlanzarotta/bufexplorer - to browse opened buffers (when you do not remember how many files you opened and modified recently and you do not remember where they are, probably because you searched for them with Ag)
rking/ag.vim to search the files with respect to gitignore
scrooloose/nerdtree to see the directory structure, lookaround, add/delete/modify files
EDIT: Recently I have been using dyng/ctrlsf.vim to search with contextual view (like Sublime search) and I switched the engine from ag to ripgrep. The performance is outstanding.
EDIT2: Along with CtrlSF you can use mg979/vim-visual-multi, make changes to multiple files at once and then at the end save them in one go.
Some answers in this thread suggest using tabs and others suggest using buffer to accomplish the same thing. Tabs and Buffers are different. I strongly suggest you read this article "Vim Tab madness - Buffers vs Tabs".
Here's a nice summary I pulled from the article:
Summary:
A buffer is the in-memory text of a file.
A window is a viewport on a buffer.
A tab page is a collection of windows.
To change all buffers to tab view.
:tab sball
will open all the buffers to tab view. Then we can use any tab related commands
gt or :tabn " go to next tab
gT or :tabp or :tabN " go to previous tab
details at :help tab-page-commands.
We can instruct vim to open ,as tab view, multiple files by vim -p file1 file2.
alias vim='vim -p' will be useful.
The same thing can also be achieved by having following autocommand in ~/.vimrc
au VimEnter * if !&diff | tab all | tabfirst | endif
Anyway to answer the question:
To add to arg list: arga file,
To delete from arg list: argd pattern
More at :help arglist
When using multiple files in vim, I use these commands mostly (with ~350 files open):
:b <partial filename><tab> (jump to a buffer)
:bw (buffer wipe, remove a buffer)
:e <file path> (edit, open a new buffer>
pltags - enable jumping to subroutine/method definitions
You may want to use Vim global marks.
This way you can quickly bounce between files, and even to the marked location in the file. Also, the key commands are short:
'C takes me to the code I'm working with,
'T takes me to the unit test I'm working with.
When you change places, resetting the marks is quick too:
mC marks the new code spot,
mT marks the new test spot.
If using only vim built-in commands, the best one that I ever saw to switch among multiple buffers is this:
nnoremap <Leader>f :set nomore<Bar>:ls<Bar>:set more<CR>:b<Space>
It perfectly combines both :ls and :b commands -- listing all opened buffers and waiting for you to input the command to switch buffer.
Given above mapping in vimrc, once you type <Leader>f,
All opened buffers are displayed
You can:
Type 23 to go to buffer 23,
Type # to go to the alternative/MRU buffer,
Type partial name of file, then type <Tab>, or <C-i> to autocomplete,
Or just <CR> or <Esc> to stay on current buffer
A snapshot of output for the above key mapping is:
:set nomore|:ls|:set more
1 h "script.py" line 1
2 #h + "file1.txt" line 6 -- '#' for alternative buffer
3 %a "README.md" line 17 -- '%' for current buffer
4 "file3.txt" line 0 -- line 0 for hasn't switched to
5 + "/etc/passwd" line 42 -- '+' for modified
:b '<Cursor> here'
In the above snapshot:
Second column: %a for current, h for hidden, # for previous, empty for hasn't been switched to.
Third column: + for modified.
Also, I strongly suggest set hidden. See :help 'hidden'.
I use the same .vimrc file for gVim and the command line Vim. I tend to use tabs in gVim and buffers in the command line Vim, so I have my .vimrc set up to make working with both of them easier:
" Movement between tabs OR buffers
nnoremap L :call MyNext()<CR>
nnoremap H :call MyPrev()<CR>
" MyNext() and MyPrev(): Movement between tabs OR buffers
function! MyNext()
if exists( '*tabpagenr' ) && tabpagenr('$') != 1
" Tab support && tabs open
normal gt
else
" No tab support, or no tabs open
execute ":bnext"
endif
endfunction
function! MyPrev()
if exists( '*tabpagenr' ) && tabpagenr('$') != '1'
" Tab support && tabs open
normal gT
else
" No tab support, or no tabs open
execute ":bprev"
endif
endfunction
This clobbers the existing mappings for H and L, but it makes switching between files extremely fast and easy. Just hit H for next and L for previous; whether you're using tabs or buffers, you'll get the intended results.
If you are going to use multiple buffers, I think the most important thing is to
set hidden
so that it will let you switch buffers even if you have unsaved changes in the one you are leaving.
I use the following, this gives you lots of features that you'd expect to have in other editors such as Sublime Text / Textmate
Use buffers not 'tab pages'. Buffers are the same concept as tabs in almost all other editors.
If you want the same look of having tabs you can use the vim-airline plugin with the following setting in your .vimrc: let g:airline#extensions#tabline#enabled = 1. This automatically displays all the buffers as tab headers when you have no tab pages opened
Use Tim Pope's vim-unimpaired which gives [b and ]b for moving to previous/next buffers respectively (plus a whole host of other goodies)
Have set wildmenu in your .vimrc then when you type :b <file part> + Tab for a buffer you will get a list of possible buffers that you can use left/right arrows to scroll through
Use Tim Pope's vim-obsession plugin to store sessions that play nicely with airline (I had lots of pain with sessions and plugins)
Use Tim Pope's vim-vinegar plugin. This works with the native :Explore but makes it much easier to work with. You just type - to open the explorer, which is the same key as to go up a directory in the explorer. Makes navigating faster (however with fzf I rarely use this)
fzf (which can be installed as a vim plugin) is also a really powerful fuzzy finder that you can use for searching for files (and buffers too). fzf also plays very nicely with fd (a faster version of find)
Use Ripgrep with vim-ripgrep to search through your code base and then you can use :cdo on the results to do search and replace
My way to effectively work with multiple files is to use tmux.
It allows you to split windows vertically and horizontally, as in:
I have it working this way on both my mac and linux machines and I find it better than the native window pane switching mechanism that's provided (on Macs). I find the switching easier and only with tmux have I been able to get the 'new page at the same current directory' working on my mac (despite the fact that there seems to be options to open new panes in the same directory) which is a surprisingly critical piece. An instant new pane at the current location is amazingly useful. A method that does new panes with the same key combos for both OS's is critical for me and a bonus for all for future personal compatibility.
Aside from multiple tmux panes, I've also tried using multiple tabs, e.g. and multiple new windows, e.g. and ultimately I've found that multiple tmux panes to be the most useful for me. I am very 'visual' and like to keep my various contexts right in front of me, connected together as panes.
tmux also support horizontal and vertical panes which the older screen didn't (though mac's iterm2 seems to support it, but again, the current directory setting didn't work for me). tmux 1.8
In my and other many vim users, the best option is to,
Open the file using,
:e file_name.extension
And then just Ctrl + 6 to change to the last buffer. Or, you can always press
:ls to list the buffer and then change the buffer using b followed by the buffer number.
We make a vertical or horizontal split using
:vsp for vertical split
:sp for horizantal split
And then <C-W><C-H/K/L/j> to change the working split.
You can ofcourse edit any file in any number of splits.
I use the command line and git a lot, so I have this alias in my bashrc:
alias gvim="gvim --servername \$(git rev-parse --show-toplevel || echo 'default') --remote-tab"
This will open each new file in a new tab on an existing window and will create one window for each git repository.
So if you open two files from repo A, and 3 files from repo B, you will end up with two windows, one for repo A with two tabs and one for repo B with three tabs.
If the file you are opening is not contained in a git repo it will go to a default window.
To jump between tabs I use these mappings:
nmap <C-p> :tabprevious<CR>
nmap <C-n> :tabnext<CR>
To open multiple files at once you should combine this with one of the other solutions.
I use multiple buffers that are set hidden in my ~/.vimrc file.
The mini-buffer explorer script is nice too to get a nice compact listing of your buffers. Then :b1 or :b2... to go to the appropriate buffer or use the mini-buffer explorer and tab through the buffers.
have a try following maps for convenience editing multiple files
" split windows
nmap <leader>sh :leftabove vnew<CR>
nmap <leader>sl :rightbelow vnew<CR>
nmap <leader>sk :leftabove new<CR>
nmap <leader>sj :rightbelow new<CR>
" moving around
nmap <C-j> <C-w>j
nmap <C-k> <C-w>k
nmap <C-l> <C-w>l
nmap <C-h> <C-w>h
I made a very simple video showing the workflow that I use. Basically I use the Ctrl-P Vim plugin, and I mapped the buffer navigation to the Enter key.
In this way I can press Enter in normal mode, look at the list of open files (that shows up in a small new window at the bottom of the screen), select the file I want to edit and press Enter again. To quickly search through multiple open files, just type part of the file name, select the file and press Enter.
I don't have many files open in the video, but it becomes incredibly helpful when you start having a lot of them.
Since the plugin sorts the buffers using a MRU ordering, you can just press Enter twice and jump to the most recent file you were editing.
After the plugin is installed, the only configuration you need is:
nmap <CR> :CtrlPBuffer<CR>
Of course you can map it to a different key, but I find the mapping to enter to be very handy.
I would suggest using the plugin
NERDtree
Here is the github link with instructions.
Nerdtree
I use vim-plug as a plugin manager, but you can use Vundle as well.
vim-plug
Vundle
When I started using VIM I didn't realize that tabs were supposed to be used as different window layouts, and buffer serves the role for multiple file editing / switching between each other. Actually in the beginning tabs are not even there before v7.0 and I just opened one VIM inside a terminal tab (I was using gnome-terminal at the moment), and switch between tabs using alt+numbers, since I thought using commands like :buffers, :bn and :bp were too much for me. When VIM 7.0 was released I find it's easier to manager a lot of files and switched to it, but recently I just realized that buffers should always be the way to go, unless one thing: you need to configure it to make it works right.
So I tried vim-airline and enabled the visual on-top tab-like buffer bar, but graphic was having problem with my iTerm2, so I tried a couple of others and it seems that MBE works the best for me. I also set shift+h/l as shortcuts, since the original ones (moving to the head/tail of the current page) is not very useful to me.
map <S-h> :bprev<Return>
map <S-l> :bnext<Return>
It seems to be even easier than gt and gT, and :e is easier than :tabnew too. I find :bd is not as convenient as :q though (MBE is having some problem with it) but I can live with all files in buffer I think.
Most of the answers in this thread are using plain vim commands which is of course fine but I thought I would provide an extensive answer using a combination of plugins and functions that I find particularly useful (at least some of these tips came from Gary Bernhardt's file navigation tips):
To toggle between the last two file just press <leader> twice. I recommend assigning <leader> to the spacebar:
nnoremap <leader><leader> <c-^>
For quickly moving around a project the answer is a fuzzy matching solution such as CtrlP. I bind it to <leader>a for quick access.
In the case I want to see a visual representation of the currently open buffers I use the BufExplorer plugin. Simple but effective.
If I want to browse around the file system I would use the command line or an external utility (Quicklsilver, Afred etc.) but to look at the current project structure NERD Tree is a classic. Do not use this though in the place of 2 as your main file finding method. It will really slow you down. I use the binding <leader>ff.
These should be enough for finding and opening files. From there of course use horizontal and vertical splits. Concerning splits I find these functions particularly useful:
Open new splits in smaller areas when there is not enough room and expand them on navigation. Refer here for comments on what these do exactly:
set winwidth=84
set winheight=5
set winminheight=5
set winheight=999
nnoremap <C-w>v :111vs<CR>
nnoremap <C-w>s :rightbelow split<CR>
set splitright
Move from split to split easily:
nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>
if you're on osx and want to be able to click on your tabs, use MouseTerm and SIMBL (taken from here). Also, check out this related discussion.
You can be an absolute madman and alias vim to vim -p by adding in your .bashrc:
alias vim="vim -p"
This will result in opening multiple files from the shell in tabs, without having to invoke :tab ball from within vim afterwards.
To open 2 or more files with vim type: vim -p file1 file2
After that command to go threw that files you can use CTRL+Shift+↑ or ↓ , it will change your files in vim.
If u want to add one more file vim and work on it use: :tabnew file3
Also u can use which will not create a new tab and will open file on screen slicing your screen: :new file3
If u want to use a plugin that will help u work with directories
and files i suggest u NERDTree.
To download it u need to have vim-plug so to download other plugins also NERDTree to type this commands in your ~/.vimrc.
let data_dir = has('nvim') ? stdpath('data') . '/site' : '~/.vim'
if empty(glob(data_dir . '/autoload/plug.vim'))
silent execute '!curl -fLo '.data_dir.'/autoload/plug.vim --create-dirs
https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim'
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
call plug#begin('~/.vim/plugged')
Plug 'scrooloose/nerdtree'
call plug#end()
Then save .vimrc via command :wq , get back to it and type: :PlugInstall
After that the plugins will be installed and u could use your NERDTree with other plugins.

Resources