开发者

Proper Setup of Tab Indents

Using Emacs with C source code, I want press开发者_运维知识库ing the tab key to insert a tab character, and have it show a tab as being 2 spaces wide. So this is what I put in my ~/.emacs:

(setq c-default-style "linux")

(setq-default c-basic-offset 2

tab-width 2)

However it does not count a tab as one column, but 2 in this case, and it causes a problem when I want to, for instance, jump to the spot that gcc specifies in an error message.

int main()  
{  
    if(1)  
    {  
        if(2)  
        {  
            error  
        }  
    }  
}

With this error:

test.c:7:5 error: `error` undeclared (first use in this function)

So when I select the error it jumps to the wrong spot. Is there any way to fix this?


Never have the ASCII HORIZONTAL TAB (HT) character (byte value 0x9) represent anything but "move to next 8-column boundary". Messing with this default messes up all sorts of unix programs, and generally causes problems.

If you want 2-space indentation that's fine, and it's also fine that you want it to happen when you hit the TAB key. Emacs separates the notion of the HT character from that of indentation, and each can be controlled separately.

Start with just

(setq c-basic-offset 2)

in your .emacs file. Now, whenever you hit the TAB key in a C program, emacs will automatically re-ident that line appropriately. You can use M-i or C-q TAB to insert a real HT character.

There's lots more customization you can do if this still isn't quite what you want, but this is the right way to begin.


Have you tried setting c-syntactic-indentation to nil?


Have you tried invoking gcc with -ftabstop=2?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜