LinuxCommandLibrary

cb

Convert between character encodings

TLDR

Show all clipboards

$ cb
copy

Copy a file to the clipboard
$ cb copy [path/to/file]
copy

Copy some text to the clipboard
$ cb copy "[Some example text]"
copy

Copy piped data to the clipboard
$ echo "[Some example text]" | cb
copy

Paste clipboard content
$ cb paste
copy

Pipe out clipboard content
$ cb | cat
copy

Show clipboard history
$ cb history
copy

Show clipboard information
$ cb info
copy

SYNOPSIS

cb [-s] [-l len] [-j] [-f type] [-ps] [-bss] [-bad] [-nfn]

PARAMETERS

-s
    Uses the standard C style, typically resembling Bell Laboratories' conventions.

-l len
    Sets the maximum line length to len characters. Lines exceeding this length will be broken. The default is 79 characters.

-j
    Joins multiple 'case' labels onto a single line.

-f type
    Specifies a formatting style. Available types include: all, k&r (Kernighan & Ritchie), indent, and sysv.

-ps
    Inserts a space after every right parenthesis.

-bss
    Breaks braces onto their own lines (e.g., if (a) { b; } becomes if (a)
{ b; }
).

-bad
    Encloses declarations in braces.

-nfn
    Prevents newlines from being inserted before function names.

DESCRIPTION

The cb (C Beautifier) command is a classic Unix utility designed to reformat C source code. It reads C program text from standard input and outputs the beautified version to standard output, making it suitable for piping or redirection. Its primary function is to enhance code readability by consistently adjusting indentation, spacing, and the placement of braces, particularly for constructs like if-else, for, while, and switch statements, as well as function definitions. While cb aims to enforce a consistent coding style, it operates on a lexical level rather than performing a full syntactic or semantic analysis of the C code. This means it can sometimes be confused by complex preprocessor macros or non-standard C constructs. Despite its age and the availability of more sophisticated code formatters today, cb historically served as a valuable tool for developers to quickly clean up poorly formatted code or to apply a basic, uniform style across a codebase.

CAVEATS

The cb command is a simple, lexical formatter and does not perform full syntactic or semantic analysis of C code. This limitation means it can sometimes produce incorrect or undesirable formatting when encountering complex preprocessor macros, especially those that drastically alter the code structure. It is designed exclusively for C code and offers no support for C++ or other programming languages. Furthermore, cb writes its output to standard output, requiring users to explicitly redirect the output to a file if they wish to modify the original source. On many modern Linux distributions, cb is not installed by default and is largely considered obsolete, having been superseded by more robust and configurable code formatters like indent, astyle, or clang-format, which offer greater control and better understanding of C syntax.

BASIC USAGE

To use cb, you typically pipe the content of a C source file to its standard input, and then redirect its standard output to a new file or back to the original file (with caution). For example:
cat my_program.c | cb > my_program_formatted.c
or
cb < my_program.c > my_program_formatted.c.

STYLING LIMITATIONS

While cb can adjust indentation and brace placement, it does not modify comments or string literals within the code. Its stylistic options are relatively limited compared to modern formatters, primarily focusing on variations of common C coding styles like Kernighan & Ritchie (K&R) or System V.

HISTORY

The cb command originated in early Unix systems, particularly in environments like Bell Laboratories, and later became a component of various Unix distributions, including System V and BSD variants. Its development reflects the need for automated code formatting during an era when consistent coding styles were manually enforced or managed with simpler tools. It was designed to help programmers maintain readable code without the aid of modern IDEs or advanced language parsers. While once a useful utility, its development largely ceased as more sophisticated formatters, capable of deeper syntax analysis and greater customization, emerged. Today, it is mostly found in historical Unix archives or as part of compatibility packages like bsd-utils on some Linux distributions, serving as a testament to early efforts in code style automation.

SEE ALSO

Copied to clipboard