I'm using VIM as my primary code editor for Laravel projects. While I'm in VIM, I want to search for a file that I can open up as a tabnew or as a new vsplit pane. I was told there's a find command. So I tried something like this:
:find ~/development.project1.com/ -name *Controller.php
But that only gave me the error E345: Can't find file "/var/www/development.project1.com/ -name *Controller.php" in path
What did I do wrong? How can I quickly search for other files in VIM and open them up as a tabnew or as a new vsplit pane?
The vim find command is not the same as the unix find command. To find out what find does, use the online help!
:h find
This will give you an answer:
:fin[d][!] [++opt] [+cmd] {file}
Find {file} in 'path' and then :edit it.
In other words, :find is like :edit but looks in your path instead of just the current directory. Note that the vim path is not the same as the operating system shell variable PATH. You can find out what is in your path with
set path?
Most likely you don't have every subdirectory of your project in your path (or in your PATH). Neither should you.
If you want to edit a file with a name ending in Controller.php, a simple solution to search through every subdirectory is to specify ** before the filename to wildcard-match against every subdirectory:
:e **/*Controller.php
Note that doing the above will only open the first file matching the wildcards. If there are several matching files, and that wasn't the file you wanted, no luck.
If you want to choose a file among several matches, and don't want to use plugins, you can read a list using the unix file command
:r! find . -name \*Controller.php
You will end up with a buffer with a list of files. To open one of the files, move the cursor above the file name, and use the gf command to open it.
While not really an answer to your question, with vanilla vim, there's wildmode command line completion. If wildmode is enabled, vim will complete filenames when you open a new file with :e.
Finally, there are lots of different fuzzy finder plugins for vim. If you don't need windows, I recommend fzf.
vim find and find command are different as noted. Perhaps, you might like ctrlp.
But a easier vanilla vim replacement is to go to the folder which contains your files and in vim
:set path+=**
:find file_name
This will find and edit file_name. Nice thing of this is that it can auto-complete the file name but this will not be in split or tab.
Related
I'm looking for a way to configure my vimrc file or set variables that can search external file for auto completion in Vim
For example:
I want the words completion in following directory:
/home/code/mycompletionfile.txt
By the way:
I do know I can use dictionary to include a file, but I'm looking for a way to use directory so that all the files under the directory can be searched for auto completion.
I knew there are many awesome plugins out there and I did install some plugins before, but some plugins slows down my Vim significantly.
This is what I did so far:
I try set path variable in vim:
Add my path that contains all my files so that Vim can search all the file in the path for auto completion
Try:
:set path+=/home/code/
Try:
:set path+=/home/code/*
Try:
:set path+=/home/code/.
None of them are working.
I do know complete variable has many options in Vim:
complete: .,w,b,u,t,i
The 'path' option is completely irrelevant.
Here is the relevant part of :help 'complete':
k{dict} scan the file {dict}. Several "k" flags can be given,
patterns are valid too. For example:
:set cpt=k/usr/dict/*,k~/spanish
When I try to open a file in Vim (Linux) for editing, when I press TAB, Vim autocompletes filename only with filenames from the current directory. However, having searched on the Web, I suppose that from version 7 Vim should support bash-like filename autocompletion using filenames from all the directories in the search path.
Say, there is a file file1 in a directory dir1 (which directory is also in the environment variable PATH).
I type the following commands in Vim:
set path=/dir1
set wildmode=list:longest
And then, when I type:
:e fil<TAB>
The filename is not autocompleted. How to enable this feature in Vim?
Tab-completion works. You just expect it to do something it is not actually supposed to do.
:e[dit] and its siblings (:sp[lit], :vs[plit], :tabe[dit]) don't use the path option at all, no matter what version of Vim you have.
Use :fin[d] fil<Tab> instead (and :sf[ind], :vert sf[ind], :tabf[ind]).
Use set path=/dir1/** to make :find recursive.
See :help 'path' and :help :find.
edit
It is generally considered "good practice" to start Vim from the root of your project:
$ cd /path/to/project
$ vim somefile
The main advantage being that it sets Vim's "current directory" to a usable value that allows you to browse your project relatively easily or use external programs on your project in a clean and intuitive way.
Let's say if I want to open a file in a specific path
C:\Program files\vim\_vimrc
Then what is the best/fast way to open the file with minimal actions or typing?
:e **/filename<tab>
:e **/*name<tab>
Note that this can be a bit slow when traversing large/deep directories.
With the right value for path, wildignore and wildignorecase, the :find command can be awesome:
:find *foo
There are also obviously many plugins if you want, like CtrlP or VimFindsMe.
:e c:\P followed by tab until it hits Program Files, then v followed by tab until you hit vim, then _v followed by tabs until you hit _vimrc.
I find myself in the position where I want to create a new file in the same directory as the one that the open file is in. How do I create a new file in the directory of the open file in vim? Also, is there a a place where I can learn these things on my own? Googling didn't help.
From within Vim, new files are created like existing files are edited, via commands like :edit filename or :split filename. To persist them to disk, you need to (optionally type in contents and) persist them via :write.
Like a command prompt, Vim has a notion of current directory (:pwd lists it). All file paths are relative to it. You don't need to duplicate the path to your current file, there are some nice shortcuts for them: % refers to the current file, :h is a modifier for its directory, minus the file name (cp. :help filename-modifiers). So,
:e %:h/filename
:w
will create a new file named filename in the same directory as the currently open file, and write it.
Alternatively, some people like Vim to always change to the current file's directory. This can be configured by placing
:set autochdir
into your ~/.vimrc file (which is read on Vim startup). Then, above becomes simply
:e filename
:w
Finally, Vim has a great built-in :help. Learn to navigate and search it!
you should have a try with "nerdtree" plugin.
In the nerdtree window, you typed key m, and file operation choices will display to you
If you want to create a new file and also show it in the window next to your current file, you can try this:
:vsp newfile
The vsp stands for vertical split, and it splits the screen in half, one showing your current file, the other showing your new file (also works with just sp, which is a horizontal split).
Per #MartinLyne's comment above, this will create the file in the directory of the file in which you opened vim. To adjust for this, you can change the current working directory as follows:
:cd %:p:h
This command changes the current working directory to the directory of the active file, meaning that running the vsp command (or any of the commands above) will create the file in that directory.
I usually use:
:tabnew my-file
Then add some content and:
:w
It will create new tab with new file.
(I use Vim 8)
When you have opened vim in non existent location like
$ vim /etc/<some_folder/<next_folder>/file.cfg
then to create a new directory while being inside vim, just run in normal mode
:! mkdir -p /etc/<some_folder/<next_folder>
next save your doc as usual :w :x ZZ (whatever you like)
that's it
I'm quite late to the party, but another option is to open NERDtree with :E or :Explore (or its splitting alternatives :Vexplore/:Sexplore == :Vex/:Sex).
In NerdTree you can create a new file with %, and type the name. It will automatically open the file, and create it after you :w/save it.
This is for Gvim!
Enter this to see the current directory.
:cd
then change it with
:cd desktop/somefolder
then save or make new file there
:enew asd.cpp
now again see the file
:cd
With NERDtree
ma <FILENAME>
ma <DIRECTORY NAME> + /
Is it possible to have ctags generate tags for filenames as well? I would like to be able to jump to a file given a filename. :find seems to be awfully slow compare to tags...
Try running ctags -R --extra=f .
The --extra=f option tells ctags to:
"Include an entry for the base file name of every source file (e.g. "example.c"), which addresses the first line of the file."
When you open vim, you can the use :tag <filename> to jump to the first line of the file.
You can open the filename under the cursor with gf
http://vim.wikia.com/wiki/Open_file_under_cursor
You can also use cscope:
:cs find f <filename>
or, if you've setup cscope in vim as recommended in :help cscope, put the cursor on top of a filename, and press <C-_>f.
Have you tried setting your path and then using vim's 'gf' command?