VIM recently edited files history - vim

Is there a history of files edited in VIM that persists between
vim sessions, e.g. last 20 opened files.
I tend to edit the same .conf files and I have to navigate to them each time
of course they are spread all over the filesystem.

Actually this behavior IS built in. With default settings you should be able to use the :oldfiles command to view a numbered list of recent files. Then use :e #>4 for example, to edit number 4 in the list. Or :browse oldfiles, according to this answer.

As standard, no, but there's a plugin here that adds this functionality. This one also mentions recent file history on the page, so it may provide an alternative.

As this answer indicates, one may use ctrl-o to jump backward in file history. It will automatically open previously edited files.

You should consider giving ctrlp a chance (if you are not using it now).
It provides the command:
:CtrlPMRUFiles
that let's you select previous opened files. That is besides all the other cool stuff.
I was using the MRU plugin mentioned by #DrAI but once I started using ctrlp I just use that one.
Another popular plugin that provides a mru capability is Unite.

You can use :marks to navigate between recently used files.

Related

Vim: file system navigation similar to Spacemacs

I'm currently going back to Vim from Spacemacs. Is there a plugin that offers a similar way to search / open files? In Spacemacs you can easily navigate the file system after pressing SPC-f-f.
You can see an example here. I really like how it shows a list of the files and directories under the current directory.
If you can live without as-you-type fuzzy-completion, the built-in :help 'wildmenu' works beautifully.
If you absolutely need as-you-type fuzzy-completion there are many plugins to choose from:
Fzf
CtrlP
Command-t
FuzzyFinder
Unite
etc.

Can't save files created with vim anymore

I usually am able to save my vim files but for some reason i am getting E212 can't open file for writing. I am using mac osx. I am confused on how to edit my .vimrc file, and that there are two? one for the system and one for user.
The one you are supposed to edit is either ~/.vimrc (all Vim versions, including 7.4 and up) or ~/.vim/vimrc (only 7.4 and up).
This page give a detail introduction about vi/vim configuration file, you can read it to find what you need.

vim plugin for directory list/file open

I'm looking for a vim plugin similar to http://www.vim.org/scripts/script.php?script_id=1325 with a directory list in which you can select a file to open. Does this exist somewhere? Wasn't able to find it myself.
NERDTree is the second best ranked plugin on vim.org and it perfectly fits your description.
There are many other variations of the same principle:
FuzzyFinder
LustyExplorer (the one I used before)
Command-T (famous among TextMate switchers, I never liked it, though)
CtrlP (the one I use now)
…
And I second :Explore.
Have you tried the :Explore command that comes with the standard vim distribution since (roughly) version 6?
See http://vim.wikia.com/wiki/File_explorer for basic help on that command.
See http://vimdoc.sourceforge.net/htmldoc/pi_netrw.html#netrw-quickhelp for detailed help on what the Explorer can do.
If you want to open an explorer without replacing the current buffer, you can use the :Sexplore command. A puerile mnemonic for that is to execute :Sex.

Recent file history in Vim?

I would like to access recent files that I had opened and then closed in GVim. I open and close GVim frequently. I would like to access recent files from previous sessions as well.
Does GVim store recent files somewhere as Word and many other desktop apps store? How to access them?
At least terminal vim stores the previous ten files into ~/.viminfo in the filemarks section. You can use '0, '1, '2, ... '9 to jump among them.
(Probably only useful for '0 to get back to the last file you were editing, unless your memory is stronger than mine.)
You can also use the :browse oldfiles command to get a menu with numbers.
The best way that I use is
:browse oldfiles
Easiest way on vim.
There is mru.vim, which adds the :MRU command.
Very late answer here ... expounding on #sarnolds answer - You can view the file history with the oldfiles command #see :h oldfiles or :h viminfo
:oldfiles
Furthermore, you can have fine-grained file management with views and sessions ... #see :h mkview and :h mksession for specifics ...
Use :bro ol then press the number that corresponds to the file you want to open.
There is an Swiss knife of file switching CtrlP plugin, which is also part of janus distributive. It has :CtrlPMRU command with smart lookup among recently used files.
Note:
CtrlP maintains its own list of most recent used files in g:ctrlp_cache_dir."mru/cache.txt". It is not reusing viminfo (set viminfo?) which contains a list of file marks. This is useful if you want to clear this list.
Adding my 2 cents here because fzf was was not mentioned in earlier answers, which is such a wonderful tool:
fzf.vim has a :History command that lets you search the most recent used files in a fuzzy and search while you type manner.
I customize the (default) behavior of this command by not letting fzf reorder the search results list to the best match: I want the order of all matching filenames to keep being the order in which these files were last used.
To accomplish this customization, I added the following in my .vimrc to override the default History command defined by the fzf.vim plugin:
command! -bang -nargs=* History
\ call fzf#vim#history({'options': '--no-sort'})
EDIT:
Currently I'm using a neovim only plugin telescope.nvim which is very similar to fzf.vim, it has the command :Telescope old_files. And it can use the fzf algorithm as a sorting algorithm in the backend (which is currently recommended over the default sorter).
It looks a bit nicer, but can be a bit slower depending on the context. It is not as mature as fzf, but to me easier to customize, it is all lua script.
If you are a neovim only user, definitely worth checking out imho.
MRU has lot of features as explained here: http://www.thegeekstuff.com/2009/08/vim-editor-how-to-setup-most-recently-used-documents-features-using-mru-plugin/
The CtrlP plugin lets you search through your recently used files as well as files in the current directory with this command:
nnoremap <c-p> :CtrlPMixed<cr>
This saves you the hassle of having to deal with built in Vim commands and the MRU plugin, neither of which let you do fuzzy file searching, which is critical when working on larger projects.
You might be able to access the list from the command line with:
grep '^>' ~/.viminfo|cut -c3-|sed 's,~,'"$HOME"','
Explanation:
grep '^>' ~/.viminfo #find the list of recent files
cut -c3- #remove the first 2 characters
sed 's,~,'"$HOME"',' #replace ~ with absolute path
You could have a bash alias if you use this regularly
alias vim_mru="grep '^>' ~/.viminfo|cut -c3-|sed 's,~,'\"$HOME\"','"
As seen in the comments here (http://stackoverflow.com/questions/571955/undo-close-tab-in-vim), your file is probably still open in a buffer:
:ls " get the buffer number
:tabnew +Nbuf " where N is the buffer number
For example you can reopen the third buffer in a new tab (use :e instead if you don't use tabs):
:tabnew +3buf
:ls to list recent files with buffer number on left-hand column.
Then do :b{buffer-number} to jump there.
Example:
:ls shows list of files. I want to jump to third-last file I visited.
:b3 will take me there.
For faster searching, map :ls to something, e.g. <Leader>. in your .vimrc file.
One more plugin that let's you choose file from the list of last modified ones is staritfy. It replaces your start screen with a list of most recently modified files. You can always open this page later using :Startify command.
Also you can go back with ctrl+O.

How to navigate in large project in VIM

How do you manage big projects (hundreds of files) using only VIM?
I personally start having problems in any larger than small project.
is there any way to quickly 'go to file', preferably with name completition?
same for 'go to class definition', when it is in another file
I kinda know all the VIM basics, so I don't have problem using it for writing scripts or quick editing some source code. But it gets really messy for me when I have to navigate between files.
VIM has excellent support for tags. Once you have created a tags file for your project, you can jump to a definition or declaration of a method, class, etc., including jumping across files, all inside the same editing session.
Try
:help tags
To generate a tags file for C/C++, go to your shell prompt (I'm assuming your system is *nix/Cygwin) and type
info ctags
or
ctags --help
I like simple solutions, my favorite way to navigate at the moment is:
Add to ~/.vimrc.local
set path=$PWD/**
Then type this in the editor to find the file anywhere in the current working directory (pwd)
:find user_spec.rb
You can use tab-completion on the filenames to find multiple choices as well, making this TextMate convert very happy.
I use a combination of NERDTree (directory sidebar), FuzzyFinder Textmate (go-to-file like TextMate's CMD+T), and Sessions (:h sessions) to help me deal with large projects.
I would suggest using some sessions helper plugin. I would mention what I use, but I'm not satisfied with it yet. Just Google "vim sessions".
One thing to note with getting FuzzyFinder Textmate to work is that it depends on an old version the FuzzyFinder plugin, specifically v2.16. Anything higher and you'll get errors. But it's definitely worth the trouble. While it doesn't have name completion, its search is smart so if I search for fro/time/actionsphp it will pull up the file apps/(fro)ntend/modules/(time)_tracking/actions/(actions).class.(php) (parenthesis denote what it's matching). It makes it very easy to pick out files that are only unique by their folder name.
As well as the invaluable ctags and the various associated commands. I also couldn't live without the project plugin, which allows you to have the files of interest associated with a project in a separate pane. I can't remember how much of my setup is customised, but if I want to open a source file called Debug.c, I hit:
<F12> " Opens the project pane
/De " Searches for "De", which is likely to be enough to find Debug.c or possibly Debug.h
<ENTER> " Opens the selected file and closes the project pane
I often then do:
:vsp " Vertically split the window
<F12> " Reopen project pane
# " Search back to find previous entry with the same name (to find
Debug.h if I was on Debug.c: my headers are in Headers/ and
my source is in Source/, so the headers come earlier in the list
than the source). I use * to go the other way, obviously.
<ENTER> " Open the header file in the other window and close the project window.
With this relatively short sequence, I can open any file and it's header in a vertical split. Since the project plugin window is just a text file, completion is achieved by using Vim's searching capability.
Starting in Vim 7.3, the :find command has tab-completion of filenames.
So if you set your 'path' option to contain your entire project (probably using the ** wildcard to allow recursively searching subdirectories), then you can use the :find, :sfind, :tabfind, etc. commands with completion to get to any file in your project. This also allows jumping to files directly with gf and friends if the file name is in your text, for example in an include directive.
With this method, no external tools or plugins are needed for navigating to specific files. Although, it may admittedly not be as fast or easy to use, and doesn't address the need for jumping to definitions. For definitions, I use ctags as other answers suggest.
If you are using ctags as other posters have recommended, then make sure you look at the taglist plugin.
Make sure you take the time to read the docs and learn the key bindings. Here are a few to get you started (from the TList window):
o - open file in new window
t - open file in new tab
[[ or backspace - previous file in list
]] or tab - next file in list
Exuberant ctags.
Use Ctrl-] to jump to the tag under the cursor.
Opening vim from root of your source file and extending path option to include all sub-directories therein.
For example set path+=/usr/include/c++/** for C++ headers and set path+=** for your source directory.
This ,then, opens a plethora of following possibilities.
1) Opening file by name or parts of it
:find file_name
You can use auto-completion and wildcard expansion with :find reliably. You type the name, it will locate the name. This works language agnostic.I am sure you will like it.
2) Navigating to files under cusror:
if you want to go a file path like #include "project/path/classA.h.
gf or gF - go to file under cursor.
Ctrl-6 - to come back to last cursor position after gf or gF
3) API lookup and navigating to the API location
[i or [I can be used to look up your function signature for word under cursor without leaving your workspace. [<Tab> to actually go to declaration. Use Ctrl-6 to come back to last location.
Without extending path, you can start navigating files by :Ex command and navigate and open your file. I prefer NerdTree over this though.
I use FindFile. If you open vim at the root of your project and run :FC . the plugin will cache all the filenames beneath your cwd. You can then do :FF to open a completion menu and type the name of the file you want (or rather, the first few letters).
Although I'm kinda hoping someone will point out a better solution so I can learn something, NERDTree has been good to me for getting to specific files with name completion as long as I have the tree expanded. The command when I need to get to a file is something like:
,d/foo.pyo (where foo.py is a file name)
,d to open the tree, / to enter search mode, the name (or partial name, or regex, or whatever) of the file, and then o to open.
Of course you may have to hit 'n' a few times if you didn't type enough of the filename or there are duplicates.
I admit it feels like a bit of a hack using NERDTree like this although it has gotten so far into my muscle memory by now that I don't even think about it.
Of course I use ctags too but those are only useful when you have a function near the cursor and need to get to its definition in another file or something. A lot of times I say "OK, I need to work on feature x now" and need to navigate to another file without any references nearby that ctags would really help with.
I'm using two plugins of mine:
searchInRuntime that completes filenames on command line. It is somehow similar to fuzzyfinder and lookupfile,
lh-tags which is completely experimental and undocumented. It offers two features: automatic and quick update of the tagfile on file save(ing?), and a tag selector plugged to <c-w><m-down> by default. You may want to check the renowned taglist plugin instead.
Both require my viml library lh-vim-lib.
Try SourceCodeObedinece. This one I developed to handle C++ 1Gb source files project.
I use it in pair with 0scan.
These two plugins are wrappers around the most popular Vim browsing tools: ctags and cscope.

Resources