Reputation: 3746
I'd like to create, move around and append to other_file.py after calling a custom command from within this_file.py.
So for instance, I'd like to write several lines of text to a range of lines in the other file.
Is there some way I can basically enter :edit mode in other_file.py from this function? This would allow me to move around, search and append to other_file as though I were in it.
Here's a run through of where I'm at:
I am in this_file.py, and I have called:
:MyComm other_file:line_1/line_2/line_3
This activates the following vimscript:
function MyFunc(param_string)
let param_split = split(a:param_string,":")
let file_name = param_split[0] . ".py"
let lines = split(class_split[1],"/")
call system("touch " . file_name)
" Here is where I want to loop through the lines and use them in other_file.py
endfunction
:command -nargs=1 MyComm :call MyFunc(<f-args>)
Upvotes: 1
Views: 794
Reputation: 172510
You can continue going through the external commands, as you've started with touch
. So, you could use sed
or awk
to replace / append lines to the other file. With Vim's system()
command, you can pass stdin input, too. For example, if you want to copy lines from the current buffer, grab them with getline()
and pass them to system()
, either as a List or a String.
However, I would recommend avoiding external commands, and do the text editing tasks inside Vim. For that you just need to :execute 'split' other_file
, and do the edits just as you would interactively, using Ex commands or :normal
. Note that there are some special Ex commands (like :wincmd w
replacing <C-w>w
normal mode commands) that can make this more readable.
Upvotes: 1