我经常从一个文件中复制文本,或者从一个格式化为html、json或python的网页中复制文本。为了使用它,我打开一个空的Vim缓冲区并将其粘贴进去(用ctrl-v)。在这一点上,我可以手动设置语法或用途:filetype detect,但我希望它自动发生,就像打开一个文件一样。我尝试了一些autocmds,但我不知道如果语法关闭,但他们没有工作。我试过
au TextChangedI * :filetype detect
字符串但没有效果
lfapxunr1#
您可以创建一个单一的Map来完成所有事情:
" the function where everything happensfunction! MagicPaste() " create a new buffer, see :help :enew enew " grab the content of register "+ let reg = getreg("+", 1, 1) " figure out how many empty lines there are at the top " and remove them, if any let i = 0 while reg[i] == "" let i += 1 endwhile if i > 0 call remove(reg, 0, i - 1) endif " insert the the remaining lines in the buffer, " see :help setline() and :help getreg() call setline(1, reg) " perform filetype detection filetype detectendfunction" the custom mappingnnoremap <key> <Cmd>call MagicPaste()<CR>
" the function where everything happens
function! MagicPaste()
" create a new buffer, see :help :enew
enew
" grab the content of register "+
let reg = getreg("+", 1, 1)
" figure out how many empty lines there are at the top
" and remove them, if any
let i = 0
while reg[i] == ""
let i += 1
endwhile
if i > 0
call remove(reg, 0, i - 1)
endif
" insert the the remaining lines in the buffer,
" see :help setline() and :help getreg()
call setline(1, reg)
" perform filetype detection
filetype detect
endfunction
" the custom mapping
nnoremap <key> <Cmd>call MagicPaste()<CR>
字符串请注意,此代码片段假设您有一个适当的Vim,构建时支持剪贴板。
1条答案
按热度按时间lfapxunr1#
您可以创建一个单一的Map来完成所有事情:
字符串
请注意,此代码片段假设您有一个适当的Vim,构建时支持剪贴板。