Wikia

Vim Tips Wiki

Changes: Toggle between tabs and spaces

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 12: Line 12:
 
|category2=
 
|category2=
 
}}
 
}}
I write code for a large company and often work with other developer's code. I personally don't choose to use all the language/formatting stuff in Vim, but I do like to quickly switch between using tabs and using spaces when I hit the <TAB> key (to integrate with the "current" file formatting).
+
I write code for a large company and often work with other developer's code. I personally don't choose to use all the language/formatting stuff in Vim, but I do like to quickly switch between using tabs and using spaces when I hit the Tab key (to integrate with the "current" file formatting).
   
Here is a snippit from my vimrc which enables my default settings (using 4 spaces for every <TAB>) and a mapping to <F9> to toggle the settings:
+
Here is a snippit from my vimrc which enables my default settings (using 4 spaces for every tab) and a mapping to <F9> to toggle the settings:
   
 
<pre>
 
<pre>
Line 23: Line 23:
 
" allow toggling between local and default mode
 
" allow toggling between local and default mode
 
function TabToggle()
 
function TabToggle()
if &amp;expandtab
+
if &expandtab
 
set shiftwidth=8
 
set shiftwidth=8
 
set softtabstop=0
 
set softtabstop=0
Line 33: Line 33:
 
endif
 
endif
 
endfunction
 
endfunction
nmap &lt;F9&gt; mz:execute TabToggle()&lt;CR&gt;'z
+
nmap <F9> mz:execute TabToggle()<CR>'z
 
</pre>
 
</pre>
   
Line 46: Line 46:
 
" allow toggling between local and default mode
 
" allow toggling between local and default mode
 
function TabToggle()
 
function TabToggle()
if &amp;expandtab
+
if &expandtab
 
set shiftwidth=8
 
set shiftwidth=8
 
set softtabstop=0
 
set softtabstop=0
Line 56: Line 56:
 
endif
 
endif
 
endfunction
 
endfunction
nmap &lt;F9&gt; mz:execute TabToggle()&lt;CR&gt;'z
+
nmap <F9> mz:execute TabToggle()<CR>'z
 
</pre>
 
</pre>
   
Line 74: Line 74:
 
" allow toggling between local and default mode
 
" allow toggling between local and default mode
 
function! TabToggle()
 
function! TabToggle()
if &amp;expandtab
+
if &expandtab
 
set shiftwidth=8
 
set shiftwidth=8
 
set softtabstop=0
 
set softtabstop=0
Line 84: Line 84:
 
endif
 
endif
 
endfunction
 
endfunction
nmap &lt;F9&gt; mz:execute TabToggle()&lt;CR&gt;'z
+
nmap <F9> mz:execute TabToggle()<CR>'z
 
</pre>
 
</pre>
   

Latest revision as of 09:24, September 29, 2008

Tip 785 Printable Monobook Previous Next

created September 9, 2004 · complexity intermediate · author Paul Braman · version 6.0


I write code for a large company and often work with other developer's code. I personally don't choose to use all the language/formatting stuff in Vim, but I do like to quickly switch between using tabs and using spaces when I hit the Tab key (to integrate with the "current" file formatting).

Here is a snippit from my vimrc which enables my default settings (using 4 spaces for every tab) and a mapping to <F9> to toggle the settings:

" virtual tabstops using spaces
set shiftwidth=4
set softtabstop=4
expandtab
" allow toggling between local and default mode
function TabToggle()
  if &expandtab
    set shiftwidth=8
    set softtabstop=0
    noexpandtab
  else
    set shiftwidth=4
    set softtabstop=4
    expandtab
  endif
endfunction
nmap <F9> mz:execute TabToggle()<CR>'z

CommentsEdit

I like this function for the same reasons, however, I encountered an error with the implementation of it which required me to make a few minor changes. The expandtab and noexpandtab were giving me errors on the Unix hosts, so I had to add the keyword "set" in front of them in three places, as shown below:

" virtual tabstops using spaces
set shiftwidth=4
set softtabstop=4
set expandtab
" allow toggling between local and default mode
function TabToggle()
  if &expandtab
    set shiftwidth=8
    set softtabstop=0
    set noexpandtab
  else
    set shiftwidth=4
    set softtabstop=4
    set expandtab
  endif
endfunction
nmap <F9> mz:execute TabToggle()<CR>'z

Grrr...that was just a mistake in my copying it into the Tip. My implementation has the "set" in front of them as well. Mea culpa!


Here is a small add_on that allows to easily change (once instead of 4 times) the local tab value. And function is changed to function! to allow local tests (:w + :so %) .

" virtual tabstops using spaces
let my_tab=4
execute "set shiftwidth=".my_tab
execute "set softtabstop=".my_tab
set expandtab
" allow toggling between local and default mode
function! TabToggle()
  if &expandtab
    set shiftwidth=8
    set softtabstop=0
    set noexpandtab
  else
    execute "set shiftwidth=".g:my_tab
    execute "set softtabstop=".g:my_tab
    set expandtab
  endif
endfunction
nmap <F9> mz:execute TabToggle()<CR>'z

Around Wikia's network

Random Wiki