Vim Tips Wiki
(Change to TipImported template + severe manual clean)
(Adding categories)
 
(5 intermediate revisions by 2 users not shown)
Line 2: Line 2:
 
{{TipImported
 
{{TipImported
 
|id=1434
 
|id=1434
|previous=1433
+
|previous=1432
 
|next=1435
 
|next=1435
|created=December 7, 2006
+
|created=2006
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Yakov Lerner
 
|author=Yakov Lerner
 
|version=6.0
 
|version=6.0
 
|rating=134/37
 
|rating=134/37
  +
|category1=
  +
|category2=
 
}}
 
}}
 
This tip is for those who edit Makefiles by hand.
 
This tip is for those who edit Makefiles by hand.
Line 14: Line 16:
 
Makefiles punish you if instead of single tab somebody puts eight spaces in your makefile (happens when pasting between Makeiles). This script guards against this annoying pitfall.
 
Makefiles punish you if instead of single tab somebody puts eight spaces in your makefile (happens when pasting between Makeiles). This script guards against this annoying pitfall.
   
The following script auto-converts 8 spaces at the beginning of line (only in Makefiles, and only at the beginning of line) into tab, as you type or paste. It is suggested to have <tt>:set list</tt> in Makefiles, too. This script will only work if <tt>:set nopaste</tt> is set.
+
The following script auto-converts 8 spaces at the beginning of line (only in Makefiles, and only at the beginning of line) into tab, as you type or paste. It is suggested to have <code>:set list</code> in Makefiles, too. This script will only work if <code>:set nopaste</code> is set.
   
 
<pre>
 
<pre>
 
" In Makefile, automatically convert eight spaces at the beginning
 
" In Makefile, automatically convert eight spaces at the beginning
 
" of line to tab, as you type (or paste).
 
" of line to tab, as you type (or paste).
au FileType make :inoremap &lt;buffer&gt;&lt;silent&gt;&lt;space&gt; &lt;space&gt;&lt;c-o&gt;:call MapSpaceInMakefile()&lt;cr&gt;
+
au FileType make :inoremap <buffer><silent><Space> <Space><c-o>:call MapSpaceInMakefile()<CR>
 
function! MapSpaceInMakefile()
 
function! MapSpaceInMakefile()
 
" if this space is 8th space from the beginning of line, replace 8 spaces with
 
" if this space is 8th space from the beginning of line, replace 8 spaces with
Line 34: Line 36:
   
 
==Comments==
 
==Comments==
You don't need a script for this! Use the subsitution command:
+
You don't need a script for this! Use the substitution command:
 
<pre>
 
<pre>
:s/^ \{8}/^V&lt;tab&gt;/
+
:%s/^ \{8}/^V<Tab>/
 
</pre>
 
</pre>
   
^V&lt;tab&gt; means pressing CTRL-V and the press the &lt;tab&gt;-key, or s/^ \{8}/^Q&lt;tab&gt;/ on windowz.
+
^V<Tab> means pressing CTRL-V and the press the <Tab>-key, or s/^ \{8}/^Q<Tab>/ on Windows.
   
 
----
 
----
Line 48: Line 50:
   
 
----
 
----
  +
[[Category:Make]]

Latest revision as of 23:42, 27 January 2014

Tip 1434 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Yakov Lerner · version 6.0


This tip is for those who edit Makefiles by hand.

Makefiles punish you if instead of single tab somebody puts eight spaces in your makefile (happens when pasting between Makeiles). This script guards against this annoying pitfall.

The following script auto-converts 8 spaces at the beginning of line (only in Makefiles, and only at the beginning of line) into tab, as you type or paste. It is suggested to have :set list in Makefiles, too. This script will only work if :set nopaste is set.

" In Makefile, automatically convert eight spaces at the beginning
" of line to tab, as you type (or paste).
au FileType make :inoremap <buffer><silent><Space> <Space><c-o>:call MapSpaceInMakefile()<CR>
function! MapSpaceInMakefile()
  " if this space is 8th space from the beginning of line, replace 8 spaces with
  " one tab (only at the beginning of file)
  let line = getline('.')
  let col = col('.')
  if strpart(line, 0, 8) == ' '
    let new = "\t" . strpart(line,8)
    call setline('.', new )
  endif
  return ""
endfunction

Comments[]

You don't need a script for this! Use the substitution command:

:%s/^ \{8}/^V<Tab>/

^V<Tab> means pressing CTRL-V and the press the <Tab>-key, or s/^ \{8}/^Q<Tab>/ on Windows.


Your command above does not fix as you type.


A competent programmer should not err when typing. It is more likely to occur on paste, when one generally will ':set paste'.