pass filename and current line to shell script

7

1

I would like to pass the file name and the current line number where my cursor is at, separated by colon, to an external shell script. For example, if am editing the file "foo.c" and I am currently on line 77, I'd like to call my script from vim with the argument "foo.c:77".

jpmuc

Posted 2012-08-28T16:14:55.667

Reputation: 173

Answers

7

An alternative to Karalos's answer:

:call system('echo ' . expand('%') . ':' . line('.'))

Of course, you replace "echo" with the name of your shell script.

The advantage of system() is that it returns the output of the command run, so you can capture it for further use in a Vim script if you need to.

You may need to look at the modifiers in :help expand() if you need to qualify/modify the filename in any way.

Heptite

Posted 2012-08-28T16:14:55.667

Reputation: 16 267

1Also: consider using shellescape() to protect any special characters. – Chris Johnsen – 2012-08-29T03:07:33.800

3

Although quite cumbersome, you could try the following

:!echo %:<Ctrl+R>=line(".")<CR><CR>

or you could use an intermediate variable

:let l=line(".")

and call

:!echo %:<Ctrl+R>=l<CR><CR>

Karolos

Posted 2012-08-28T16:14:55.667

Reputation: 2 304