Emacs: replace tabs with spaces

When you want to replace tab with spaces or vice versa don’t use M-% (query-replace) but M-x tabify or M-x untabify. They work on the current selection so if you want it to be applied to a whole buffer, try C-x h (mark-whole-buffer) before to select the whole buffer.

This entry was posted in emacs. Bookmark the permalink.

3 Responses to Emacs: replace tabs with spaces

  1. Tim Heard says:

    First, there is a typo; I believe you meant M-x tabify, not M-x tabidy. Second, this command did not work for me. A much simpler solution ended up being to type “M-% C-q ” (i.e. doing a find replace, pressing C-q for a quoted search, pressing the tab key, pressing enter [to choose that as the search string] and then typing in four spaces as the replacement string; then just hit ‘!’ to replace all occurrences).

  2. Bryan Ash says:

    Thanks, this work like a champ for me.

    Looking up `untabify` with `C-h f untabify`, I found that I can set the `tab-width` variable to 2 spaces … perfect!

Leave a Reply

Your email address will not be published. Required fields are marked *