History Report a problem
Article Edit this page Discussion

Autoconvert leading eight spaces to tab when type or paste in Makefile

From Vim Tips Wiki

Jump to: navigation, search
 

Tip 1434Previous TipNext Tip

Created: December 7, 2006 Complexity: intermediate Author: Yakov Lerner Minimum version: 6.0 Karma: 134/37 Imported from: Tip#1434


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

[edit] Comments

 

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

:s/^ \{8}/^V<tab>/

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


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'.


Rate this article:

Share this article:

Hubs Highlights International Sites Wikia messages
Entertainment
Gaming
Cartoons & Comics
Science Fiction
Hobbies
Sports
See all...
Grand Theft Auto
Doctor Who
Legend of Zelda Wiki
Terminator Wiki
Everquest II Wiki
Mystery Science Theater 3000
German
Spanish
Chinese
Japanese
More...
Wikia is hiring for several open positions
Send this article to a friend
"Autoconvert leading eight spaces to tab when type or paste in Makefile"
 
 
Hi!

I thought you'd like this page from Wikia!

http://vim.wikia.com

Come check it out!
Send confirmation