Results 1 to 7 of 7
Hi All,
I would like to know if there is a way to get the editors Vim or Emacs (I use both as I haven't decided which I prefer yet) ...
- 04-16-2010 #1Just Joined!
- Join Date
- Apr 2010
- Posts
- 3
C indentation with Emacs or Vim
Hi All,
I would like to know if there is a way to get the editors Vim or Emacs (I use both as I haven't decided which I prefer yet) to change the indentation automatically when you wrap existing code into a new statement. Or, at the very least is there a way to indent three lines with one command rather than doing it one by one ? This can potentially make me finally choose the editor which suits me best.
For example:
Code:/* original code */ inst 1; inst 2; inst 3; /* wrapping the code into a while loop */ while (cond) { inst 1; inst 2; inst 3; } /* desired result */ while (cond) { inst1; inst2; inst3; }
Is there anyway to get the editor to change the indentation of the three instructions? Although I use the autoindent, it doesn't acknowledge subsequent changes. Any clue ?
Thanks.
- 04-16-2010 #2
I can really only speak for vi (as that is all I use). If you have a .vimrc file, add the below code. If not, go into vi and type :mkv and then you will have the file.
Anyway, put this into the .vimrc file:
set autoindent
set smartindent
set shiftwidth=4
- 04-16-2010 #3Just Joined!
- Join Date
- Apr 2010
- Posts
- 3
thanks for the reply, but I didn't make myself understood.
I'm talking about modifying existing code. Is there anyway to get the editor to automatically modify the indentation accordingly ?
Example:
Is there any easier way of doing this ?Code:#include <stdio.h> int main (void) { printf ("something"); printf ("something else"); return 0; } /* now I decide that I want to change my code so that */ /* printf function calls are inside a loop */ int main (void) { for (i=0, i < 10, i++) { /* if I add the header of the loop */ printf ("something"); /* the body of the loop isn't automatically */ printf ("something else"); /* indented and I have to indent printf manually */ } return 0; }
- 04-16-2010 #4
Ah, sorry, I misunderstood. Hmm, sorry, I am not too sure how to do that.
- 04-17-2010 #5Just Joined!
- Join Date
- Mar 2008
- Location
- Chennai, India
- Posts
- 26
In emacs you can indent your code by
M-x indent-region command
For this to work you must be in a certain programing mode.
You can set a C-programming mode by M-x c-set-style <your mode>
If you don't get this done then you can use the command line indent command to get your code indented(if indent is available with your distro!).
Hope this was helpful.
cheers
Sarma
- 04-17-2010 #6Just Joined!
- Join Date
- Apr 2010
- Posts
- 3
Thanks, I got that to work with emacs !
- 04-17-2010 #7
There is a similar element in vi as well.
The "==" command will correctly indent the current line. I usually use it by using visual mode to highlight the lines I want, and then use "=" to indent them all.
Not that this is dependent on vi knowing what the correct indentation is. You will need to have filetype indentation (or something like cindent) enabled in order for this to work.DISTRO=Arch
Registered Linux User #388732


Reply With Quote