3

How do I configure vim so that I can delete a tab one space at a time rather than the entire tab? I often want to do this when I'm trying to align multiple tests for an if statement. In the below example, condition3 is aligned as I want and condition2 is off by one space due to the tab.

if condition1 or \
    condition2 or \
   condition3:
    do something

I'm using vim 7.2.108 on OSX 10.6.7 and have the following mode line in my code file:

# ex: tabstop=4 softtabstop=1 shiftwidth=4 expandtab: 

FWIW, this mode line does allow me to use the arrow keys to navigate through a tab one space at a time.

Update: Here's the changes I've made to the example vimrc file from Bram Mooleanar:

map z :w<cr>
map q :q!<cr>
map m :make<cr>

set expandtab
set shiftwidth=4
set tabstop=4

set bs=2        " backspace over anything in insert mode
set showmatch       " display the matching bracket of the pair
set nowrap      " don't wrap line to fit window
set showmatch       " show matching paren, bracket, or brace
set ruler       " show current cursor position at bottom
set incsearch       " show next match as you type in search pattern
set ignorecase
set smartcase

syntax on       " enable syntax highlighting
DannyTree
  • 1,137
  • 2
  • 12
  • 16

3 Answers3

4

I would say put this in your .vimrc file:

set expandtab

Which basically turns tabs into spaces when you hit the tab key.

Dhaivat Pandya
  • 6,499
  • 4
  • 29
  • 43
  • Hi Dhaivat, in starting with a clean vimrc and test file to try your suggestion, I found my problem: when I have the softtabstop set, it prevented me from deleting a tab one space at a time. However, when I readded the vimrc file that I had been using (an example one that Bram Moolenaar wrote), my problem returns. – DannyTree Apr 27 '11 at 17:42
1

I would just hit x in normal mode to delete a single character. I'll also point out the great indent/python.vim script by Eric McSween. It doesn't really change the backspace behavior, but it allows you to do something more in line with PEP8---using Python's implied line continuation using parentheses. With the script, the code above would look like

if (condition1 or
    condition2 or
    condition3):
    do something
Sam Britt
  • 380
  • 1
  • 10
  • Thanks, Sam. I'll try your suggestions. Even simply enclosing the conditions within parens would solve my problem. – DannyTree Apr 28 '11 at 03:38
0

It looks like the problem is that you are using a mix of tabs and spaces to manage alignment. In most environments where there are many people working on the same files it is recommended that only spaces or only tabs are used.
That said you could map a key to delete the tab character and insert 3 spaces in its place by adding the following to your vimrc.

map <F1> s   <ESC>

If you want to do it in insert mode:

imap <F1> <BS>   

*note that there are three spaces after the ">"

Sam Brinck
  • 891
  • 1
  • 7
  • 11
  • Thanks, Sam. I don't know how my tabs and spaces are getting mixed up, but I'll experiment with these vim key mappings. – DannyTree Apr 28 '11 at 03:36