[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4. Commands

This chapter specifies all of CC Mode’s commands, and thus contains nearly everything you need to know to use CC Mode (as contrasted with configuring it). Commands here means both control key sequences and electric keys, these being characters such as ‘;’ which, as well as inserting themselves into the buffer, also do other things.

You might well want to review (emacs)Moving by Parens section ‘Moving by Parens’ in GNU Emacs Manual, which describes commands for moving around brace and parenthesis structures.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.1 Indentation Commands

The following commands reindent C constructs. Note that when you change your coding style, either interactively or through some other means, your file does not automatically get reindented. You will need to execute one of the following commands to see the effects of your changes.

Also, variables like c-hanging-* and c-cleanup-list (see section Customizing Auto-newlines) only affect how on-the-fly code is formatted. Changing the “hanginess” of a brace and then reindenting, will not move the brace to a different line. For this, you’re better off getting an external program like GNU indent, which will rearrange brace location, amongst other things.

Preprocessor directives are handled as syntactic whitespace from other code, i.e. they can be interspersed anywhere without affecting the indentation of the surrounding code, just like comments.

The code inside macro definitions is, by default, still analyzed syntactically so that you get relative indentation there just as you’d get if the same code was outside a macro. However, since there is no hint about the syntactic context, i.e. whether the macro expands to an expression, to some statements, or perhaps to whole functions, the syntactic recognition can be wrong. CC Mode manages to figure it out correctly most of the time, though.

Some macros, when invoked, “have their own semicolon”. To get the next line indented correctly, rather than as a continuation line, See section Macros with semicolons.

Reindenting large sections of code can take a long time. When CC Mode reindents a region of code, it is essentially equivalent to hitting <TAB> on every line of the region.

These commands indent code:

<TAB> (c-indent-command)

This command indents the current line. That is all you need to know about it for normal use.

c-indent-command does different things, depending on the setting of c-syntactic-indentation (see section Indentation Engine Basics):

The precise behavior is modified by several variables: With c-tab-always-indent, you can make <TAB> insert whitespace in some circumstances—c-insert-tab-function then defines precisely what sort of “whitespace” this will be. Set the standard Emacs variable indent-tabs-mode to t if you want real ‘tab’ characters to be used in the indentation, to nil if you want only spaces. See (emacs)Just Spaces section ‘Just Spaces’ in GNU Emacs Manual.

User Option: c-tab-always-indent

This variable modifies how <TAB> operates.

  • When it is t (the default), <TAB> simply indents the current line.
  • When it is nil, <TAB> (re)indents the line only if point is to the left of the first non-whitespace character on the line. Otherwise it inserts some whitespace (a tab or an equivalent number of spaces - see below) at point.
  • With some other value, the line is reindented. Additionally, if point is within a string or comment, some whitespace is inserted.
User Option: c-insert-tab-function

When “some whitespace” is inserted as described above, what actually happens is that the function stored in c-insert-tab-function is called. Normally, this is insert-tab, which inserts a real tab character or the equivalent number of spaces (depending on indent-tabs-mode). Some people, however, set c-insert-tab-function to tab-to-tab-stop so as to get hard tab stops when indenting.

The kind of indentation the next five commands do depends on the setting of c-syntactic-indentation (see section Indentation Engine Basics):

C-j (newline-and-indent)

Inserts a newline and indents the new blank line, ready to start typing. This is a standard (X)Emacs command.

C-M-q (c-indent-exp)

Indents an entire balanced brace or parenthesis expression. Note that point must be on the opening brace or parenthesis of the expression you want to indent.

C-c C-q (c-indent-defun)

Indents the entire top-level function, class or macro definition encompassing point. It leaves point unchanged. This function can’t be used to reindent a nested brace construct, such as a nested class or function, or a Java method. The top-level construct being reindented must be complete, i.e. it must have both a beginning brace and an ending brace.

C-M-\ (indent-region)

Indents an arbitrary region of code. This is a standard Emacs command, tailored for C code in a CC Mode buffer. Note, of course, that point and mark must delineate the region you want to indent.

C-M-h (c-mark-function)

While not strictly an indentation command, this is useful for marking the current top-level function or class definition as the current region. As with c-indent-defun, this command operates on top-level constructs, and can’t be used to mark say, a Java method.

These variables are also useful when indenting code:

User Option: indent-tabs-mode

This is a standard Emacs variable that controls how line indentation is composed. When it’s non-nil, tabs can be used in a line’s indentation, otherwise only spaces are used.

User Option: c-progress-interval

When indenting large regions of code, this variable controls how often a progress message is displayed. Set this variable to nil to inhibit the progress messages, or set it to an integer which is how often (in seconds) progress messages are to be displayed.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.2 Comment Commands

When the commands in this section add comment delimiters, they use either line comments or block comments depending on the setting of the comment style (see section Minor Modes).

C-c C-c (comment-region)

This command comments out the lines that start in the region. With a negative argument, it does the opposite - it deletes the comment delimiters from these lines. See (emacs)Multi-Line Comments section ‘Multi-Line Comments’ in GNU Emacs Manual, for fuller details. comment-region isn’t actually part of CC Mode - it is given a CC Mode binding for convenience.

M-; (comment-dwim or indent-for-comment (4))

Insert a comment at the end of the current line, if none is there already. Then reindent the comment according to comment-column (see (emacs)Options for Comments section ‘Options for Comments’ in GNU Emacs Manual) and the variables below. Finally, position the point after the comment starter. C-u M-; kills any comment on the current line, together with any whitespace before it. This is a standard Emacs command, but CC Mode enhances it a bit with two variables:

User Option: c-indent-comment-alist

This style variable allows you to vary the column that M-; puts the comment at, depending on what sort of code is on the line, and possibly the indentation of any similar comment on the preceding line. It is an association list that maps different types of lines to actions describing how they should be handled. If a certain line type isn’t present on the list then the line is indented to the column specified by comment-column.

See the documentation string for a full description of this variable (use C-h v c-indent-comment-alist).

User Option: c-indent-comments-syntactically-p

Normally, when this style variable is nil, M-; will indent comment-only lines according to c-indent-comment-alist, just as it does with lines where other code precede the comments. However, if you want it to act just like <TAB> for comment-only lines you can get that by setting c-indent-comments-syntactically-p to non-nil.

If c-indent-comments-syntactically-p is non-nil then c-indent-comment-alist won’t be consulted at all for comment-only lines.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.3 Movement Commands

CC Mode contains some useful commands for moving around in C code.

C-M-a (c-beginning-of-defun)
C-M-e (c-end-of-defun)

Move to the beginning or end of the current or next function. Other constructs (such as a structs or classes) which have a brace block also count as “functions” here. To move over several functions, you can give these commands a repeat count.

The start of a function is at its header. The end of the function is after its closing brace, or after the semicolon of a construct (such as a struct) which doesn’t end at the brace. These two commands try to leave point at the beginning of a line near the actual start or end of the function. This occasionally causes point not to move at all.

By default, these commands will recognize functions contained within a declaration scope such as a C++ class or namespace construct, should the point start inside it. If CC Mode fails to find function beginnings or ends inside the current declaration scope, it will search the enclosing scopes. If you want CC Mode to recognize functions only at the top level(5), set c-defun-tactic to t.

These functions are analogous to the Emacs built-in commands beginning-of-defun and end-of-defun, except they eliminate the constraint that the top-level opening brace of the defun must be in column zero. See (emacs)Defuns section ‘Defuns’ in GNU Emacs Manual, for more information.

C-M-a (AWK Mode) (c-awk-beginning-of-defun)
C-M-e (AWK Mode) (c-awk-end-of-defun)

Move to the beginning or end of the current or next AWK defun. These commands can take prefix-arguments, their functionality being entirely equivalent to beginning-of-defun and end-of-defun.

AWK Mode defuns are either pattern/action pairs (either of which might be implicit) or user defined functions. Having the ‘{’ and ‘}’ (if there are any) in column zero, as is suggested for some modes, is neither necessary nor helpful in AWK mode.

M-a (c-beginning-of-statement)
M-e (c-end-of-statement)

Move to the beginning or end of the innermost C statement. If point is already there, move to the next beginning or end of a statement, even if that means moving into a block. (Use C-M-b or C-M-f to move over a balanced block.) A prefix argument n means move over n statements.

If point is within or next to a comment or a string which spans more than one line, these commands move by sentences instead of statements.

When called from a program, these functions take three optional arguments: the repetition count, a buffer position limit which is the farthest back to search for the syntactic context, and a flag saying whether to do sentence motion in or near comments and multiline strings.

C-c C-u (c-up-conditional)

Move back to the containing preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move forward to the end of the containing preprocessor conditional.

‘#elif’ is treated like ‘#else’ followed by ‘#if’, so the function stops at them when going backward, but not when going forward.

This key sequence is not bound in AWK Mode, which doesn’t have preprocessor statements.

M-x c-up-conditional-with-else

A variety of c-up-conditional that also stops at ‘#else’ lines. Normally those lines are ignored.

M-x c-down-conditional

Move forward into the next nested preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move backward into the previous nested preprocessor conditional.

‘#elif’ is treated like ‘#else’ followed by ‘#if’, so the function stops at them when going forward, but not when going backward.

M-x c-down-conditional-with-else

A variety of c-down-conditional that also stops at ‘#else’ lines. Normally those lines are ignored.

C-c C-p (c-backward-conditional)
C-c C-n (c-forward-conditional)

Move backward or forward across a preprocessor conditional, leaving the mark behind. A prefix argument acts as a repeat count. With a negative argument, move in the opposite direction.

These key sequences are not bound in AWK Mode, which doesn’t have preprocessor statements.

M-x c-backward-into-nomenclature
M-x c-forward-into-nomenclature

A popular programming style, especially for object-oriented languages such as C++ is to write symbols in a mixed case format, where the first letter of each word is capitalized, and not separated by underscores. E.g. ‘SymbolsWithMixedCaseAndNoUnderlines’.

These commands move backward or forward to the beginning of the next capitalized word. With prefix argument n, move n times. If n is negative, move in the opposite direction.

Note that these two commands have been superseded by c-subword-mode, which you should use instead. See section Subword Movement and Editing. They might be removed from a future release of CC Mode.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.4 Filling and Line Breaking Commands

Since there’s a lot of normal text in comments and string literals, CC Mode provides features to edit these like in text mode. The goal is to do it seamlessly, i.e. you can use auto fill mode, sentence and paragraph movement, paragraph filling, adaptive filling etc. wherever there’s a piece of normal text without having to think much about it. CC Mode keeps the indentation, fixes suitable comment line prefixes, and so on.

You can configure the exact way comments get filled and broken, and where Emacs does auto-filling (see see section Customizing Filling and Line Breaking). Typically, the style system (see section Styles) will have set this up for you, so you probably won’t have to bother.

Line breaks are by default handled (almost) the same regardless of whether they are made by auto fill mode (see (emacs)Auto Fill section ‘Auto Fill’ in GNU Emacs Manual), by paragraph filling (e.g. with M-q), or explicitly with M-j or similar methods. In string literals, the new line gets the same indentation as the previous nonempty line.(6).

M-q (c-fill-paragraph)

This command fills multiline string literals and both block and line style comments. In Java buffers, the Javadoc markup words are recognized as paragraph starters. The line oriented Pike autodoc markup words are recognized in the same way in Pike mode.

The formatting of the starters (/*) and enders (*/) of block comments are kept as they were before the filling. I.e., if either the starter or ender were on a line of its own, then it stays on its own line; conversely, if the delimiter has comment text on its line, it keeps at least one word of that text with it on the line.

This command is the replacement for fill-paragraph in CC Mode buffers.

M-j (c-indent-new-comment-line)

This breaks the current line at point and indents the new line. If point was in a comment, the new line gets the proper comment line prefix. If point was inside a macro, a backslash is inserted before the line break. It is the replacement for indent-new-comment-line.

M-x c-context-line-break

Insert a line break suitable to the context: If the point is inside a comment, the new line gets the suitable indentation and comment line prefix like c-indent-new-comment-line. In normal code it’s indented like newline-and-indent would do. In macros it acts like newline-and-indent but additionally inserts and optionally aligns the line ending backslash so that the macro remains unbroken. See section Customizing Macros, for details about the backslash alignment. In a string, a backslash is inserted only if the string is within a macro(7).

This function is not bound to a key by default, but it’s intended to be used on the RET key. If you like the behavior of newline-and-indent on RET, you should consider switching to this function. See section Sample .emacs File.

M-x c-context-open-line

This is to C-o (M-x open-line) as c-context-line-break is to RET. I.e. it works just like c-context-line-break but leaves the point before the inserted line break.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.5 Minor Modes

CC Mode contains several minor-mode-like features that you might find useful while writing new code or editing old code:

comment style

This specifies whether comment commands (such as M-;) insert block comments or line comments(8).

electric mode

When this is enabled, certain visible characters cause reformatting as they are typed. This is normally helpful, but can be a nuisance when editing chaotically formatted code. It can also be disconcerting, especially for users who are new to CC Mode.

auto-newline mode

This automatically inserts newlines where you’d probably want to type them yourself, e.g. after typing ‘}’s. Its action is suppressed when electric mode is disabled.

hungry-delete mode

This lets you delete a contiguous block of whitespace with a single key - for example, the newline and indentation just inserted by auto-newline when you want to back up and write a comment after the last statement.

subword mode

This mode makes basic word movement commands like M-f (forward-word) and M-b (backward-word) treat the parts of sillycapsed symbols as different words. E.g. ‘NSGraphicsContext’ is treated as three words ‘NS’, ‘Graphics’, and ‘Context’.

syntactic-indentation mode

When this is enabled (which it normally is), indentation commands such as C-j indent lines of code according to their syntactic structure. Otherwise, a line is simply indented to the same level as the previous one and <TAB> adjusts the indentation in steps of ‘c-basic-offset’.

Full details on how these minor modes work are at Electric Keys and Keywords, Auto-newline Insertion, Hungry Deletion of Whitespace, Subword Movement and Editing, and Indentation Engine Basics.

You can toggle each of these minor modes on and off, and you can configure CC Mode so that it starts up with your favorite combination of them (see section Sample .emacs File). By default, when you initialize a buffer, the comment style is set to the default for the major mode, electric mode and syntactic-indentation mode are enabled, but the other two modes are disabled.

CC Mode displays the current state of the first five of these minor modes on the mode line by appending characters to the major mode’s name: ‘/’ or ‘*’ to indicate the comment style (respectively line or block), and one letter for each of the other minor modes which is enabled - ‘l’ for electric mode, ‘a’ for auto-newline mode, ‘h’ for hungry delete mode, and ‘w’ for subword mode. If the comment style was block and all the other modes were enabled, you’d see ‘C/*lahw’(9).

Here are the commands to toggle these modes:

C-c C-k (c-toggle-comment-style)

Toggle the comment style between line style and block style. In modes (such as AWK Mode) which only have one of these styles, this function does nothing.

C-c C-l (c-toggle-electric-state)

Toggle electric minor mode. When the command turns the mode off, it also suppresses auto-newline mode.

C-c C-a (c-toggle-auto-newline)

Toggle auto-newline minor mode. When the command turns the mode on, it also enables electric minor mode.

M-x c-toggle-hungry-state(10)

Toggle hungry-delete minor mode.

M-x c-toggle-auto-hungry-state(11)

Toggle both auto-newline and hungry delete minor modes.

C-c C-w (M-x c-subword-mode)

Toggle subword mode.

M-x c-toggle-syntactic-indentation

Toggle syntactic-indentation mode.

Common to all the toggle functions above is that if they are called programmatically, they take an optional numerical argument. For c-toggle-comment style, a positive value will select block comments, a negative value will select line comments. For the other functions, a positive value will turn on the minor mode (or both of them in the case of c-toggle-auto-hungry-state) and a negative value will turn it (or them) off.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.6 Electric Keys and Keywords

Most punctuation keys provide electric behavior - as well as inserting themselves they perform some other action, such as reindenting the line. This reindentation saves you from having to reindent a line manually after typing, say, a ‘}’. A few keywords, such as else, also trigger electric action.

You can inhibit the electric behavior described here by disabling electric minor mode (see section Minor Modes).

Common to all these keys is that they only behave electrically when used in normal code (as contrasted with getting typed in a string literal or comment). Those which cause re-indentation do so only when c-syntactic-indentation has a non-nil value (which it does by default).

These keys and keywords are:

#

Pound (bound to c-electric-pound) is electric when typed as the first non-whitespace character on a line and not within a macro definition. In this case, the variable c-electric-pound-behavior is consulted for the electric behavior. This variable takes a list value, although the only element currently defined is alignleft, which tells this command to force the ‘#’ character into column zero. This is useful for entering preprocessor macro definitions.

Pound is not electric in AWK buffers, where ‘#’ starts a comment, and is bound to self-insert-command like any typical printable character.

*
/

A star (bound to c-electric-star) or a slash (c-electric-slash) causes reindentation when you type it as the second component of a C style block comment opener (‘/*’) or a C++ line comment opener (‘//’) respectively, but only if the comment opener is the first thing on the line (i.e. there’s only whitespace before it).

Additionally, you can configure CC Mode so that typing a slash at the start of a line within a block comment will terminate the comment. You don’t need to have electric minor mode enabled to get this behavior. See section Clean-ups.

In AWK mode, ‘*’ and ‘/’ do not delimit comments and are not electric.

<
>

A less-than or greater-than sign (bound to c-electric-lt-gt) is electric in two circumstances: when it is an angle bracket in a C++ ‘template’ declaration (and similar constructs in other languages) and when it is the second of two < or > characters in a C++ style stream operator. In either case, the line is reindented. Angle brackets in C ‘#include’ directives are not electric.

(
)

The normal parenthesis characters ‘(’ and ‘)’ (bound to c-electric-paren) reindent the current line. This is useful for getting the closing parenthesis of an argument list aligned automatically.

You can also configure CC Mode to insert a space automatically between a function name and the ‘(’ you’ve just typed, and to remove it automatically after typing ‘)’, should the argument list be empty. You don’t need to have electric minor mode enabled to get these actions. See section Clean-ups.

{
}

Typing a brace (bound to c-electric-brace) reindents the current line. Also, one or more newlines might be inserted if auto-newline minor mode is enabled. See section Auto-newline Insertion. Additionally, you can configure CC Mode to compact excess whitespace inserted by auto-newline mode in certain circumstances. See section Clean-ups.

:

Typing a colon (bound to c-electric-colon) reindents the current line. Additionally, one or more newlines might be inserted if auto-newline minor mode is enabled. See section Auto-newline Insertion. If you type a second colon immediately after such an auto-newline, by default the whitespace between the two colons is removed, leaving a C++ scope operator. See section Clean-ups.

If you prefer, you can insert ‘::’ in a single operation, avoiding all these spurious reindentations, newlines, and clean-ups. See section Other Commands.

;
,

Typing a semicolon or comma (bound to c-electric-semi&comma) reindents the current line. Also, a newline might be inserted if auto-newline minor mode is enabled. See section Auto-newline Insertion. Additionally, you can configure CC Mode so that when auto-newline has inserted whitespace after a ‘}’, it will be removed again when you type a semicolon or comma just after it. See section Clean-ups.

Command: c-electric-continued-statement

Certain keywords are electric, causing reindentation when they are preceded only by whitespace on the line. The keywords are those that continue an earlier statement instead of starting a new one: else, while, catch (only in C++ and Java) and finally (only in Java).

An example:

 
for (i = 0; i < 17; i++)
  if (a[i])
    res += a[i]->offset;
else

Here, the else should be indented like the preceding if, since it continues that statement. CC Mode will automatically reindent it after the else has been typed in full, since only then is it possible to decide whether it’s a new statement or a continuation of the preceding if.

CC Mode uses Abbrev mode (see (emacs)Abbrevs section ‘Abbrevs’ in GNU Emacs Manual) to accomplish this. It’s therefore turned on by default in all language modes except IDL mode, since CORBA IDL doesn’t have any statements.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.7 Auto-newline Insertion

When you have Auto-newline minor mode enabled (see section Minor Modes), CC Mode inserts newlines for you automatically (in certain syntactic contexts) when you type a left or right brace, a colon, a semicolon, or a comma. Sometimes a newline appears before the character you type, sometimes after it, sometimes both.

Auto-newline only triggers when the following conditions hold:

You can configure the precise circumstances in which newlines get inserted (see see section Customizing Auto-newlines). Typically, the style system (see section Styles) will have set this up for you, so you probably won’t have to bother.

Sometimes CC Mode inserts an auto-newline where you don’t want one, such as after a ‘}’ when you’re about to type a ‘;’. Hungry deletion can help here (see section Hungry Deletion of Whitespace), or you can activate an appropriate clean-up, which will remove the excess whitespace after you’ve typed the ‘;’. See Clean-ups for a full description. See also Electric Keys and Keywords for a summary of clean-ups listed by key.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.8 Hungry Deletion of Whitespace

If you want to delete an entire block of whitespace at point, you can use hungry deletion. This deletes all the contiguous whitespace either before point or after point in a single operation. “Whitespace” here includes tabs and newlines, but not comments or preprocessor commands. Hungry deletion can markedly cut down on the number of times you have to hit deletion keys when, for example, you’ve made a mistake on the preceding line and have already pressed C-j.

Hungry deletion is a simple feature that some people find extremely useful. In fact, you might find yourself wanting it in all your editing modes!

Loosely speaking, in what follows, <DEL> means “the backspace key” and <DELETE> means “the forward delete key”. This is discussed in more detail below.

There are two different ways you can use hungry deletion:

Using Hungry Delete Mode with <DEL> and C-d

Here you toggle Hungry Delete minor mode with M-x c-toggle-hungry-state(13) (see section Minor Modes.) This makes <DEL> and C-d do backwards and forward hungry deletion.

<DEL> (c-electric-backspace)

This command is run by default when you hit the DEL key. When hungry delete mode is enabled, it deletes any amount of whitespace in the backwards direction. Otherwise, or when used with a prefix argument or in a literal (see section Auto-newline Insertion), the command just deletes backwards in the usual way. (More precisely, it calls the function contained in the variable c-backspace-function, passing it the prefix argument, if any.)

c-backspace-function

Hook that gets called by c-electric-backspace when it doesn’t do an “electric” deletion of the preceding whitespace. The default value is backward-delete-char-untabify (see (elisp)Deletion section ‘Deletion’ in GNU Emacs Lisp Reference Manual, the function which deletes a single character.

C-d (c-electric-delete-forward)

This function, which is bound to C-d by default, works just like c-electric-backspace but in the forward direction. When it doesn’t do an “electric” deletion of the following whitespace, it just does delete-char, more or less. (Strictly speaking, it calls the function in c-delete-function with the prefix argument.)

c-delete-function

Hook that gets called by c-electric-delete-forward when it doesn’t do an “electric” deletion of the following whitespace. The default value is delete-char.

Using Distinct Bindings

The other (newer and recommended) way to use hungry deletion is to perform c-hungry-delete-backwards and c-hungry-delete-forward directly through their key sequences rather than using the minor mode toggling.

C-c C-<DEL>, or C-c <DEL> (c-hungry-delete-backwards)(14)

Delete any amount of whitespace in the backwards direction (regardless whether hungry-delete mode is enabled or not). This command is bound to both C-c C-<DEL> and C-c <DEL>, since the more natural one, C-c C-<DEL>, is sometimes difficult to type at a character terminal.

C-c C-d, C-c C-<DELETE>, or C-c <DELETE> (c-hungry-delete-forward)

Delete any amount of whitespace in the forward direction (regardless whether hungry-delete mode is enabled or not). This command is bound to both C-c C-<DELETE> and C-c <DELETE> for the same reason as for <DEL> above.

When we talk about <DEL>, and <DELETE> above, we actually do so without connecting them to the physical keys commonly known as <Backspace> and <Delete>. The default bindings to those two keys depends on the flavor of (X)Emacs you are using.

In XEmacs 20.3 and beyond, the <Backspace> key is bound to c-electric-backspace and the <Delete> key is bound to c-electric-delete. You control the direction it deletes in by setting the variable delete-key-deletes-forward, a standard XEmacs variable. When this variable is non-nil, c-electric-delete will do forward deletion with c-electric-delete-forward, otherwise it does backward deletion with c-electric-backspace. Similarly, C-c <Delete> and C-c C-<Delete> are bound to c-hungry-delete which is controlled in the same way by delete-key-deletes-forward.

Emacs 21 and later automatically binds <Backspace> and <Delete> to DEL and C-d according to your environment, and CC Mode extends those bindings to C-c C-<Backspace> etc. If you need to change the bindings through normal-erase-is-backspace-mode then CC Mode will also adapt its extended bindings accordingly.

In earlier (X)Emacs versions, CC Mode doesn’t bind either <Backspace> or <Delete> directly. Only the key codes DEL and C-d are bound, and it’s up to the default bindings to map the physical keys to them. You might need to modify this yourself if the defaults are unsuitable.

Getting your <Backspace> and <Delete> keys properly set up can sometimes be tricky. The information in (emacs)DEL Does Not Delete section ‘DEL Does Not Delete’ in GNU Emacs Manual, might be helpful if you’re having trouble with this in GNU Emacs.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.9 Subword Movement and Editing

In spite of the GNU Coding Standards, it is popular to name a symbol by mixing uppercase and lowercase letters, e.g. ‘GtkWidget’, ‘EmacsFrameClass’, or ‘NSGraphicsContext’. Here we call these mixed case symbols nomenclatures. Also, each capitalized (or completely uppercase) part of a nomenclature is called a subword. Here are some examples:

NomenclatureSubwords
‘GtkWindow’‘Gtk’ and ‘Window’
‘EmacsFrameClass’‘Emacs’, ‘Frame’, and ‘Class’
‘NSGraphicsContext’‘NS’, ‘Graphics’, and ‘Context’

The subword minor mode replaces the basic word oriented movement and editing commands with variants that recognize subwords in a nomenclature and treat them as separate words:

KeyWord oriented commandSubword oriented command
M-fforward-wordc-forward-subword
M-bbackward-wordc-backward-subword
M-@mark-wordc-mark-subword
M-dkill-wordc-kill-subword
M-DELbackward-kill-wordc-backward-kill-subword
M-ttranspose-wordsc-transpose-subwords
M-ccapitalize-wordc-capitalize-subword
M-uupcase-wordc-upcase-subword
M-ldowncase-wordc-downcase-subword

Note that if you have changed the key bindings for the word oriented commands in your ‘.emacs’ or a similar place, the keys you have configured are also used for the corresponding subword oriented commands.

Type C-c C-w to toggle subword mode on and off. To make the mode turn on automatically, put the following code in your ‘.emacs’:

 
(add-hook 'c-mode-common-hook
          (lambda () (c-subword-mode 1)))

As a bonus, you can also use c-subword-mode in non-CC Mode buffers by typing M-x c-subword-mode.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

4.10 Other Commands

Here are the various other commands that didn’t fit anywhere else:

C-c . (c-set-style)

Switch to the specified style in the current buffer. Use like this:

 
C-c . style-name <RET>

You can use the <TAB> in the normal way to do completion on the style name. Note that all style names are case insensitive, even the ones you define yourself.

Setting a style in this way does not automatically reindent your file. For commands that you can use to view the effect of your changes, see Indentation Commands and Filling and Line Breaking Commands.

For details of the CC Mode style system, see Styles.

C-c : (c-scope-operator)

In C++, it is also sometimes desirable to insert the double-colon scope operator without performing the electric behavior of colon insertion. C-c : does just this.

C-c C-z (c-display-defun-name)

Display the current function name, if any, in the minibuffer. Additionally, if a prefix argument is given, push the function name to the kill ring. If there is no current function, c-display-defun-name does nothing. In Emacs, you can use this command in the middle of an interactive search if you set the customizable option isearch-allow-scoll to non-nil. See (emacs)Not Exiting Isearch section ‘Not Exiting Isearch’ in GNU Emacs Manual.

C-c C-\ (c-backslash-region)

This function inserts and aligns or deletes end-of-line backslashes in the current region. These are typically used in multi-line macros.

With no prefix argument, it inserts any missing backslashes and aligns them according to the c-backslash-column and c-backslash-max-column variables. With a prefix argument, it deletes any backslashes.

The function does not modify blank lines at the start of the region. If the region ends at the start of a line, it always deletes the backslash (if any) at the end of the previous line.

To customize the precise workings of this command, Customizing Macros.

The recommended line breaking function, c-context-line-break (see section Filling and Line Breaking Commands), is especially nice if you edit multiline macros frequently. When used inside a macro, it automatically inserts and adjusts the mandatory backslash at the end of the line to keep the macro together, and it leaves the point at the right indentation column for the code. Thus you can write code inside macros almost exactly as you can elsewhere, without having to bother with the trailing backslashes.

C-c C-e (c-macro-expand)

This command expands C, C++, Objective C or Pike macros in the region, using an appropriate external preprocessor program. Normally it displays its output in a temporary buffer, but if you give it a prefix arg (with C-u C-c C-e) it will overwrite the original region with the expansion.

The command does not work in any of the other modes, and the key sequence is not bound in these other modes.

c-macro-expand isn’t actually part of CC Mode, even though it is bound to a CC Mode key sequence. If you need help setting it up or have other problems with it, you can either read its source code or ask for help in the standard (X)Emacs forums.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated on September 26, 2026 using texi2html 1.82.