How can I setup file names completion while I am opening the file.
For example:
:o ~/.vimr<tab>
I want to get .vimrc file, but get ^I char if I press the Tab-key.
I use MacVim.
How about using :edit instead of :o? Apparently, :o doesn't take a file path as an argument.
Related
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.
I'm having an issue with vim while I'm trying to remotely edit a sqlrpgle file in AS/400. I'm using the next command to open the file in vim:
:e ftp://myusername#mydomain/mylibrary/mymember.myfile
The file opens up but is filled with # characters instead of source code. Am I calling it the wrong way?
I tried writing the command this way but it failed:
:e ftp://myusername#mydomain/mylibrary/mymember.myfile.sqlrpgle
The "#" characters (0x40) are EBCDIC spaces.
Enable ASCII transfer mode with the following netrw option:
:let g:netrw_ftpmode="ascii"
Edit files using the following format:
:e ftp://user#host/library/file.member
I know how to open a file in Vim from a terminal (vim fileName). What I can't figure out is how to open a file when I'm already within Vim.
I tried :r fileName, but that appears to read (or append) the file into the unsaved buffer I have open. It creates a new file, because when I try to write it with :w, it asks for a filename.
:e <filename>
or
:Ex <directory>
lets you browse for the file from the given directory.
:Ex on its own will open the pwd.
:enew
will create an empty buffer.
this vim command you won't forget:
:Sex
if you want to point to certain dir, then :Sex <dir>
Also, to open multiple files (or just one, so I tend to use this for opening a single file, since :e fails to open multiple files)
:n file1 file2
:n resets the argument list so it is as if you had entered them on the command line (so commands like :rew will work with this list), but :e does not.
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?