Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search using the value returned by function

Tags:

vim

editor

I would like to search using current filename in this file. I already know getting current filename using :call expand("%:p:h"). So i'd like to using some command like /call expand("%:p:h") to search. I tried this but failed, because vim parse this function call literally.

like image 991
Frank Cheng Avatar asked Jun 07 '26 04:06

Frank Cheng


1 Answers

Generally, to insert in command-line (or just in insert mode) a value, returned by some function, you should use the following:

Ctrl+R=function_name()Enter

By the way, you said that expand("%:p:h") returns current filename, but this is wrong: actually it returns full path to current file, without filename. To get just a filename without a path, you should use expand("%:t"). Please read :help expand to get more info.

EDITED: In your case, you can use solution with search register @/ that @ib suggested: this is a safer way. But if i need just to find filename in the buffer, and if my filename does not contain some special characters, I would use Ctrl+R= instead.

So, the whole expression to search current filename, is:

/\VCtrl+R=expand('%:t')EnterEnter

P.S.

\V at the beginning of the search pattern means "very nomagic" mode. You can read about it here: :help \V

like image 161
Dmitry Frank Avatar answered Jun 10 '26 19:06

Dmitry Frank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!