How do you open a file from within Vim? - vim

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.

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.

File completion while opening the file

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.

VIM: opening files in the same folder as the remote file in current buffer

When using the delightful netrw support in vim to open a file, opening directly works well, as in
:e scp://hostname//path/to/file
when I have a buffer open, I often need to load another file in the same directory that file is in. However, you can't change the working directory via
:cd scp://hostname//path/
And even with autochdir on, simply
:e another_file
doesn't work.
Is there a way of getting this or equivalent behavior? Key part is I want to be able to use a relative path to edit files.
I also failed to find a way (e.g. environment variable) to suggest VIM to look up relative scp:// path with.
Instead of editing a file with:
:e scp://hostname//path/to/file
you can list the parent directory with:
:e scp://hostname//path/to/
and use netrw to do the file navigation.
The :Rex command, allows you to open the last netrw listing so your workflow could be:
:e scp://hostname//path/to/ " list remote directory
/foo<CR> " search for a file
<CR> " edit that file
:Rex " re-list remote directory
/bar<CR> " search for another file
<CR> " edit that file
:Rex " re-list remote directory
" rince
" repeat

Why vim disallows globbing with :e command?

I use buffers as "tabs" in Vim, and open new files using :e[dit] command. Why can't I use globbing with :e command?
:e some_dir/*
E77: Too many file names
Just use :n instead:
:n some_dir/*
You can use globbing with :edit and other commands; however, there must be a single, unique result of the glob, because the :edit command only takes a single file. (How else should it display multiple matches in the single current window?)
If you want to edit multiple files sequentially, you can use :args or :argadd instead. Note that the :split command does not take multiple files, neither (probably because of the risk that many matches will inadvertently cause an impractically large number of window splits), but you could write your own :Split command that provides this functionality.
You should use :args or :argadd instead.
:args src/*
:tab all
First command loads all files from src/ folder to buffers and second command puts each buffer then into separate tabs.
:help arglist will give more information

Create a new file in the directory of the open file in vim?

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> + /

Resources