Use ctrlp.vim to browse files without recursing? - vim

Is there any way to use ctrlp.vim to browse files without recursing through all the possible directories?
For example, so that it can usefully be used from a home directory (or other large directory):
<c-p>
co<tab> " matches ~/code directory
code/pyt<tab> " matches python-2.7.6
code/python-2.7.6/R<tab> " matches README
Note: setting g:ctrlp_max_depth doesn't help here, since there isn't any obvious way to ask ctrlp to re-scan a directory once it has been matched (ex, in the above example, the code/ directory might be matched, but no sub-directories will be found).

Related

ctags, generate tags using multiple paths?

When I build/update my tags file, ctags starts at the current directory and works its way down recursively.
I would like for it to also include a completely different search path, a mapped network drive, and add those results to my tags file as well.
Is there any way to do that?
When the files in the other directory are related and often change together with the current directory hierarchy, I'd write a custom :Ctags command that supplies the other path to the :!ctags call.
If the other files are unrelated, and rarely update (as based on your comments seems to be the case), I'd run ctags there separately and include them via
:set tags+=/path/to/other/dir/tags
NOTE: Add the tag filename at the end, else there will be "tag not found" error. By default the name is tags but it could be renamed with -f option as below.
ctags -f my_tags -R
:set tags+=/path/to/other/dir/my_tags

How can I use `path` and `lvim` to find files easily?

At the moment I use :Explore to navigate to my project directory, I press 'c' to set that as the current directory. I have set path=.,, in my .vimrc, and then I use:
:lvim pattern ./**/*
to find patterns in files. This does work, but I have to type ./**/* instead of just * or *.rb for example. Is there a setting for path that I can use to avoid having to do that?
Also, I am working in an environment where I cannot use plugins at all. (Also I would rather use vim plain-and-simple anyway) Thanks!
:lvim, :lgrep, :vim and :grep don't use the path option: they work by default from the current directory, from the directory of the current file if you use ./ or from any arbitrary directory if you use an absolute path.
The value of path has no effect whatsoever: it is used for other unrelated commands.
:lvim pattern ./*.rb
would search for pattern in every Ruby file under the directory of the current file.
:lvim pattern *.rb
would search for pattern in every Ruby file under the current directory.
The current directory and the directory of the current file may or may not be the same. If both the current directory and the directory of the current file are identical, the two commands above should have the same outcome but, if they are different, those two commands could have very different outcomes so make sure you are able to tell the difference.
If you want to search in the current directory, use:
*.rb
If you want to search recursively in the current directory, use:
**/*.rb
If you want to search in the directory of the current file, use:
./*.rb
If you want to search recursively in the directory of the current file, use:
./**/*.rb
Since you have changed the current directory with netrw's c, it seems logical to assume that you want that directory to be the base for your search. The right command would therefore be:
:lvim pattern **/*.rb

Prevent CtrlP from switching to search in submodule directory

I'm still learning to use vim like a pro. In the process I have noticed that when I open up a file in submodule in a project directory CtrlP plugin switches the root for search to the root of the submodule directory. Is there a way to prevent this and keep the root search directory the one of the orignal project or the the one that was opened up initially?
You probably want to tweak the g:ctrlp_working_path_mode setting. It sounds to me like you most likely want to just disable this feature all together and manually set your working directory with :cd.
From the current ctrlp docs on GitHub:
When starting up, CtrlP sets its local working directory according to this
variable:
let g:ctrlp_working_path_mode = 'ra'
c - the directory of the current file.
a - like "c", but only applies when the current working directory outside of
CtrlP isn't a direct ancestor of the directory of the current file.
r - the nearest ancestor that contains one of these directories or files:
.git .hg .svn .bzr _darcs
w - begin finding a root from the current working directory outside of CtrlP
instead of from the directory of the current file (default). Only applies
when "r" is also present.
0 or <empty> - disable this feature.
Note #1: if "a" or "c" is included with "r", use the behavior of "a" or "c" (as
a fallback) when a root can't be found.
Note #2: you can use a |b:var| to set this option on a per buffer basis.
In your .vimrc you must change CtrlP behavior by setting:
let g:ctrlp_working_path_mode = 'rw'
With this setting it will look for version control files in current working dir outside.
Then before working on your project you change dir to its main folder, where main .git is or to a dir inside that (not in a submodule):
:cd my/project/main/path
If you open a file inside a submodule and while there hit C-p, if current working dir is outside the submodule, it will still use the file were your main .git is.
I've got these mappings in my ~/.vimrc:
nnoremap <leader>f :CtrlP<CR>
nnoremap <leader>F :CtrlPCurWD<CR>
I use the first if I want to open a file anywhere in my project and the second if I want to open a nearby file.

Searching with command-T

When I search some file with command-T it often failes to find it because I'm not in the right directory, so I have to change the directory.
Is it possible to set that command-T will search first in the directories that are bookmarked in Nerdtree or somewhere else?
I could change the directory to / but this search very large scope of files. When I change the dir to my home directory and I'm looking for something ordinary like .bashrc I will find rather many files that are located under .wine directory.
In 99 % of time I need to search files in project directories that I actively work with. Can I set these directories in some preferences?
According to the documentation you can exclude directories from your search:
*command-t-wildignore*
|'wildignore'| string (default: '')
Vim's |'wildignore'| setting is used to determine which files should be
excluded from listings. This is a comma-separated list of glob patterns.
It defaults to the empty string, but common settings include "*.o,*.obj"
(to exclude object files) or ".git,.svn" (to exclude SCM metadata
directories). For example:
:set wildignore+=*.o,*.obj,.git
A pattern such as "vendor/rails/**" would exclude all files and
subdirectories inside the "vendor/rails" directory (relative to
directory Command-T starts in).
So if you wanted to exclude a backup dir, you would write:
set wildignore+=project/backup
in your .vimrc
In addition, to ignore dotfiles/dotdirs you can look into these options:
g:CommandTNeverShowDotFiles
g:CommandTScanDotDirectories
g:CommandTMaxDepth
These allow you to:
- ignore dotfiles completely;
- stop searching recursively in dotdirs;
- specify at what depth should Command-T stop scanning.
I found this information in the author's git, but you can probably see this document by issuing in vim:
:help Command-T
(or a similar name)
I did not see any reference to preferences or bookmarks in the plugin.
However if you start vim by opening a file in said project directory you might want to add this line to your .vimrc:
set autochdir
This option will set on startup your directory to the current file's directory.
You could try Ctrl-P. I had the same problems as you do and making the change solved them.
I also ignore some folders (in .vimrc):
let g:ctrlp_custom_ignore = {
\ 'dir': '\.git$\|\.hg$\|\.svn$',
\ 'file': '\.exe$\|\.so$\|\.dll$' }

How to change the folder path for swp files in Vim

I'm working at a project on a remote server. I don't want to have the swap files on the server. I would like all swap files for Vim (and, of course, gVim) to be saved on the specified directory. How could I do that?
You can set the directory option to the location that you want vim to store its swap files, e.g.:
mkdir -p $HOME/.vim/swapfiles # this dir must exist vi does not create it
" $HOME/.vimrc
:set directory=$HOME/.vim/swapfiles//
I use trailing double path separators because, from the help docs:
For Unix and Win32, if a directory ends in two path separators "//" or "\\", the swap file name will be built from the complete path to the file with all path separators substituted to percent '%' signs. This will ensure file name uniqueness in the preserve directory.
You might want to consider setting the backupdir options as well:
set backupdir=Z:\backups
That makes vim store backups in a specific location rather than in the current directory.

Resources