Read file and store contents in register

Is there a way in vim to read a file, for example, using :r , and then save it in some register? and is there a way to do the same, but only with lines in the file that matches some pattern?

+4
source share
3 answers

Using external grep, you can do:

 :let @x = system('grep pattern filename') 

place only lines matching pattern from filename in register x .

+4
source

Try the following:

 :let @x = join(readfile("~/.vimrc"), "\n") 

He will read .vimrc to register x .

+13
source

You can create a new buffer with :new , read the file :r <filename> , upload the entire file for the register (say x) with gg"xyG" and delete the buffer with :q! . If you intend to perform these actions several times, you can create a mapping for it.

To do the same with strings that match the pattern, you can form a global command ( :h :g ) before making a copy to the register.

+2
source

All Articles