在 Vim 中将文本添加到不同的文件中

问题描述 投票:0回答:1

我正在尝试将 Vim 中的文本添加到另一个文件中。特别是当前行,但视觉选择也很好。最终我想将其映射到类似<leader>i

的东西。

在 bash 中我可以使用以下 sed 命令来实现此目的:

sed -i '1i some new text' myfile.md
所以我在 vim 中尝试了这个:

:execute "!sed -i '1i" .getline('.') "' myfile.md"
这有效,但是当当前行中有 

!

 时:
shell returned 2

然后我发现了 shellescape,并且我得到了以下命令来工作。现在看来我在转义字符上做了一些错误的事情,因为这会在字符串周围添加单引号。

:execute "!sed -i \"1i " . shellescape(getline('.'), 1) . "\" myfile.m
我错过了什么?有更好的办法吗?

bash sed vim txt
1个回答
0
投票
function! PrependLine() update " Save the current file to avoid problems with switching buffers yank " copy the current line to the default register edit myfile.md " Open a new buffer or switch to exiting one 1put! " Paste content of the default register before the 1st line update " Save the file edit # " Return to the previous file endfunction map <leader>i :call PrependLine()<CR>
唯一的问题是——文件名是固定的。 Vim 宏没有参数。功能确实有。

© www.soinside.com 2019 - 2024. All rights reserved.