Keyboard shortcut to regard the entire line as a filename, and :edit that file?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Keyboard shortcut to regard the entire line as a filename, and :edit that file?

Dun Peal
Hi there,

I'd like to setup a keyboard shortcut that would work similar to `gf`,
except it will regard the entire cursor line as the filename to open.

So for example, if I'm on a line that contains the text "foo bar
baz.", and hit the shortcut, the file "foo bar baz." - a new one if it
doesn't already exist - will be :edit'ed.

What's the best way to implement that?

Thanks, D.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Keyboard shortcut to regard the entire line as a filename, and :edit that file?

Tim Chase
On 11/14/11 18:47, Dun Peal wrote:
> I'd like to setup a keyboard shortcut that would work similar to `gf`,
> except it will regard the entire cursor line as the filename to open.
>
> So for example, if I'm on a line that contains the text "foo bar
> baz.", and hit the shortcut, the file "foo bar baz." - a new one if it
> doesn't already exist - will be :edit'ed.

I'd do a mapping something like

   :nnoremap <f4> :exec 'e '.getline('.')<cr>

(change the "e" to "sp" if you'd rather split the file in a new
window rather than edit in the window containing your list of files).

-tim


--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Keyboard shortcut to regard the entire line as a filename, and :edit that file?

Jean-Rene David-2
In reply to this post by Dun Peal
* Dun Peal [2011.11.14 19:50]:
> I'd like to setup a keyboard shortcut that would
> work similar to `gf`, except it will regard the
> entire cursor line as the filename to open.
>
> So for example, if I'm on a line that contains the text "foo bar
> baz.", and hit the shortcut, the file "foo bar baz." - a new one if it
> doesn't already exist - will be :edit'ed.

nmap <F2> :exe "e " . escape(getline('.'), ' *?\[]')<cr>

Note that the behavior is different from 'gf' in that 'gf' will not create the
file if it doesn't exist. But from your description this is what you want.


--
JR

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Keyboard shortcut to regard the entire line as a filename, and :edit that file?

Dun Peal
Thank you, Tim and Jean-Rene.

It seems Jean-Rene's is slightly better, since without the escape() I
get errors for trying to open a line that contains spaces. For
example, pressing <F4> on "foo bar baz." results in:

  E172: Only one file name allowed: e foo bar baz.

The escape() solution seems to work perfectly.

.D

On Nov 14, 8:23 pm, Jean-Rene David <[hidden email]> wrote:

> * Dun Peal [2011.11.14 19:50]:
>
> > I'd like to setup a keyboard shortcut that would
> > work similar to `gf`, except it will regard the
> > entire cursor line as the filename to open.
>
> > So for example, if I'm on a line that contains the text "foo bar
> > baz.", and hit the shortcut, the file "foo bar baz." - a new one if it
> > doesn't already exist - will be :edit'ed.
>
> nmap <F2> :exe "e " . escape(getline('.'), ' *?\[]')<cr>
>
> Note that the behavior is different from 'gf' in that 'gf' will not create the
> file if it doesn't exist. But from your description this is what you want.
>
> --
> JR

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
Reply | Threaded
Open this post in threaded view
|

Re: Keyboard shortcut to regard the entire line as a filename, and :edit that file?

Benjamin Fritz
In reply to this post by Dun Peal


On Nov 14, 6:47 pm, Dun Peal <[hidden email]> wrote:
> Hi there,
>
> I'd like to setup a keyboard shortcut that would work similar to `gf`,
> except it will regard the entire cursor line as the filename to open.
>

Map something to Vgf and you've got it. Or maybe 0v$gf (I'm not 100%
certain line-wise visual acts correctly with gf, but I expect it
does).

> So for example, if I'm on a line that contains the text "foo bar
> baz.", and hit the shortcut, the file "foo bar baz." - a new one if it
> doesn't already exist - will be :edit'ed.
>

gf doesn't do that. There are some suggestions for using :e with the
file name, but this will not search your 'path' option as gf does.

> What's the best way to implement that?
>

Probably you could use :find in place of :e, catch error 345 for when
the file was not found, and use :e as a fallback.

--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php