1

There are two features related to indenting that I find very helpful in Textmate, and I'm looking to find the closest thing in vim. First, I can tab to the correct level of indentation, especially in python. For example:

enter image description here

[After hitting tab once]

enter image description here

On the other hand (assuming all space-chars to the left of the cursor), using backspace in Textmate goes back a full tab-stop if it is "on" a tab or a space until it hits an even tab (for example, if tabwidth=4 and I am at column 6, pressing backspace would go to 5, then 4, then 0. Here is an example video of the two: https://gyazo.com/2f1d2731a9d243448350b942eac29524

What would be the closest way to emulate this behavior in vim? I feel like it would only be most relevant in insert mode.

1
  1. The indentation level is automatically adjust (given that you have set autoindent and such) upon entering Normal mode commands o, O, S, cc etc. If, for some reason, you're already in Insert mode on empty line #4, you still can press <Esc>S. However, the chances are you're misusing Vim, because you violate the rules (a) leave from the Insert mode ASAP; and (b) use the most appropriate Normal mode command to re-enter it. Therefore you should (almost) never see the picture #1 in Vim.

  2. An indentation level is changed by <{motion}, >{motion}, [count]<< and [count]>> in Normal mode, and {Visual}> and {Visual}<in Visual mode. Specifically for Insert mode there are also <C-D> and <C-T>.

That should be enough to deal with this thing, however, if you really miss that "backspace" you can try to emulate such behaviour. For example,

inoremap <expr><BS> searchpos('^\s*\%#', 'bn')[1] ? '<C-D>' : '<BS>'

That is, if there are only spaces and tabs before the cursor position then use <C-D> instead of <BS>.

| improve this answer | |
1

For the first part of your question:

First, I can Tab to the correct level of indentation, especially in python.

In Vim you can use Ctrl+F to trigger automatic indentation, be it through 'indentexpr' or 'cindent'.

(This is usually implemented by adding the ^F key to 'indentkeys' or 'cinkeys'. See :help i_CTRL-F for more details.)

For your specific Python example, it works exactly as you describe it.

But remember that Vim's Python indentation will only set indentation for selected lines, such as those following a line ending in : or a line with a keyword such as else: or elif: which needs to be dedented from the block that precedes it. So Ctrl+F will only work on those lines that Python indentation will have an opinion on. That's the case with this particular line, which follows a for statement. But it won't indent you to a block if it's to match a simple command that's there, in that case you'll have to indent manually (with Tab or CTRL+T) or use o in Normal mode to have 'autoindent' keep same indentation as the preceding line.

| improve this answer | |
1

Judging from the images, you want 4 spaces for tab. Put the following in your .vimrc:

set tabstop=4                         " Redefine tab as four spaces
set softtabstop=4                     " Number of spaces in tab when editing
set shiftwidth=4                      " To change the number of space characters inserted for indentation
set autoindent                        " Auto indent
set smartindent                       " Does the right thing (mostly) in programs
filetype indent plugin on             " Enables filetype detection, add after all plugins if using Vundle

softtabstop=4 takes care of the backspace property that you demand. Other settings in the above are for indentation. With shiftwidth=4, autoindent and smartindent indentation in python will be taken care of I think, vim will automatically determine filetype (filetype indent plugin on) and add required number of tabs while you are editing your file.

If further, you want each tab-press to introduce 4 actual spaces in your document, you should further add this to your .vimrc:

set expandtab                         " Four spaces for tabs everywhere

See https://stackoverflow.com/a/1878983/9445700.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.