Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file name that is inside the register in Ex mode in Vim

Tags:

vim

If I have full path inside a register(e.g. @a="/dir/file.txt") and I want to open the file in Ex mode.

I try :e @a that, but it doesn't work.

And I also try :e echo @a but it doesn't work either.

Obviously I can type :e CTRL-R a in Ex mode, it works.

But I'm wondering is there a way to get the content of register in Ex mode without use CTRL-R.

like image 649
1234 Avatar asked Sep 10 '25 06:09

1234


1 Answers

The :edit command takes a file specification. There are certain special characters (like % for the current file name; cp. :h cmdline-special), but the @reg notation does not work here.

Vim's evaluation rules are different than most programming languages. You need to use :execute in order to evaluate a variable (or expression); otherwise, it's taken literally; i.e. Vim uses the variable name itself as the argument.

So, the solution to your problem is

:execute 'edit' @a

If you don't want the special characters to apply to the register contents, a more robust (but longer) way is

:execute 'edit' fnameescape(@a)
like image 200
Ingo Karkat Avatar answered Sep 13 '25 08:09

Ingo Karkat



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!