Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read file and store contents in a register

Tags:

vim

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

like image 365
Kristian Avatar asked Aug 24 '12 11:08

Kristian


3 Answers

Try this:

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

It will read .vimrc to register x.

like image 70
kev Avatar answered Oct 14 '22 13:10

kev


Using external grep, you could do:

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

to place only the lines matching pattern from file filename in register x.

like image 33
romainl Avatar answered Oct 14 '22 13:10

romainl


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

To do the same with lines that matches a pattern, you can peform a global command (:h :g) before performing the copy to the register.

like image 25
mMontu Avatar answered Oct 14 '22 14:10

mMontu