How to create a new separate buffer list in vim? - vim

There are some occasions that I wanted to open several files to be references and didn't want them to mess up my current buffer list. I have tried :new, sp, vs, and :tabnew, however, the new opened files are always kept in my current buffer list. Are there a way to create a brand new buffer list in order to keep these opened files separately? So, I could close these files entirely, whenever I didn't need to see them anymore, by using 1,100bd. I have been googling it but still no luck.

You can try this plugin: vim-ctrlspace.
From README.md of mentioned plugin:
The answer are tabs – a secret weapon of Vim-CtrlSpace. Each tab holds
a separate list of buffers. And this is something very different when
compared to plain Vim. Tabs powered by the plugin can be seen as piles
of documents on the desk.
With tabs you can, for example:
group related buffers
extract to other tabs
name them accordingly
move or copy them
Tabs usage in Vim-CtrlSpace is quite more extensive than in Vim. This
is because they serve mainly as independent buffer lists, so you are
likely to have plenty of them. Tabs can be accessed and managed within
Tab List.
All your buffers, tabs, and tab layouts can be persisted as a
workspace. It's like taking a picture of your desk with an instant
camera. You can save multiple workspaces per project with Workspace
List.

Try using the argument list. See :help :args and surrounding sections. If all your reference files (and only those) are in your arg list, you could :argdo bd to get rid of them all. There are local arg lists too, if you are already using the global list.

Disclaimer: Using the plugin is almost certainly a more robust solution as I made this myself and I'm relatively new to vim. But this is at least a good exercise in the uses of quickfix lists.
My Answer:
You could use quickfix error lists as file lists. see :help quickfix for full documentation. Here is my work flow for when I want to go through all the files that differ from master (using git).
vim to open vim in the directory you are working on.
new to open a new buffer
read ! git diff --name-only master to paste the output of my bash command into the new buffer. You can run any command or manually enter a file list here. You may want to clean it a little by removing any blank lines.
w! /tmp/quickfix.txt to create a file name we can use later.
set errorformat+=%f to change the errorformat to accept vanilla lists of file paths. see :help errorformat for more details on that.
cfile /tmp/quickfix.txt to read the list of files and create a quickfix "error" list using the new errorformat.
set errorformat-="%f" (optional) this removes the added errorformat to make sure you're not messing with any other use of errorformat, like for a vimgrep.
Now you can use quickfix options to move between files. :copen to open an interactive list and :cnext/:cprevious to cycle them. You can also have multiple lists at once. I use :colder to go back to previous lists. You should be familiar with this if you use any grep commands in vim.
These are just the manual steps. You can make a function that does all this in one go or any part that you need. Here's what I'm using.
function! LoadQuickfixFileList(filename)
set errorformat+=%f
execute "cfile ".a:filename
set errorformat-=%f
endfunction
function! WriteBufferToQuickFixFileList(filename)
call writefile(getbufline(bufnr(bufname("%")), 0, "$"), a:filename)
endfunction
function! GitDiffNameOnlyMaster()
execute "new"
execute "read ! git diff --name-only master"
execute "normal! ggdd"
call WriteBufferToQuickFixFileList("/tmp/quickfix.txt")
call LoadQuickfixFileList("/tmp/quickfix.txt")
endfunction
nnoremap <silent> <leader>cgd :call GitDiffNameOnlyMaster()<CR>
nnoremap <silent> <leader>cw :call WriteBufferToQuickFixFileList("/tmp/quickfix.txt")<CR>
nnoremap <silent> <leader>cf :call LoadQuickfixFileList("/tmp/quickfix.txt")<CR>
<leader>cgd does the whole thing for git diff --name-only master. If I make a list of files in a new buffer myself, I can use <leader>cw to write it to /tmp/quickfix.txt. Once I've done that or if I create a /tmp/quickfix.txt file myself in another way I use <leader>cf to load that file.

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 netrw - Explore when modified without split

I've opened a file and modified it - now I want to do :Explore without splitting.
It splits - because I have an unsaved buffer. By default this is nice, becasuse Vim generally doesn't allow to switch files, until you do something with the modified buffer (save or discard changes).
But I have set hidden option, so I can jump from buffer to buffer, switch files and everything - and save or not when I feel I want to. It seems Netrw doesn't follow this policy. How could I help it?
One way I know of is to map netrw explore to save & explore, but I'm not sure if autowriting is a good way of doing things... I am actually using other autowriting settings now, but I was just rethinking to maybe get rid of them.
So here is the function, that does just that:
function! ExploreWithHidden()
let s:cw = getcwd()
lcd %:p:h
enew
Explore
cd `=s:cw`
endfunction
Seems to work like expected.
You could use :Texplore instead. This is the same as explore except in a new tab (and will have no splits).
:h Texplore
Another thing you could do is use :lcd %:p:h to change the current working directory to the directory of the file. Then use :enew to create another buffer then open explore. (Make sure hidden is on if the buffer is modified)
:command! BExplore :lcd %:p:h | enew | Explore
To run the command use :BExplore.
The old buffer will be sitting in the background and the explore window is pointing at the directory the file was in.
You could just upgrade your netrw -- its up to v153s at http://www.drchip.org/astronaut/vim/index.html#NETRW, and it does avoid the split if the modified buffer is hidden.
tpope's vim-vinegar operates like this. It maps - to Explore the current file's directory. (That's the same key netrw uses to go up one directory.) It uses the current window instead of splitting.
When the current file is modified:
If you have 'hidden' set, it will not split and Explore from the current window.
If you do not have 'hidden' set, it will issue an error and do nothing.

vim, switching between files rapidly using vanilla Vim (no plugins)

I understand that limiting myself to vanilla Vim (not using plugins) limits the power of the editor, but as I switch between different machines frequently, it is often too much trouble to move my environment around everywhere. I want to just stay in vanilla Vim.
Something that holds me back is the ability to quickly switch between files.
I (believe at least) have a good understanding of buffers, windows, tabs, as well as netrw (Vex, Ex, etc).
But in an editor such as Sublime Text, I can just type ctrl-p and instantly I am at the file.
I know that I can drop down to the shell, but I wonder if there are any other "hidden" secrets to rapidly switching between files in Vim based off more than just the filename.
The closest equivalent ("closest", not "exact") to ST2's Ctrl+P is a plugin called, get ready… CtrlP. There are other similar plugins like Command-T or FuzzyFinder.
I use CtrlP and I love it but I wholeheartedly support your decision to go "plugin-free". It's not the easiest way to go but it will pay off in the long run.
Opening files
The most basic way to open a file is :e /path/to/filename. Thankfully, you get tab-completion and wildcards: the classic * and a special one, **, which stands for "any subdirectory".
Combining all of that, you can do:
:e **/*foo<Tab>
to choose from all the files containing foo in their name under the working directory or:
:e **/*foo/*bar<Tab>
to choose from all the files containing bar in their name under any subdirectory containing foo in its name, anywhere under the working directory.
Of course, that works for :tabe[dit], :sp[lit] and :vs[plit], too.
Those commands are limited to one file, though. Use :next to open multiple files:
:next **/*.js
and take a look at :help arglist.
Jumping between buffers
:b[uffer] is the basic buffer-switching command:
:b4 " switch to buffer number 4
:bn " switch to next buffer in the buffer list
:bp " switch to previous buffer in the buffer list
:bf " switch to first buffer in the buffer list
:bl " switch to last buffer in the buffer list
:b foo<Tab> " switch by buffer name with tab-completion
:b# " switch to the alternate file
Note that many of these commands and their relatives accept a count.
The :ls command shows you a list of loaded buffers. It is a bit "special", though: buffers are assigned a number when they are created so you can have a list that looks like 1 2 5 if you delete buffers. This is a bit awkward, yes, and that makes switching to a buffer by its number a bit too troublesome. Prefer switching by partial name, :b foo<Tab> or cycling, :bn :bp.
Anyway, here is a cool mapping that lists all loaded buffers and populates the prompt for you, waiting for you to type the number of a buffer and press <enter>:
nnoremap gb :ls<CR>:b<Space>
With this mapping, switching to another buffer is as simple as:
gb
(quickly scanning the list)
3<CR>
or:
gb
(quickly scanning the list)
foo<tab><CR>
The idea comes from this image taken from Bairui's collection of Vim infographics:
Vim also has <C-^> (or <C-6> on some keyboards)—the normal mode equivalent of :b#—to jump between the current buffer and the previous one. Use it if you often alternate between two buffers.
Read all about buffers in :help buffers.
Go to declaration
Within a file, you can use gd or gD.
Within a project, Vim's "tags" feature is your friend but you'll need an external code indexer like ctags or cscope. The most basic commands are :tag foo and <C-]> with the cursor on a method name. Both tools are well integrated into Vim: see :help tags, :help ctags and :help cscope.
For what it's worth, I use tag navigation extensively to move within a project (using CtrlP's :CtrlPTag and :CtrlPBufTag commands, mostly, but the buit-in ones too) and my favorite "generic" buffer switching method is by name.
Deploying your config
A lot of Vim users put their config under version control which makes it very quick and easy to install your own config on a new machine. Think about it.
EDIT
A few months ago, I had to work on a remote machine with an outdated Vim. I could have installed a proper Vim and cloned my own beloved config but I decided to travel light, this time, in order to "sharpen the saw". I quickly built a minimalist .vimrc and revisited a couple of half forgotten native features. After that gig, I decided CtrlP wasn't that necessary and got rid of it: native features and custom mappings are not as sexy but they get the job done without much dependencies.
Juggling with files
set path=.,**
nnoremap <leader>f :find *
nnoremap <leader>s :sfind *
nnoremap <leader>v :vert sfind *
nnoremap <leader>t :tabfind *
:find is a truly great command as soon as you set path correctly. With my settings, ,ffoo<Tab> will find all the files containing foo under the current directory, recursively. It's quick, intuitive and lightweight. Of course, I benefit from the same completion and wildcards as with :edit and friends.
To make the process even quicker, the following mappings allow me to skip entire parts of the project and find files recursively under the directory of the current file:
nnoremap <leader>F :find <C-R>=expand('%:h').'/*'<CR>
nnoremap <leader>S :sfind <C-R>=expand('%:h').'/*'<CR>
nnoremap <leader>V :vert sfind <C-R>=expand('%:h').'/*'<CR>
nnoremap <leader>T :tabfind <C-R>=expand('%:h').'/*'<CR>
WARNING! The path option is extremely powerful. The value above—.,**—works for me, as a default fallback value. In the real world, the exact value of the option will differ from project/language/framework/workflow to project/language/framework/workflow, so the proper value depends entirely on your needs. Don't blindly copy that line and expect it to solve all your problems.
Juggling with buffers
set wildcharm=<C-z>
nnoremap <leader>b :buffer <C-z><S-Tab>
nnoremap <leader>B :sbuffer <C-z><S-Tab>
The mappings above list the available buffers in the "wildmenu" with an empty prompt, allowing me to either navigate the menu with <Tab> or type a few letters and <Tab> again to narrow down the list. Like with the file mappings above, the process is quick and almost friction-less.
nnoremap <PageUp> :bprevious<CR>
nnoremap <PageDown> :bnext<CR>
Those mappings speak for themselves.
Juggling with tags
nnoremap <leader>j :tjump /
This mapping uses regex search instead of whole word search so I can do ,jba<Tab> to find tag foobarbaz().
Yes, fuzzy matching is addictive but you can be just as productive without it. And for a fraction of the cost.
MORE EDIT
A couple of additional tips/tricks…
Wildmenu options
The "wildmenu", enabled with set wildmenu, makes file/buffer navigation easier. Its behavior is governed by a bunch of options that are worth investigating:
wildmode tells Vim how you want the "wildmenu" to behave:
set wildmode=list:full
wildignore filters out all the cruft:
set wildignore=*.swp,*.bak
set wildignore+=*.pyc,*.class,*.sln,*.Master,*.csproj,*.csproj.user,*.cache,*.dll,*.pdb,*.min.*
set wildignore+=*/.git/**/*,*/.hg/**/*,*/.svn/**/*
set wildignore+=tags
set wildignore+=*.tar.*
wildignorecase allows you to search for foo and find Foo:
set wildignorecase
File marks
augroup VIMRC
autocmd!
autocmd BufLeave *.css normal! mC
autocmd BufLeave *.html normal! mH
autocmd BufLeave *.js normal! mJ
autocmd BufLeave *.php normal! mP
augroup END
I recently found this gem in someone else's ~/.vimrc. It creates a file mark at the exact position of the cursor whenever you leave a buffer so that, wherever you are, 'J jumps to the latest JavaScript buffer you edited. Awesome.
The answer depends a lot on your preferences and circumstances. Some examples:
If it's mostly two files (e.g. a C header and implementation file), <C-^> is very handy. In general, the alternate file is an important concept.
If you use a large editor window, window :splits turn the problem of locating a buffer from locating the window (once you've got all buffers opened). You can use [N]<C-w><C-w> to quickly switch to it.
If you can memorize (a few) buffer numbers, the :[N]b[uffer] and :[N]sb[uffer] commands are quite handy; :ls tells you the numbers.
Plugins (or at least custom mappings) can improve things a lot, and there's a whole variety on this topic on vim.org. There are various mechanisms to distribute your config (Pathogen + GitHub, Dropbox, ...), or you could remotely edit server files through the netrw plugin that ships with Vim.
Sometimes it is also handy to go sequentially through a list of files (e.g., if you did something like vim *.php to open several files at once). Then you can use :n[ext] (as well as :prev[ious], :fir[st], and :la[st]) for navigation (in addition to what was suggested in the other answers).
You can do wildcard tab completion on the command line without any plugins. e.g.
:e src/**/foo*<tab>
will let you cycle through all the files starting with 'foo' in the directory tree under ./src and select the one you want to edit.
If you have already edited the file and it is still in a buffer then you can switch to it with:
:b foo<tab>
which will cycle through all the buffers with 'foo' in the path.
You may need to set the wildmode and wildmenu options to get the behaviour you want. I have
wildmode=longest:full
wildmenu
in my .vimrc.
If you are on a filename and want to jump to that file, gf will do it for you. I also like using ctags, which isn't a plugin; you just build the tags and can easily jump around your codebase.
If you want switch between files in vim editor, please see below answer
First press Esc key to exit from edit mode.
Then type :e to check current file path.
if you want to go another file then type :e /path-of-file.txt/ using this you are able to switch.
If you want to go previous file simply type :e# which switch to previous file path.
I had the same issue with Vim.
The last thing I want is to depend on plugins for a task as mundane as file switching.
I added the following lines to .vimrc
set path+=**
set wildmenu
And BAM! I can now :find any/filename/in/any/folder/ as long as vim is in the root directory of the project. Tab completion works. Wildcards work!
Once files are opened already, and there are a ton of buffers in the background (you could use :ls to see all buffers), running :b any/file <TAB> will fuzzy search for all buffers and jumps to the required file. In case it is not unique there will be a wildmenu of tabs (hence the 2nd line in .vimrc) which can be selected using tab.
My answer is coming from this awesome video
https://www.youtube.com/watch?v=XA2WjJbmmoM&feature=youtu.be&t=489
There are more tricks in and I recommend watching it.

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)

View a list of recent documents in Vim

Is there a way to view the list of recent documents you've opened in Vim?
I realize I could view the cursor jump list, :ju, and then go to a cursor position in the list but this is not ideal because there will be multiple listings of the same document in the list.
Is there another command which would do what I'm looking for?
Don't use a plugin, unless you want a nice menu. From Vim Documentation: Starting (or :help old):
:ol[dfiles]
Then to open one of the listed files, use: '0, '1, '2, ... '9
List the files that have marks stored in the viminfo file.
:bro[wse] ol[dfiles][!]
List file names as with :oldfiles, and then prompt for a number. When the number is valid that file from the list is edited. Use ! to abandon a modified buffer.
The Most Recently Used (MRU) plugin provides an easy access to a list of
recently opened/edited files in Vim. This plugin automatically stores the
file names as you open/edit them in Vim.
http://www.vim.org/scripts/script.php?script_id=521
Besides :oldfiles, fzf.vim has :History.
Start Vim and hit Ctrl-o-o to open previously edited file. Keep hitting o (while still pressing the Ctrl key) to cycle back through earlier files. See https://dev.to/jovica/3-little-known-but-useful-vim-tips-1pbg
vim plugin: minibufexpl may help you.
the opened file list is displayed on the top or bottom of the screen:
in vim normal mode, type :b${bufid} to jump to the ${bufid}_th buffer, for example: type :b13 to jump to the 13th buffer, ie. ngx_mail_ssl_module.c.
besidies, you can map some convenient bindings in your vimrc, such as:
" ------------------------------- minibufexpl mappings -----------------------------------
"let g:miniBufExplSplitBelow=1
nnoremap <silent> <leader>bn :bn<cr>
nnoremap <silent> <leader>bp :bp<cr>
nnoremap <silent> <leader>bf :bf<cr>
nnoremap <silent> <leader>bl :bl<cr>
nnoremap <silent> <leader>bt :TMiniBufExplorer<cr>
Get the plugin from here: https://github.com/fholgado/minibufexpl.vim
In addition to oldfiles there's a nice thing called tinyMRU.
Vim-tinyMRU's only purpose is to provide an intuitive alternative to the built-in :oldfile command. Nothing more, nothing less.
It's very simple:
https://github.com/romainl/vim-tinyMRU/blob/master/plugin/tinymru.vim
A good plugin is https://github.com/Shougo/denite.nvim
You can call :Denite file_old in order to have fuzzy search on the list of old files. In particular, just hitting Enter will re-open the last opened file. Assigning a shortcut to this is useful:
nnoremap <leader>o :Denite<space>file_old<CR>
This saves few keystrokes compared to :browse oldfiles, q, 1, Enter
The easiest way for me to access recent files is to add the following to one's .gvimrc file:
let g:netrw_sort_by = 'time'
let g:netrw_sort_direction = 'r'
These lines get netrw to sort files by those most recently modified. Then one simply calls :e. and selects the file one wants.
This solution presupposes files are saved in one main directory so specified in .gvimrc. E.g.
cd ~/vim
No directly the answer but related.
you can define aliases to open the last opened file(s) by vim:
alias vil='vim -c "normal! '\''0"' # open the last file
alias vil1='vim -c "normal! '\''1"' # open the second last file ...
alias vil2='vim -c "normal! '\''2"'
:ol works, but why not use fuzzy search to get the exact match quickly from a long list of file?
There's a very handy plugin, ctrlp which allows you to use :CtrlPMRU, and you can quickly get what you looking for.
For many many years, ctrlp is one of the first thing I would install for vim!
https://github.com/ctrlpvim/ctrlp.vim

Resources