1

Besides type in cmdline directly, I also use

vnoremap <F2> :<c-u>exec join(getline("'<","'>"),"\n")<CR>
nnoremap <F2> :exec getline(".")<CR>
nnoremap <F3> :.w !sed 's/\r$// ; s/^\s*\$\s*\(.*\)\s*/\1/' \|bash -o pipefail<CR>

to run vim/shell command in current buffer. eg,

  • <F2> to run current line as vim command.
  • <F3> to run current line as shell command.
  • 4<F3> to run curent line and 3 lines below it as shell command.

So how can I disable expansion of %,

  • at cmdline, eg :!awk 'BEGIN { print 4 % 3} '
  • exec + getline()
2
  • 1
    simply escape it :) Oct 12, 2017 at 10:30
  • Yes, that is exactly what I did, escaping them, either individually, or s/%/\\%/g when multiple ones.
    – qeatzy
    Oct 12, 2017 at 10:39

1 Answer 1

5

escape(getline('.'), '%')

See :h escape() for more information

1
  • See also shellescape(), eg, let cmd = shellescape(join( getline(a:firstline,a:lastline), "\n"),1), other item that might be of interest is v:count, func-range, and function-range-example.
    – qeatzy
    Oct 12, 2017 at 13:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.