Friday, November 25, 2011

Re: Open a file whose name is selected after a search pattern in the current line

On 11/25/11 09:33, Fabio Spelta wrote:
> Suppose I have a file that reads like
>
> sometxt1 somemoretxt2 abc.TEXTIWANT3.cde
> sometxt2 somemoretxt5 fgh.TEXTIWANT6.ijk
>
> and so on.
>
> I want to extract the TEXTIWANTX part and open a file named
> TEXTIWANTX.txt by executing a command (hitting a key, whatever) when I
> am on a line formatted like that.

You might try something like this:

:nnoremap <f4> :sp <c-r>=matchstr(getline('.'), @/ .
'\d\+').'.txt'<cr><cr>

which, upon hitting <f4> grabs the current line you're on, looks
for the pattern you searched for (tacking on one or more digits),
tacks on the ".txt" extension, and then splits that file open in
a new window. Odd behaviors may exist if you have additional
path separators, file-glob characters, or spaces in your
file-path, but it should handle the bulk of cases.

-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

No comments: