2

Does Vim have a command equivalent to Emacs' Ctrl-I?

Emacs has an extremely useful shortcut "C-I" which fixes indentation of the current line (or selected lines.)

For example, suppose you write this tiny code on Emacs:

    def foo
  print "boo"
end

and you are selecting the whole 3 lines. You press "C-I" then the code becomes

def foo
  print "boo"
end

I can use this in quiet mode i.e. ignoring. Emacs as well. Does anyone know how to do this on Vim?

1
  • FYI: C-i and TAB are the same in Emacs, so it's even easier to properly indent a line.
    – remcycles
    Aug 21, 2023 at 15:30

4 Answers 4

4

In insert mode Ctrl+F reindents the current line. The keybinding may be changed by setting cinkeys

1
  • 1
    [in INSERT mdoe] Wow, I didn't know that! Thank you very much!
    – Culip
    Nov 12, 2010 at 16:43
3

maybe there are other ways but try:

=
3
  • Thanks for your quick responding, but "=" just puts the same length of indentation (or none?) in all the line. I'm looking for the way to put adequate length of indentations as in the example.
    – Culip
    Sep 23, 2010 at 22:54
  • = is the standard way for C-like languages, but it looks like you are using a more python-like language, which usually requires some extra setup, such as here: henry.precheur.org/vim/python Sep 24, 2010 at 2:11
  • I believe equals should do the trick but you have to have the proper indentation method setup. Check out :help index.txt It gives a quick overview of the various indent settings. You still may need to also check out :help ft-python-indent. I don't think you should need any extras plugins to get decent python indenting, however I could be wrong :)
    – Neg_EV
    Sep 24, 2010 at 14:45
3

The answers to this question: https://stackoverflow.com/questions/235839/how-do-i-indent-multiple-lines-quickly-in-vi offers several ways to indent.

You may be looking for ={ to indent your current code block.

1
  • Thanks. The last section about "filetype plugin indent on" was really helpful. ;-)
    – Culip
    Sep 25, 2010 at 8:06
2

Thanks for all of your help. I really appreciate it. But actually I found a solution:

In vimrc, you will see the code below in near L37-39:

"if has("autocmd")
"  filetype plugin indent on
"endif

and so delete " at the beginning of each line to enable this setting. Now the auto indent and indentation fixing work!

As Casual Coder says, when you want to fix indentation in the current line, press = (in Emacs C-h.) Also you can combine this with selecting commands like gg=G (in Emacs, C-x h C-i) for the whole code, and C-v (Visual Block Mode) would be handy.

(The sample code I wrote in the question is written in Ruby. My Vim which I downloaded from the Ubuntu repository comes with Ruby's indent configuration file (/usr/share/vim/vim72/indent/ruby.vim) and so you don't have to get "vim-ruby".)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .