Say in my C head file I wanna include another file which has not being created yet:
#include "AnotherFile.h" /*not being created yet*/
Now, I select the file in Visual Mode,
#include "AnotherFile.h
"
How to create a new file with the name of what I've selected? I mean,
:e {something that refers to what I selected}
The closest I can think of is to create a function:
function! W() range
execute "e " . getline("'<")[getpos("'<")[2]-1:getpos("'>")[2]]
endfu
You can then select the word and type :call W()
+ enter, which should open the new buffer.
EDIT The function above does not work without errors if the buffer containing the #include
is modified. In such case, the following function is suited better:
function! W() range
let l:fileName = getline("'<")[getpos("'<")[2]-1:getpos("'>")[2]]
new
execute "w " . l:fileName
endfu
EDIT 2 You can also try to type :e <cfile>
(see :help <cfile>
).
EDIT 3 Finally, under :help gf
you find hidden
If you do want to edit a new file, use: >
:e <cfile>
To make gf always work like that:
:map gf :e <cfile><CR>
In Command-line mode CTRL-R
followed by register "name" inserts the contents of specified register.
Assuming you have just selected the file name, press y :e SPACE CTRL + R" ENTER which means:
y
-- yank selected text into unnamed register:e
+ SPACE -- enter Command-line mode and start typing your :edit
commandCTRL-R"
-- insert just yanked textSee :help c_CTRL-R
, :help registers
.
BTW, CTRL-R
does the same in insert mode too, I do use it often. See :help i_CTRL_R
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With