Vim Tips Wiki
(Adding categories)
(Change <tt> to <code>, perhaps also minor tweak.)
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
  +
{{TipNew
If you save a file using Vim or MacVimWhen and try to open it with Quicklook, the characters won't display right. This is because Mac OS uses an extended attribute to determine the encode of a file.
 
  +
|id=1654
  +
|previous=1653
  +
|next=1655
  +
|created=June 5, 2010
  +
|complexity=basic
  +
|author=Dnlcorrea
  +
|version=7.0
  +
|subpage=/201006
  +
|category1=Encoding
  +
|category2=Mac OS X
  +
}}
 
If you save a file using Vim or MacVim and try to open it with Quicklook, the characters will not display correctly. That is because Mac OS uses an extended attribute to determine the encoding of a file.
   
Just add this in your .gvimrc:
+
To fix, add this to your [[vimrc]]:
  +
<pre>
 
au BufWritePost * :if &fenc=='utf-8' || (&fenc=='' && &enc=='utf-8') | exec "silent !xattr -w com.apple.TextEncoding 'UTF-8;134217984' \"%\"" | endif
  +
</pre>
   
 
This will append the extended attribute in every file you save with a UTF-8. Note that <code>%</code> is in double quotes to escape spaces for the shell. The <code>:silent</code> command prevents Vim from displaying the results (which are empty).
<code> au BufWritePost * :silent !xattr -w com.apple.TextEncoding 'UTF-8;134217984' "%" </code>
 
   
 
If you want this for <code>.txt</code> files only, use the following instead:
This will append the extended attribute in every file you save. Note that <%> is between <">, for escaping spaces in the shell. The :silent command prevents vim from printing the results (which are empty) and annoy you.
 
  +
<pre>
 
au BufWritePost *.txt :if &fenc=='utf-8' || (&fenc=='' && &enc=='utf-8') | exec "silent !xattr -w com.apple.TextEncoding 'UTF-8;134217984' \"%\"" | endif
  +
</pre>
   
  +
Using the command <code>ls -l</code> will show '@' in the file's attributes, for example:
If you want this for .txt files only, you could do
 
  +
<pre>
 
-rw-r--r--@ 1 daniel staff 15 Jun 5 10:12 lol.txt
  +
</pre>
   
  +
==Comments==
<code> au BufWritePost txt :silent !xattr -w com.apple.TextEncoding 'UTF-8;134217984' "%" </code>
 
  +
{{todo}}
 
  +
* Use shellquote instead of surrounding with spaces manually?
If you do a `ls -l`, you'll notice the '@' in the file's attributes:
 
  +
* explain the 134... number. What the heck is it for?
 
<code> -rw-r--r--@ 1 daniel staff 15 Jun 5 10:12 lol.txt </code>
 
 
--[[User:Dnlcorrea|Dnlcorrea]] 16:05, June 5, 2010 (UTC)--[[User:Dnlcorrea|Dnlcorrea]] 14:03, June 5, 2010 (UTC)
 
[[Category:Mac OS X]]
 

Revision as of 06:44, 13 July 2012

Tip 1654 Printable Monobook Previous Next

created June 5, 2010 · complexity basic · author Dnlcorrea · version 7.0


If you save a file using Vim or MacVim and try to open it with Quicklook, the characters will not display correctly. That is because Mac OS uses an extended attribute to determine the encoding of a file.

To fix, add this to your vimrc:

au BufWritePost * :if &fenc=='utf-8' || (&fenc=='' && &enc=='utf-8') | exec "silent !xattr -w com.apple.TextEncoding 'UTF-8;134217984' \"%\"" | endif

This will append the extended attribute in every file you save with a UTF-8. Note that % is in double quotes to escape spaces for the shell. The :silent command prevents Vim from displaying the results (which are empty).

If you want this for .txt files only, use the following instead:

au BufWritePost *.txt :if &fenc=='utf-8' || (&fenc=='' && &enc=='utf-8') | exec "silent !xattr -w com.apple.TextEncoding 'UTF-8;134217984' \"%\"" | endif

Using the command ls -l will show '@' in the file's attributes, for example:

-rw-r--r--@ 1 daniel  staff  15 Jun  5 10:12 lol.txt

Comments

 TO DO 

  • Use shellquote instead of surrounding with spaces manually?
  • explain the 134... number. What the heck is it for?