Vim Tips Wiki

Set options with automatic escaping of spaces

Redirected from VimTip1155

1,623pages on
this wiki
Add New Page
Add New Page Talk0
Tip 1155 Printable Monobook Previous Next

created 2006 · complexity basic · author Salman Halim · version 6.0

This function/command works just like the built-in :set except that it escapes out the spaces on the rhs, making things a bit more easy:

Regular set:

set fp=par\ 70j

Our set:

Set fp=par 70j

Just like the regular :set, multiple options may still be specified:

Set fp=par 70j tw=100 ai! ai?

The only known caveat is in single word options:

Set fp=par 70j ai

What you'll end up with is 'fp' set to 'par 70j ai' because Set uses the presence of the =, ! or ? symbols to differentiate the current word as the start of a new option rather than part of the last one.

Of course, there is no real reason to use Set unless there is an obscure set, such as:

Set mp=texify -b -p --src-specials %


set mp=texify\ -b\ -p\ --src-specials\ %

Of course, there is:

let &mp='texify -b -p --src-specials %'

I like the ability to use Set, however; requires much less thought and planning: if I do a regular 'set' and get an error, I just go up a line in the command-line history and change the 'set' to 'Set' and forget it.

function! Set( ... )
  let result = ''
  for i in a:000
    if ( i !~ '[=!?]' )
      let result .= '\'
    " Escaping out any existing spaces takes care of the case where we passed in escaped spaces.
    let result .= ' ' .escape( i, ' ' )
  execute 'set' .result
com! -nargs=+ -complete=option Set call Set( <f-args> )


If I'm reading that right, it can't correctly preseve multiple spaces on the original line. That may sometimes be a problem. Multiple consecutive spaces, I mean.

You are correct. I had toyed with the idea of passing everything in as one big argument and parsing through it myself (match() and strpart() or something), but I just couldn't think of an example from my personal usage where I ever passed in anything but single spaces.

Also on Fandom

Random Wiki