简体   繁体   English

在 vim 中粘贴时转义字符

[英]Escape characters during paste in vim

I copy stuff from output buffers into C++ code I'm working on in vim.我将输出缓冲区中的内容复制到我在 vim 中处理的 C++ 代码中。 Often this output gets stuck into strings.通常这个输出会卡在字符串中。 And it'd be nice to be able to escape all the control characters automatically rather than going back and hand editing the pasted fragment.并且能够自动转义所有控制字符而不是返回并手动编辑粘贴的片段会很好。

As an example I might copy something like this:例如,我可能会复制这样的内容:

error in file "foo.dat"

And need to put it into something like this并且需要把它变成这样的东西

std::string expected_error = "error in file \"foo.dat\""

I'm thinking it might be possible to apply a replace function to the body of the last paste using the start and end marks of the last paste, but I'm not sure how to make it fly.我认为可能可以使用最后一次粘贴的开始和结束标记将替换功能应用于最后一次粘贴的主体,但我不确定如何让它飞起来。

UPDATE:更新:

Joey Mazzarelli sugested using Joey Mazzarelli sugested 使用

`[v`]h:%s/\%V"/\\"/g

after a paste.一贴后。

Since no explaination was given for what that was going and I initially found it a bit terse, but hard to explain in the comments I thought I'd put an explaination of what I think that does here:由于没有解释发生了什么,我最初觉得它有点简洁,但在评论中很难解释,我想我会在这里解释我的想法:

`[  : Move to start of last paste
v   : Start visual mode
`]  : Move to end of last paste
h   : adjust cursor one position left
:%  : apply on the lines of the selection
s/  : replace
\%V : within the visual area
"   : "
/   : with
\\" : \"
/g  : all occurrences

This seems like a good approach, but only handles the one character, ", I'd like it to handle newlines, tabs, and other things that might be expected to fall in text. (Probably not general unicode though) I understand that may not have been clear in the problem definition.这似乎是一个很好的方法,但只处理一个字符,“,我希望它处理换行符、制表符和其他可能会出现在文本中的东西。(虽然可能不是通用的 unicode)我理解这可能问题定义中没有明确。

Here are a couple of vimscript functions that should do what you want.这里有几个 vimscript 函数可以做你想做的事。

  • EscapeText() transforms arbitrary text to the C-escaped equivalent. EscapeText()将任意文本转换为 C 转义等效项。 It converts newline to \\n , tab to \\t , Control+G to \\a , etc., and generates octal escapes (like \\o032 ) for special characters that don't have a friendly name.它将换行符转换为\\n ,制表符转换为\\t ,Control+G 转换为\\a等,并为没有友好名称的特殊字符生成八进制转义\\o032 (如\\o032 )。

  • PasteEscapedRegister() escapes the contents of the register named by v:register , then inserts it into the current buffer. PasteEscapedRegister()转义由v:register命名的寄存器的内容,然后将其插入当前缓冲区。 (The register is restored when the function returns, so the function can be called repeatedly without escaping the register contents multiple times.) (当函数返回时寄存器会被恢复,因此可以重复调用该函数而无需多次转义寄存器内容。)

There are also a couple of key mappings included to make PasteEscapedRegister() easy to use interactively.还包含几个键映射,使PasteEscapedRegister()易于交互使用。 <Leader>P pastes escaped register contents before the cursor position, and <Leader>p pastes after. <Leader>P在光标位置前粘贴转义的寄存器内容, <Leader>p在其后粘贴。 Both can be prefixed with a register specification, like "a\\P to paste the escaped contents of register a.两者都可以使用寄存器规范作为前缀,例如"a\\P以粘贴寄存器 a 的转义内容。

Here's the code:这是代码:

function! EscapeText(text)

    let l:escaped_text = a:text

    " Map characters to named C backslash escapes. Normally, single-quoted
    " strings don't require double-backslashing, but these are necessary
    " to make the substitute() call below work properly.
    "
    let l:charmap = {
    \   '"'     : '\\"',
    \   "'"     : '\\''',
    \   "\n"    : '\\n',
    \   "\r"    : '\\r',
    \   "\b"    : '\\b',
    \   "\t"    : '\\t',
    \   "\x07"  : '\\a',
    \   "\x0B"  : '\\v',
    \   "\f"    : '\\f',
    \   }

    " Escape any existing backslashes in the text first, before
    " generating new ones. (Vim dictionaries iterate in arbitrary order,
    " so this step can't be combined with the items() loop below.)
    "
    let l:escaped_text = substitute(l:escaped_text, "\\", '\\\', 'g')

    " Replace actual returns, newlines, tabs, etc., with their escaped
    " representations.
    "
    for [original, escaped] in items(charmap)
        let l:escaped_text = substitute(l:escaped_text, original, escaped, 'g')
    endfor

    " Replace any other character that isn't a letter, number,
    " punctuation, or space with a 3-digit octal escape sequence. (Octal
    " is used instead of hex, since octal escapes terminate after 3
    " digits. C allows hex escapes of any length, so it's possible for
    " them to run up against subsequent characters that might be valid
    " hex digits.)
    "
    let l:escaped_text = substitute(l:escaped_text,
    \   '\([^[:alnum:][:punct:] ]\)',
    \   '\="\\o" . printf("%03o",char2nr(submatch(1)))',
    \   'g')

    return l:escaped_text

endfunction


function! PasteEscapedRegister(where)

    " Remember current register name, contents, and type,
    " so they can be restored once we're done.
    "
    let l:save_reg_name     = v:register
    let l:save_reg_contents = getreg(l:save_reg_name, 1)
    let l:save_reg_type     = getregtype(l:save_reg_name)

    echo "register: [" . l:save_reg_name . "] type: [" . l:save_reg_type . "]"

    " Replace the contents of the register with the escaped text, and set the
    " type to characterwise (so pasting into an existing double-quoted string,
    " for example, will work as expected).
    " 
    call setreg(l:save_reg_name, EscapeText(getreg(l:save_reg_name)), "c")

    " Build the appropriate normal-mode paste command.
    " 
    let l:cmd = 'normal "' . l:save_reg_name . (a:where == "before" ? "P" : "p")

    " Insert the escaped register contents.
    "
    exec l:cmd

    " Restore the register to its original value and type.
    " 
    call setreg(l:save_reg_name, l:save_reg_contents, l:save_reg_type)

endfunction

" Define keymaps to paste escaped text before or after the cursor.
"
nmap <Leader>P :call PasteEscapedRegister("before")<cr>
nmap <Leader>p :call PasteEscapedRegister("after")<cr>

This might at least get you started...这至少可以让你开始......

After pasting it in:粘贴进去后:

`[v`]h:%s/\%V"/\\"/g

You can obviously map that to something easier to type.您显然可以将其映射到更易于键入的内容。

While Joeys solution looks like it might be extensible to cover all the cases that I need, I thought I'd share my partial solution using vims python integration (Since I'm more familiar at python than vim script)虽然 Joeys 的解决方案看起来可以扩展以涵盖我需要的所有情况,但我想我会使用 vims python 集成分享我的部分解决方案(因为我对 python 比 vim 脚本更熟悉)

# FILE : tocstring.py
import vim
def setRegister(reg, value):
  vim.command( "let @%s='%s'" % (reg, value.replace("'","''") ) )

def getRegister(reg):
  return vim.eval("@%s" % reg )

def transformChar( map, c):
  if c in map:
    return map[c]
  return c

def transformText( map, text ):
  return ''.join( [ transformChar(map,c) for c in text ] )

cmap={}
cmap["\\"]="\\\\"
cmap["\n"]="\\n" 
cmap["\t"]=r"\\t"
cmap['"']="\\\""

def convertToCString( inregister, outregister ):
  setRegister(outregister, transformText( cmap, getRegister(inregister) ) )

Then in my .vimrc or other conf file I can put然后在我的 .vimrc 或其他 conf 文件中我可以放

# FILE cpp.vim
python import tocstring
# C-Escape and paste the currently yanked content
nmap <Leader>P :python tocstring.convertToCString("@","z")<CR>"zP
# C-Escape and paste the current visual selection
vmap <Leader>P "zd:python tocstring.convertToCString("z","z")<CR>"zP

It would be nice if I could the first function to work so that "a\\P pasted the transformed contents of the "a" register, and I assume this is doable using v:register somehow, but it escapes me.如果我可以让第一个函数工作以便“a\\P 粘贴“a”寄存器的转换内容,我认为这可以使用 v:register 以某种方式可行,但它使我逃脱了。

A version of this that works in the same way as Joeys solution could be crafted as一个与 Joeys 解决方案工作方式相同的版本可以制作为

nmap <Leader>P `[v`]"zd:python tocstring.convertToCString("z","z")<CR>"zP

Acknowledgement : This uses code from Can you access registers from python functions in vim for interacting with registers from vims python致谢:这使用了Can you access registers from python functions in vim for registers from vims python

for Java/JavaScript type of escaping one can use json_encode对于 Java/JavaScript 类型的转义可以使用json_encode

nmap <leader>jp :call setreg('e', json_encode(@+))\| normal "ep<CR>

json_encode(@+) - json encode content of register + (mapped to clipboard) json_encode(@+) - 对寄存器+内容进行 json 编码(映射到剪贴板)

setreg('e',...) - write it to register e setreg('e',...) - 将其写入注册e

normal "ep - paste content of register e normal "ep - 粘贴寄存器e内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM