The git repo behind my blog.

specific-vim-config-per-git-repository.html 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  6. <meta name="HandheldFriendly" content="True" />
  7. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  8. <meta name="robots" content="index, follow" />
  9. <link href='https://phyks.me/theme/stylesheet/fonts.css' rel='stylesheet' type='text/css'>
  10. <link rel="stylesheet" type="text/css" href="https://phyks.me/theme/stylesheet/style.min.css">
  11. <link rel="stylesheet" type="text/css" href="https://phyks.me/theme/pygments/monokai.min.css">
  12. <link rel="stylesheet" type="text/css" href="https://phyks.me/theme/font-awesome/css/font-awesome.min.css">
  13. <link href="https://phyks.me/feeds/all.atom.xml" type="application/atom+xml" rel="alternate" title="Phyks' blog Atom">
  14. <link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon">
  15. <link rel="icon" href="/images/favicon.ico" type="image/x-icon">
  16. <!-- Chrome, Firefox OS and Opera -->
  17. <meta name="theme-color" content="#333">
  18. <!-- Windows Phone -->
  19. <meta name="msapplication-navbutton-color" content="#333">
  20. <!-- iOS Safari -->
  21. <meta name="apple-mobile-web-app-capable" content="yes">
  22. <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
  23. <!-- Microsoft EDGE -->
  24. <meta name="msapplication-TileColor" content="#333">
  25. <meta name="author" content="Phyks" />
  26. <meta name="description" content="I was looking for a way to add custom vim options on a per-repo basis. The standard way I saw for now, was adding some comments to change the Vim config on a per-file basis. There were some alternatives which were working for any project folder (and not only git …" />
  27. <meta name="keywords" content="">
  28. <meta property="og:site_name" content="Phyks' blog"/>
  29. <meta property="og:title" content="Specific Vim config per Git repository"/>
  30. <meta property="og:description" content="I was looking for a way to add custom vim options on a per-repo basis. The standard way I saw for now, was adding some comments to change the Vim config on a per-file basis. There were some alternatives which were working for any project folder (and not only git …"/>
  31. <meta property="og:locale" content="en_US.UTF-8"/>
  32. <meta property="og:url" content="https://phyks.me/2014/07/specific-vim-config-per-git-repository.html"/>
  33. <meta property="og:type" content="article"/>
  34. <meta property="article:published_time" content="2014-07-15 22:20:00+02:00"/>
  35. <meta property="article:modified_time" content=""/>
  36. <meta property="article:author" content="https://phyks.me/author/phyks.html">
  37. <meta property="article:section" content="ConfigPorn"/>
  38. <meta property="og:image" content="/images/profile.png">
  39. <title>Phyks' blog &ndash; Specific Vim config per Git repository</title>
  40. </head>
  41. <body>
  42. <aside>
  43. <div>
  44. <a href="https://phyks.me">
  45. <img src="/images/profile.png" alt="Phyks" title="Phyks">
  46. </a>
  47. <h1><a href="https://phyks.me">Phyks</a></h1>
  48. <p>I write about dev, FOSS, DIY and more, in French and English.</p>
  49. <ul class="social">
  50. <li><a class="sc-rss" href="feeds/all.atom.xml" target="_blank"><i class="fa fa-rss"></i></a></li>
  51. <li><a class="sc-envelope-o" href="mailto:phyks+blog@phyks.me" target="_blank"><i class="fa fa-envelope-o"></i></a></li>
  52. <li><a class="sc-github" href="http://github.com/phyks/" target="_blank"><i class="fa fa-github"></i></a></li>
  53. <li><a class="sc-gitlab" href="https://git.phyks.me/phyks" target="_blank"><i class="fa fa-gitlab"></i></a></li>
  54. </ul>
  55. </div>
  56. </aside>
  57. <main>
  58. <nav>
  59. <a href="https://phyks.me">Home</a>
  60. <a href="https://links.phyks.me">Bookmarks</a>
  61. <a href="/pages/hosted-tools.html">Tools</a>
  62. <a href="/archives.html">Archives</a>
  63. <a href="/pages/memo-autohebergement.html">Autohébergement</a>
  64. <a href="https://phyks.me/feeds/all.atom.xml">Atom</a>
  65. </nav>
  66. <article class="single">
  67. <header>
  68. <h1 id="specific-vim-config-per-git-repository">Specific Vim config per Git&nbsp;repository</h1>
  69. <p>
  70. Posted on July 15, 2014 in <a href="https://phyks.me/category/configporn.html">ConfigPorn</a>
  71. &#8226; 1 min read
  72. </p>
  73. </header>
  74. <div>
  75. <p>I was looking for a way to add custom vim options on a per-repo basis.
  76. The standard way I saw for now, was adding some comments to change the
  77. Vim config on a per-file basis. There were some alternatives which were
  78. working for any project folder (and not only git repos) but they were
  79. only working if you start Vim from the root of the project, or they were
  80. exploring the tree from local folder up to root (and needed a plugin).
  81. None of them were&nbsp;satisfactory.</p>
  82. <p>When I edit a file and need specific Vim configuration, it is usually
  83. inside a git repo. So, it is easy to know what is the root folder, and I
  84. just wanted to search for a <code>Vimrc</code> file in this folder. No
  85. complicated tree searching, working from anywhere inside the repo, no
  86. per-file specific&nbsp;configuration.</p>
  87. <p>First of all, the magic command I used to find the git root folder is
  88. <code>git rev-parse --show-top-level</code>.</p>
  89. <p>Then, all I had to do is wrap it correctly in my <code>.vimrc</code>:</p>
  90. <div class="highlight"><pre><span></span><span class="c">&quot; Git specific configuration</span>
  91. <span class="k">let</span> git_path <span class="p">=</span> system<span class="p">(</span><span class="s2">&quot;git rev-parse --show-toplevel 2&gt;/dev/null&quot;</span><span class="p">)</span>
  92. <span class="k">let</span> git_vimrc <span class="p">=</span> substitute<span class="p">(</span>git_path<span class="p">,</span> <span class="s1">&#39;\n&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span> . <span class="s2">&quot;/.vimrc&quot;</span>
  93. <span class="k">if</span> <span class="p">!</span>empty<span class="p">(</span>glob<span class="p">(</span>git_vimrc<span class="p">))</span>
  94. <span class="k">sandbox</span> exec <span class="s2">&quot;:source &quot;</span> . git_vimrc
  95. <span class="k">endif</span>
  96. </pre></div>
  97. <p>Note: I use sandbox to prevent arbitrary functions from being&nbsp;executed.</p>
  98. <p>This small code, added at the end of your <code>.vimrc</code> will just
  99. look for a <code>.vimrc</code> at the root the git repository and
  100. source it if possible. That&#8217;s exactly what I wanted&nbsp;:)</p>
  101. </div>
  102. <div class="tag-cloud">
  103. <p>
  104. </p>
  105. </div>
  106. </article>
  107. <footer>
  108. <p>
  109. &copy; 2017 - This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/4.0/">Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License</a>
  110. </p>
  111. <p>Powered by <a href="http://getpelican.com" target="_blank">Pelican</a> - <a href="https://github.com/alexandrevicenzi/flex" target="_blank">Flex</a> theme by <a href="http://alexandrevicenzi.com" target="_blank">Alexandre Vicenzi</a></p><p>
  112. <a rel="license"
  113. href="http://creativecommons.org/licenses/by-nc-sa/4.0/"
  114. target="_blank">
  115. <img alt="Creative Commons License"
  116. title="Creative Commons License"
  117. style="border-width:0"
  118. src="https://phyks.me/theme/img/cc/by-nc-sa.png"
  119. width="80"
  120. height="15"/>
  121. </a>
  122. </p> </footer>
  123. </main>
  124. <script type="application/ld+json">
  125. {
  126. "@context" : "http://schema.org",
  127. "@type" : "Blog",
  128. "name": " Phyks' blog ",
  129. "url" : "https://phyks.me",
  130. "image": "/images/profile.png",
  131. "description": "I write about dev, FOSS, DIY and more, in French and English."
  132. }
  133. </script>
  134. </body>
  135. </html>