What is the fastest way to open a file in gvim - vim

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.

Related

Using VIM to find files in project

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.

In Vim tab-completion of file names doesn't work

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.

How do I stay within the scope of my project when leaving a file in vim?

I open up a project in Vim. Lets call it portfolio. I open up the index.php but now want to leave that and go to another file, controller.php. I learned that the command :e. will take me out but then it may lead me outside the scope of my project.
How do I stay within the scope of my project when leaving a file in vim?
If I understand your question correctly, you want :e. to show you the files in the same directory as the first file. If that's correct, and if the files are in one directory, use :cd path-to-your-project-directory
That will change your working directory to your project directory, and subsequently :e. will show the files in that directory.
Note too that once you use :e. (or :Explore :Texplore or :Sexplore), you can navigate through your directory hierarchy by navigating to subdirectories and pressing enter, or to the .. entry and pressing enter to move up a directory.
You may also be interested in the NERDTree plugin.
Your initial working directory in vim is the directory you're in when you start vim. If you're in a directory other than your project directory when you start vim, :e will not change your current directory to your project directory when you open a file, so if you just use :e to open different files, you'll have to specify the path each time.
eg: say your project path is /project/foo and you cd to /project and open vim. To open /project/foo/a.txt, you'll have to :e foo/a.txt. If you then want to open file /project/foo/b.txt, you'll have to :e foo/b.txt. :e doesn't change the current working directory in vim. If you cd /project/foo then start vim, you can just do :e a.txt and :e b.txt
You could also do :cd foo from within vim in the scenario above.
Hope this helps
I would say, just open controller.php with :sp controller.php you can then edit both files and switch between then with: ^w k and ^w j
I agree with other answers:
:e. provides a listing of the current working directory.
Plugins such as NERDTree build on this basic idea but also allow you to store bookmarks to commonly visited files and other cool features, see
If you augment this with plugins like bufkill, and tagbar you can actually build up quite a feature rich IDE where you can edit project files within the same context. I put together an installer script you can grab from here
Another alternative is the autochdir option which changes your working directory to that of the current buffer (so each time you change tabs/windows/buffers it changes the current directory to that of the file being edited). Link
It's not usually recommended because it doesn't play nicely with some plugins.

Show the list of the files inside the folder of the current file

Imagine I'm editing file, and I want to show the list of the files inside the folder who belongs the file I'm editing, to edit one of them.
How can I do that? Is there any way using FuzzyFinder?
Did you even read FuzzyFinder's documentation (:help fuzzyfinder)? Quickly opening nearby files is one of that plugin's main features.
Without installing anything, you can do:
:Ex[plore]
to open the netrw file tree. See :help netrw.
You can also do:
:e <Tab>
Add these lines to your ~/.vimrc to make command line completion even better:
set wildmenu
set wildmode=list:full
and read :help wildmenu and :help commandline-completion.
set autochdir is a useful option to add to your ~/.vimrc, by the way.
change vim current directory to current file's:
:cd %:h
then
FuzzyFinder can do what you want (pick and edit). (:FufFile) I have mapping :
nmap <Leader>ff :FufFile<cr>
NERDTree can do that as well.
Depends on what you mean by showing the file.
To include the list of files in the currently edited files, you can do something like:
:read !ls /path/to/file
(it can be shortened to :cd %:h | read !ls if you don't mind if vim changes it's current directory...)
If you want to pick another file to edit, I'd suggest to take a look at NerdTree plugin (here is a little intro). Or you can simply issue:
:cd %:h | e .

Vim :e starting directory?

I code in Vim, not an IDE.
My source code is often nested 2-3 directories deep.
~/foo$ find
xyz
bar/abc
bar/def
~/foo$ vim
// inside of vim
:e bar/abc
... some work ...
:e <-- is there a way I can have this :e start in ~/foo/bar instead of ~/foo ?
Basically, I want :e to start the directory in "pathname of last edited file"
Thanks!
There's a lot of reasons not to like autochdir as it messes up some plugins and if you end up doing :e ../../../foo.txt you are not gaining anything. Just as an idea try this cmap I knocked up
:cnoremap red edit <c-r>=expand("%:h")<cr>/
then you can type :red and get
:e /the/path/to/your/current/files/dir/
(edit: perhaps use z instead of red as there are commands that start with red)
To expand the topic, also check out the FuzzyFinder plugin and some custom mappings to rapidly jump to common files you are always editing. Eg
10 or so of your regular files should be no more than 2 keystrokes away. It helps if they are systematically named
Here's an idea I use for django.
http://code.djangoproject.com/wiki/UsingVimWithDjango#Mappings
Try the autochdir option. It will automatically change the current working directory to whatever file was most recently opened or selected. In .vimrc:
set autochdir
For more info, :help autochdir
To always change the working directory to the current file's directory I have this in my .vimrc:
if has("autocmd")
autocmd BufEnter * :lcd %:p:h
endif " has("autocmd")
Sorry, but vim's :edit command takes a path which is interpreted relative to the present working directory of the vim instance.
You do have a :cd command which you could use to :cd bar then work for a while, then :cd ...
Hope that help some.
Some time ago I asked questions related to this on the vim mailing list: http://www.mail-archive.com/vim_use#googlegroups.com/msg03266.html Maybe you will find useful tips in that thread.
I tested a lot of plugins, but since CLI based GUIs are not my taste, I simply ended up using standard vim with a few configuration settings.
As honk pointed out, this line sets the working directory to the same as the file your working on:
autocmd BufEnter * lcd %:p:h
My other tip is to use the wildmenu. It makes it easier to get an overview of the files in your current directory when you go :e and then TAB. I'm a python programmer so the last line shows how to hide auto generated files that the python interpreter spits out, but you could use it to hide java .class files or c .obj files or whatever.
set wildmode=list:longest
set wildignore=*.pyc,*pyo
:cd changes directory
:pwd prints the current one.
why not just :E? Explore directory of current file.
:help :E
This isn't exactly what you wanted, but check out NERDTree.
On vim/gVim I just have cd C:/blah/blah at the top of my vimrc. I imagine it works on all platforms.
I personally use vagrant for each project so one CD is enough, but I think you can get vim to use different config files too, -u flag I think.
Or map a key to each project you have so pressing Ctrl+F1 does cd path/to/project/1 and Ctrl+F2 does cd path/to/project/2 perhaps?
Note: I don't use any plugins

Resources