Linux Ask!

Linux Ask! is a Q & A web site specific for Linux related questions. Questions are collected, answered and audited by experienced Linux users.

Linux Ask!

Sep 192010
 

Code formatting and indentation in VIM

Answer:

Vim support code formatting and auto indentation - if you tell Vim to know the filetype (either by file extension, or by setting the filetype)

e.g. A simple C program (test.c)


#include 

main()
{
for(int i=0;i<10;i++)
{ 
printf ("Hello World!\n");
}
}

Then type following in Vim command mode (by pressing Esc):

gg=G

Now the code will be formatted to:

#include 

main()
{
    for(int i=0;i<10;i++)
    {
        printf ("Hello World!\n");
    }
}
Sep 172010
 

Multiple separators in awk

Answer:

Sometimes when you split a string using awk, you need to support more than 1 separators, you can use the -F option.

E.g.

# cat foo.txt | awk -F "[ ?]" '{print $7}'

Now the awk split by a space and by a ?.

Sep 162010
 

Add a user to group in Linux

Answer:

To add a user to a group (supplementary), you can use the command below:

e.g.

# useradd -G admin john

If you want to change the user's primary group, you can use

e.g.

# useradd -g admin john