5

I'm working with python code. After some modifications, I want to update the identation but obviously, select everything and press '=' doesn't work, python being python.

So, is there any other way to add (and/or remove) some character (here, tabs) at the beggining of each line ?

  • 4
    Try this: :help v_>. – Sato Katsura Oct 15 '15 at 10:35
6

As Sato mentioned in comments, :help v_> will show you help for the best tool you can use for this. >> in normal mode will indent the current line; >3> will indent the current line and the following two lines; << will decrease indentation.

Another feature that works will in combination with > can be found at :help text-objects. (aB, a[, and so on.) For example in C-style code that uses curly braces, >aB or >iB to indent the current block including curly braces, or only indent the lines between the curly braces, respectively. For Python code, >ap (indent a paragraph) may be more useful, but using visual mode to select the lines as described in :help v_> is even more adjustable.

For the general answer to "How to add a character/some text at the beginning of each line?" i.e., when you want to insert something other than tabs or spaces, there are a couple ways:

:%normal Itext to insert will prepend text to insert to every line in the file. % can be replaced with any range you like. (See :help range and also :help :normal)

Or you can use Ctrlvto enter blockwise-visual mode, use j and k to select a column of characters, then I (capital) to insert text at that point in all selected lines. (It will only be visible in all the lines after you Esc and then make another motion of any sort.) This has the advantage that you can enter text at ANY point in the line, not only at the beginning. (See :help v_b_I)

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.