ctrlp fuzzy search string in all file in directory - vim

Using ctrlp, I would like to search for a string in all files in the current directory ( or a specified directory).
Using the "lines" mode is only searching for files opened. I would like to search for all files, including the files not opened.
Thanks in advance.

Related

VIM & NVIM have trouble differentiating file with same name but different very long path

I have a two file with the same name & content like this.
// File 1
m/d/b/x/s/d/x/same_name.c
// File 2
p/t/k/x/d/x/same_name.c
When I have File 1 open in the buffer and try to open File 2, it always opens File 1. The only way to open File 2 is to do
:bw <Buffer number of File1>
Tried this with/without plugins, on VIM and NVIM but I get the same result.
This is not an issue when the path name is smaller.
I am guessing that this might be because of 'd/x/same_name.c' which is common on both long paths. (Or am completely wrong :D )
What setting in vim/nvim can I use to control the path matching for buffers?
Or is this a bug (I doubt it)
PS: I have already looked at this
2 files having the same name but different paths => vim thinks they are the same
I am already using "//" in directory.

See file metadata from NERDTree

nerdtree allows navigation through the file system within vim and performing file system operation like creating and deleting files and directories.
Is there any command to see file metadata like timestamp, owner or permissions, as a unix ls -l command would output?
This is an explicit answer. You can find the answer if you dig into the comments from Ingo Karkat's answer.
Open Nerdtree
Move to the specific file or directory
Press the keys m and l
92 represents the filesize in bytes.
There's nothing built-in (as NERDTree is mainly a file explorer for locating and opening files within Vim, this isn't a typical use case), but you can surely build something like that through the plugin's extension points.
:help NERDTreeAPI documents how to define custom key mappings and menu items. As these get passed the current tree object, you can then query and display the metadata, e.g. using
let metadata = system('ls -l ' . shellescape(filespec))

Supply a path for file editing in VIM?

Is there a way to set a PATH-like sequence of directories to search for files in vim? My project has C files split across many directories, and it would be nice to jump back and forth without remembering the full path each time.
For instance, if I have:
platform/drivers/uart.c
ui/display/menu.c
cpu/registers/regs.h
I would like to be able to set PATH to "platform/drivers:ui/display:cpu/registers". Then when I want to switch to a file, I can just type:
:e uart.c
instead of
:e platform/drivers/uart.c
I understand that I can change the working directory, but then I have to type
:e ../../ui/display/menu.c
to get to another directory.
Alternatively, is there a better way to navigate a project like this than using :edit?
There is, and it's called path. The way you use path is with the :find command: :find menu.c would search for menu.c in the directories in path and edit it. There are other commands that use path, like :sfind that opens the found file in a new split. See the documentation of path for details and other commands that use it.
Another thing that may help you find your files is the **-wildcard that can expand to any directory path. For example :edit **/menu.c will look for menu.c in subdirectories, so you don't have remember and type the full path.

vim set working directory

When I switch buffers in Vim (using :bn and :bp), I want it to automatically set the working directory, BUT not to be the directory of the opened file. I want Vim to search recursively upwards for a file called "tags", and when it finds that file, set the working directory to the directory with the "tags" file.
Example:
:e ~/programming/projects/foobar/src/js/scripts.js
As you can see, "foobar" is kind of the "project root". Let's assume the "tags" file is in the foobar directory. Now Vim should look in "js", but there's no tags file. Then it should look in "src", no tags file there. Then it should look in "foobar" and find the "tags" file, then do:
:cd ~/programming/projects/foobar/
Is there a simple way to do this? :)
If your whole point is to get to the correct "tags"-file then this could be done easier:
set tags=./tags,tags;$HOME/programming,$HOME/programming/your/global/tags
The tags option accepts a comma (or space) delimited list of entries. In my example I have the following entries:
./tags this means it should look first for a tags-file in the current directory
tags;$HOME/programming this means look for a tags-file from the current directory up to $HOME/programming (that's what the semicolon does, see file-searching). If you don't specify the "stop"-directory using the semicolon then it searches up to the root directory /
$HOME/programming/your/global/tags lastly this is a tags file referred to by absolute file name
My example is probably overkill for your purpose from your description you only need this:
set tags=tags;$HOME/programming
But if you really need to change the working directory then you could add something like this (change lcd to cd if you have to) to your .vimrc:
au BufNewFile,BufRead *.js execute ":lcd " . fnamemodify(findfile("tags", ".;"), ":p:h")
Disclaimer: I'm the author of the mentioned plugin.
I think you could use the little codepath.vim. I wrote it because I was in need of a little function that would help me to reach my project root. The plugin makes the assumption you have a folder with all your code. Something like $HOME/code. Well, it provides the following function:
codepath#path()
I use in combinations with plugins like NERDTree or command-t. So I can open a NERDTree window in my project root. It really is a little plugin but I use it all the time.

Save file with new filename: append to existing filename

Is there a simple way to (in VIM) do save the currently open file with it's current name plus an appended phrase?
IE, from /home/affert/ type vim /data/folder/file1.txt
then save the file as /data/folder/file1.txt_BACKUP without needing to copy and paste the filename?
Context: I have a file that has full paths in it to other files in other folders. I use ctrl+W, ctrl+F to open the file in a new window. That's why I don't want to copy and paste. BTW, the folder and file names are a lot longer, so typing them myself is not a useful option.
:w %:p_BACKUP
For explanation see How can I expand the full path of the current file to pass to a command in Vim?.
Easy:
:w %_BACKUP
If you need override:
:w %_BACKUP!
The it gonna answer:
"filename_BACKUP!" [New] XL, XC written

Resources