title: How can I escape the % and # characters in a Vim command?
body: <p>I'm using Ack (<a href="https://github.com/mileszs/ack.vim" rel="noreferrer">https://github.com/mileszs/ack.vim</a>) with the <code>--literal</code> flag to search through projects in Vim. I noticed that whenever I search for a string with the <code>%</code> or <code>#</code> characters, the search doesn't match things as I'd expect it to. I did some research and found that it's because Vim will expand these characters in commands (<code>%</code> is the current file and <code>#</code> to something else, not sure what).</p>
  
  <p>This is pretty annoying behavior when performing a search, considering these symbols come up pretty often in code. Is there a way to escape them, preferably automatically, so that the search works as expected? My current mapping is: <code>nnoremap &lt;leader&gt;al :Ack --literal&lt;space&gt;</code>.</p>
  
  <p><strong>Example</strong></p>
  
  <p>Say I have a selector <code>#body</code> in a CSS file someplace and I want to find it. These are the things I've tried (that haven't worked):</p>
  
  <pre><code>:Ack --literal #body
  :Ack --literal \#body
  :Ack --literal "#body"
  :Ack --literal "\#body"
  </code></pre>
  
  <p>Any ideas why escaping wouldn't work as usual here, or what this is even searching for? I haven't had these examples match <em>anything</em>.</p>
  
  <p><strong>Solution</strong></p>
  
  <p>I've gotten it to work by double-escaping the characters. For example, <code>:Ack --literal "\\#body"</code> will show <code>:ack -H --nocolor --nogroup --column --literal "#body"</code> in the statusline of the result window and bring up the expected results. The quotes seem to be required as well.</p>