Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 748483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:19:32+00:00 2026-05-14T14:19:32+00:00

I copy stuff from output buffers into C++ code I’m working on in vim.

  • 0

I copy stuff from output buffers into C++ code I’m working on in vim.
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

`[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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T14:19:32+00:00Added an answer on May 14, 2026 at 2:19 pm

    Here are a couple of vimscript functions that should do what you want.

    • EscapeText() transforms arbitrary text to the C-escaped equivalent. 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.

    • PasteEscapedRegister() escapes the contents of the register named by v:register, then inserts it into the current buffer. (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. <Leader>P pastes escaped register contents before the cursor position, and <Leader>p pastes after. Both can be prefixed with a register specification, like "a\P to paste the escaped contents of register 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>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 365k
  • Answers 365k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can use the data in the /proc filesystem to… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer Do you still get the error when you use a… May 14, 2026 at 3:57 pm
  • Editorial Team
    Editorial Team added an answer You can use array_chunk to create a single array comprised… May 14, 2026 at 3:57 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.