我可以在Vim中复制一段代码和行号吗?

我可以在Vim中复制一段代码和行号吗?,vim,Vim,我尝试过使用鼠标,但行号就是无法选择 如何复制代码和行号,就像它们在Vim中的外观一样?顺便说一句,我更喜欢命令行方法。当您让终端处理鼠标时,鼠标可能在终端而不是GVIM中工作,即:set mouse= 对于一般解决方案,您必须临时预先设置行号,然后拖动/写入缓冲区。我使用以下功能: ":[range]Number[!] [{offset}] " Number all lines in range, or the entire buffer. " The

我尝试过使用鼠标,但行号就是无法选择


如何复制代码和行号,就像它们在Vim中的外观一样?顺便说一句,我更喜欢命令行方法。

当您让终端处理鼠标时,鼠标可能在终端而不是GVIM中工作,即:set mouse=

对于一般解决方案,您必须临时预先设置行号,然后拖动/写入缓冲区。我使用以下功能:

":[range]Number[!] [{offset}]
"           Number all lines in range, or the entire buffer.
"           The first line of the range is number 1, or the actual
"           line number in the buffer if [!] is given.
"
"           Numbering is done in the first column, with
"           right-aligned numbers. Existing numbering will be
"           overwritten, so you can re-apply this command over a
"           range already numbered.
"           The number column width is chosen based on the necessary
"           width.
function! s:Number( line1, line2, isAbsolute, offset )
    let l:offset = str2nr(a:offset)
    let l:startLine = (a:isAbsolute ? 1 : a:line1) - l:offset
    let l:width = len(a:line2 - l:startLine + 1)
    execute a:line1 . ',' . a:line2 . 'global/^/execute "substitute/^\\%(\\s*\\d\\+\\%( \\|$\\)\\)\\?\\(.*\\)$/\\=".string(printf("%' . l:width . 'd", line(".") - ' . l:startLine . ' + 1)) . " . (empty(submatch(1)) ? \"\" : \" \") . submatch(1)"'
    call histdel('search', -1)
endfunction
command! -bar -bang -range=% -nargs=? Number call setline(<line1>, getline(<line1>)) | call <SID>Number(<line1>, <line2>, <bang>0, <q-args>)

你的意思是语法突出显示和所有?如果只是纯文本,请使用cat-n file.txt纯文本即可。好的解决方案:如果您想复制语法高亮显示,您可以使用这里描述的TOhtml脚本
$ vim -c 'Number' -c 'write numbered.out' -c 'quit' file.in