Skip to content

Instantly share code, notes, and snippets.

@paxunix
Created February 2, 2013 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paxunix/4699245 to your computer and use it in GitHub Desktop.
Save paxunix/4699245 to your computer and use it in GitHub Desktop.
Vim hackery.

Annoyed that the Perl debugger thinks each entered line should be immediately evaluated? It's hard to paste in a multi-line sub definition that way. Add escape-newlines.vim into ~/.vimrc.

How it works:

  1. visually-select multiple lines
  2. Type "+\e. (If your mapleader is set to something else, use it instead of \).
  3. The contents of your clipboard will now be the selected lines with \ appended to each.
  • It won't affect your buffer.
  • It supports yanking into arbitrary registers.
  • You can run the command directly if you want to operate on a numerical range and pass it a register name: :20,+10EscapeNewlines "
" Function that appends a '\' to the end of each line in the given range.
" Does not affect the buffer.
function! s:EscapeNewlines(reg, line1, line2)
let lines = map(getline(a:line1, a:line2), 'v:val . (empty(v:val) ? "\\" : " \\")')
let reg = empty(a:reg) ? '"' : a:reg " default to unnamed register
execute 'let @' . reg . ' = join(lines, "\n") . "\n"'
echomsg a:line2 - a:line1 + 1 . ' lines escaped+yanked into register ' . reg
endfunction
command! -register -range EscapeNewlines call <SID>EscapeNewlines(<q-reg>, <line1>, <line2>)
" Define visual- and normal-mode maps to put the \ lines into the current
" register. These also work with ranges.
vnoremap <silent> <expr> <Leader>e ':EscapeNewlines ' . v:register . '<CR>'
nnoremap <silent> <expr> <Leader>e ':EscapeNewlines ' . v:register . '<CR>'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment