Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace text with current filename in VIM?

Tags:

file

replace

vim

I suppose my question in not that clear but let me try to explain it here.

Let's suppose I have opened a file named myfilename.java with the below content

public class test{
}

Now, what I want is to replace test with myfilename. Now to get the filename in vim I used :echo expand('%:r') which gave me myfilename.

Now, my question is how to do I use the above output and replace test with it and map it to a key for future use. What I need is may be something like:

:%s/test/above_output_from_command/g
like image 755
RanRag Avatar asked Jul 15 '12 13:07

RanRag


1 Answers

You need to add \= to tell Vim you're trying to call a function:

:%s/test/\=expand('%:r')/g

See :help sub-replace-expression.

like image 122
Andrew Marshall Avatar answered Oct 05 '22 04:10

Andrew Marshall