Open file from complete path under cursor in Vim - vim

If I have a file that contains a complete path for a file, is there a way to highlight the filename (using visual mode) and open the file (preferably in a split screen)?
Here is the behavior I would like: if the file name contains a / character, assume it is a full path (i.e. the current directory is root). Otherwise, use the current folder (i.e. default behavior). Is this possible?

Put the cursor on the filename and type gf (in command mode). Or use CTRL+W | CTRL+F to open in another window. See also :help gf (no, it's not your girlfriend).

gf command opens file under cursor.

Related

In Vim, how to copy content in file and use it in command?

For example, I open a file which includes
FATAL ERROR: Simulation failed. Please read /path/to/the/error/file.error
What I want to do is copy the path of the error file, which I've already done that by "v" into the visual mode and "y" to copy it, and open that file by splitting the window by ":sp /path/to/the/error/file.error".
My problem is: how to paste the copied path in command mode?
Vim also has a feature to open the filename under the cursor directly.
If you want to open it in a new split, then you can use Ctrl+W, f (or the equivalent
Ctrl+W, Ctrl+F).
See :help CTRL-W_f for more details.
The mapping to open the file under the cursor in the current window (not in a new split) is gf.
In command mode, use Ctrl-R, which is paste register, then " to paste the last yanked thing from the unnamed register.
to see your current registers, execute the following command :reg

Vim: How to open a file from current directory list without using arrow keys

Suppose I am editing a file using vim, and I go back to the file's current directory using :Ex, and I have a list of all the files I could open, I know arrow keys + Enter works, but is there a way to use : something to open a specific file? I tried :e filename but this goes directly back to the root of vim instead of the current directory.
Thanks.
The following is not a :-command, but it does the job and it can be in the muscle memory already:
You can move around the directory listing just like in a standard buffer. So you can /filename<Enter> to get to your file and <Enter> to open it. But typing whole filename can be rather cumbersome, so let's improve:
If there is something specific in the filename-baz, it will be enough to /baz<Enter><Enter>. And yet better, if you run vim with set incsearch and set hlsearch as many do, you'll see the search space narrow down to your filename, so you can easily get the prefix-search behavior of file commanders. Or even better, thanks to the coloring.
In case you can see the filename on the screen, then with EasyMotion, you can <Leader><Leader>w, then the usually two letters to get there and <Enter><Enter>.
tried :e filename but this goes directly back to the root of vim instead of the current directory.
This may happen because you are running vim from a different directory.
Suppose I run vim from my home directory, you will have to run :e /path/to/filename and :tabe /path/to/filename where the filepath is relative to the home directory.
you can open another file while vim is open with :tabe filename and to switch to the other file you type :tabn or :tabp for next and previous accordingly.
Maybe this link can help you

VIM: How to open another file from same location as an already open file

Let's say I am in my home directory. I open a file that is present in some deep nested directory structure:
vim /some/really/long/path/file.txt
Now, within vim, I want to start a vertical split with another file from that same long location (but I don't want to change the current directory to that path, I still want to be in the home directory).
A layman (like me) will do something like:
:vsp /some/really/long/path/file2.txt
But I want to find out from all you VIM geniuses out there, is there an easier way to do this?
On the same note, if I had multiple files already open residing in different paths, can VIM automatically assign some sort of internal variables to all the locations? And then when I want to start a new VSP or SP with one of the files from one of those locations, I simply use those internal variables?
Try this:
:vs %:p:h/otherfile
as %:p:h gives you the path of the current file.
See :help %:p for more info.
Edit another file in the same directory:
:vs %<Tab><C-w><C-w><C-w>file2<Tab>
With a mapping:
nnoremap <key> :vs <C-R>=expand('%:p:h')<CR>
If you like more navigation than typing the commands, a convenient option could be the use of the embedded Explore command, which opens a window with the list files in the directory of the file in current buffer. Then you can navigate with the cursors and press v when over the file you want to open in a vertical split.
A convenient way is to map the Explore command, like:
" open embedded file system navigator
map <leader>\ :Explore<cr>
Press <leader>\ and navigate to the file, then press v.
With some versions of vim, the Explore window stays open after pressing v -- in that case, if you want to avoid extra burden to close the Explore window, you can do first the vertical split with :vsp, invoke :Expore and open the desired file by pressing Enter.

How can I create a map combination using Tab in command mode?

I want to create a key combination that would copy the text under the cursor, open the :find command, yank the word and then press Tab to autocomplete to the first filename in the list, so (in 99% of cases) I would have just to press enter to open the file.
map <Leader>o yw:find <C-R>"<Tab>
However, when I press <Leader>o, I get :find FileName^I in the command line instead. How can I make it react the same way as if I pressed the key myself?
You need the wildcharm options:
set wildcharm=<C-z>
map <Leader>o yw:find <C-R>"<C-z>
See :help 'wildcharm'.
Here is a more solid, non-recursive, alternative that doesn't clobber the unnamed register for no reason:
nnoremap <leader>o :find <C-r><C-w><C-z>
The gf (goto file) command should do the same thing as your mapping. This opens the file whose name is under or after the cursor.
If not getting the expected behaviour, it's worth checking the contents of the isfname option (use set isfname? to check). This specifies a list of characters that are treated as valid characters for file path names.
It’s also worth checking / setting the contents of Vim’s path option which lists the directories which are searched when using gf, :find and similar commands, e.g., the default setting on MS Windows is .,,:
. searches relative to the directory of the file currently being edited
,, (empty string) searches the current directory; use the cd command to check (or set) your current directory.
See
help gf
help isfname
help path

Easily open include filenames in vim?

Basically, I'm editing files that have include file names and I want a quick way of opening the file without having to type the path/filename:
include('inc/doctype.inc.php');
Is there an easy way to do this? (Ideally, I'd like to use :tabnew to open the file in a new tab.)
Thanks.
Use the gf shortcut. Move your cursor on a path string, the exact cursor position is not important and then press gf in normal mode. gf stands for "goto file".
See vims help page with :h gf:
Uses the 'isfname' option to find out which characters
are supposed to be in a file name. Trailing
punctuation characters ".,:;!" are ignored.
Uses the 'path' option as a list of directory names
to look for the file. Also looks for the file
relative to the current file.
Uses the 'suffixesadd' option to check for file names
with a suffix added.
If the file can't be found, 'includeexpr' is used to
modify the name and another attempt is done.
To get back, use Ctrl-o in normal mode.
Note: This command brings the cursor position to older positions in the jump list. The opposite command is Ctrl-i which brings the cursor to newer positions in the jump list.
Put the cursor on the filename, then Ctrl+wgf
:h ctrl-w_gf

Resources