I've some codes copied from the Internet that have 2-space indenting and I want to change it into 4-space indenting. I wonder if there is a short vim routine to accomplish the task without having to write vim script? Here is how I'm currently doing it with an HTML file:
In short qa0vt<yp<esc>jq
The macro fails for a blank line or a line that doesn't start with "<". And I have no idea how to extend this solution to non-HTML file.
The shiftwidth parameter controls your indentation size; if you want four space indents, use :set shiftwidth=4 , or the abbreviation :set sw=4 . If only this is done, then indentation will be created using a mixture of spaces and tabs, because noexpandtab is the default. Use :set expandtab .
Start in the top of a file (to get there, press gg anywhere in the file.). Then press =G , and Vim will fix the indentation in the whole file. If you don't start in the beginning of the file, it will fix indentation from current line to the bottom of file.
Answer. In most code editors, tabs are not the same as 2 spaces or 4 spaces by default. A tab is stored differently than spaces in the code. Tabs can be seen as a big “jump” in the text, while spaces are always 1 space each.
A general way of changing the indent is by changing the tabstop:
Paste your file into an empty buffer, then:
:set ts=2 sts=2 noet :retab!
This changes every 2 spaces to a TAB character, then:
:set ts=4 sts=4 et :retab
This changes every TAB to 4 spaces.
The advantage of this method is that you can also use it the other way around, to convert from 4 to 2 spaces for example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With