# Advanced Editing Tools

## Comment/Uncomment

The Comment and Uncomment commands, available from the Tools menu allow you to add or remove comment markers to the selection, or the current line if no text is selected, if comments are supported by the format of the text you are editing.

The rules for how commenting is done are defined in the syntax definitions, so if syntax highlighting is not used, commenting/uncommenting is not possible.

Some formats define single line comment markers, some multiline markers and some both. If multiline markers are not available, commenting out a selection that does not fully include its last line is not possible.

If a single line marker is available, commenting single lines is preferred where applicable, as this helps to avoid problems with nested comments.

When removing comment markers, no uncommented text should be selected. When removing multiline comment markers from a selection, any whitespace outside the comment markers is ignored.

To place comment markers, use the Tools Comment menu item or the related keyboard shortcut sequence, the default is &Ctrl;D.

To remove comment markers, use the Tools Uncomment menu item or the related keyboard shortcut, the default is &Ctrl;&Shift;D.

## The Editor Component Command Line

&kappname;'s editor component has an internal command line, allowing you to perform various actions from a minimal &GUI;. The command line is a text entry at the bottom of the editor area; to show it select View Switch to Command Line or use the shortcut (default is F7). The editor provides a set of commands as documented below, and additional commands can be provided by plugins.

To execute a command, type the command then press the return key. The command line will indicate whether it succeeded and possibly display a message. If you entered the command line by pressing F7 it will automatically hide after a few seconds. To clear the message and enter a new command, press F7 again.

The command line has a built-in help system; issue the command `help` to get started. To see a list of all available commands issue `help list`; to view help for a specific command, do `help `command``.

The command line has a built in history, so you can reuse commands already typed. To navigate the history, use the &Up; and &Down; keys. When showing historical commands, the argument part of the command will be selected, allowing you to easily overwrite the arguments.

### Standard Command Line Commands

### Argument types

BOOLEAN

- This is used with commands that turns things on or off. Legal values are `on`, `off`, `true`, `false`, `1` or `0`.

INTEGER

- An integer number.

STRING

- A string, surrounded by single quotes (') or double quotes (") when it contains spaces.

#### Commands for Configuring the Editor

These commands are provided by the editor component, and allow you to configure the active document and view only. This is handy if you want to use a setting different from the default settings, for example for indentation.

    set-tab-widthINTEGER width

- Sets the tab width to the number `width`.

    set-indent-widthINTEGER width

- Sets the indentation width to the number `width`. Used only if you are indenting with spaces.

    set-word-wrap-columnINTEGER width

- Sets the line width for hard wrapping to `width`. This is used if you are having your text wrapped automatically.

    set-icon-borderBOOLEAN enable

- Sets the visibility of the icon border.

    set-folding-markersBOOLEAN enable

- Sets the visibility of the folding markers pane.

    set-line-numbersBOOLEAN enable

- Sets the visibility of the line numbers pane.

    set-replace-tabsBOOLEAN enable

- If enabled, tabs are replaced with spaces as you type.

    set-remove-trailing-spaceBOOLEAN enable

- If enabled, trailing whitespace is removed whenever the cursor leaves a line.

    set-show-tabsBOOLEAN enable

- If enabled, TAB characters and trailing whitespace will be visualized by a small dot.

    set-show-indentBOOLEAN enable

- If enabled, indentation will be visualized by a vertical dotted line.

    set-indent-spacesBOOLEAN enable

- If enabled, the editor will indent with `indent-width` spaces for each indentation level, rather than with one TAB character.

    set-mixed-indentBOOLEAN enable

- If enabled, &kappname; will use a mix of TAB and spaces for indentation. Each indentation level will be `indent-width` wide, and more indentation levels will be optimized to use as many TAB characters as possible.

  When executed, this command will additionally set space indentation enabled, and if the indent width is unspecified it will be set to half of the `tab-width` for the document at the time of execution.

    set-word-wrapBOOLEAN
    enable

- Enables dynamic word wrap according to `enable`.

    set-replace-tabs-saveBOOLEAN enable

- When enabled, tabs will be replaced with whitespace whenever the document is saved.

    set-remove-trailing-space-saveBOOLEAN enable

- When enabled, trailing space will be removed from each line whenever the document is saved.

    set-indent-modeSTRING name

- Sets the autoindentation mode to `name`. If `name` is not known, the mode is set to 'none'. Valid modes are 'none', 'normal', 'cstyle', 'haskell', 'lilypond', 'lisp', 'python', 'ruby' and 'xml'.

    set-auto-identBOOLEAN script

- Enable or disable autoindentation.

    set-highlightSTRING highlight

- Sets the syntax highlighting system for the document. The argument must be a valid highlight name, as seen in the Tools Highlighting menu. This command provides an autocompletion list for its argument.

    reload-scripts

- Reload all &javascript; scripts used by &kate;, including indenters and command line scripts.

    set-modeSTRING mode

- Choose the filetype scheme for the current document.

    nn[oremap]STRING original
    STRING mapped

- Map the key sequence `original` to `mapped`.

#### Commands for editing

These commands modify the current document.

    indent

- Indents the selected lines or the current line.

    unindent

- Unindents the selected lines or current line.

    cleanindent

- Cleans up the indentation of the selected lines or current line according to the indentation settings in the document.

    comment

- Inserts comment markers to make the selection or selected lines or current line a comment according to the text format as defined by the syntax highlight definition for the document.

    uncomment

- Removes comment markers from the selection or selected lines or current line according to the text format as defined by the syntax highlight definition for the document.

    kill-line

- Deletes the current line.

    replace STRING pattern
    STRING replacement

- Replaces text matching `pattern` with `replacement`. If you want to include whitespace in the `pattern`, you must quote both the `pattern` and `replacement` with single or double quotes. If the arguments are unquoted, the first word is used as `pattern` and the rest for `replacement`. If `replacement` is empty, each occurrence of `pattern` is removed.

  You can set flags to configure the search by adding a colon, followed by one or more letters each representing a configuration, giving the form `replace:options pattern replacement`. Available options are:

  `b`

- Search backwards.

`c`

- Search from cursor position.

`e`

- Search in the selection only.

`r`

- Do regular expression search. If set, you may use `\N` where N is a number to represent captures in the replacement string.

`s`

- Do case sensitive search.

`p`

- Prompt for permission to replace the next occurrence.

`w`

- Match whole words only.

    dateSTRING format

- Inserts a date/time string as defined by the specified `format`, or the format yyyy-MM-dd hh:mm:ss if none is specified. The following translations are done when interpreting `format`:

  `d` | The day as number without a leading zero (1-31).
  `dd` | The day as number with a leading zero (01-31).
  `ddd` | The abbreviated localized day name (⪚ 'Mon'..'Sun').
  `dddd` | The long localized day name (⪚ 'Monday'..'Sunday').
  `M` | The month as number without a leading zero (1-12).
  `MM` | The month as number with a leading zero (01-12).
  `MMMM` | The long localized month name (⪚ 'January'..'December').
  `MMM` | The abbreviated localized month name (⪚ 'Jan'..'Dec').
  `yy` | The year as two digit number (00-99).
  `yyyy` | The year as four digit number (1752-8000).
  `h` | The hour without a leading zero (0..23 or 1..12 if AM/PM display).
  `hh` | The hour with a leading zero (00..23 or 01..12 if AM/PM display).
  `m` | The minute without a leading zero (0..59).
  `mm` | The minute with a leading zero (00..59).
  `s` | The second without a leading zero (0..59).
  `ss` | The second with a leading zero (00..59).
  `z` | The milliseconds without leading zeroes (0..999).
  `zzz` | The milliseconds with leading zeroes (000..999).
  `AP` | Use AM/PM display. AP will be replaced by either "AM" or "PM".
  `ap` | Use am/pm display. ap will be replaced by either "am" or "pm".

    charSTRING identifier

- This command allows you to insert literal characters by their numerical identifier, in decimal, octal or hexadecimal form. To use it launch the Editing Command dialog and type `char: [number]` in the entry box, then hit OK.

  ##### `char` examples

  Input: `char:234`

  Output: `ê`

  Input: `char:0x1234`

  Output: `ሴ`

`s///[ig]` `%s///[ig]`

- This command does a sed-like search/replace operation on the current line, or on the whole file (`%s///`).

  In short, the text is searched for text matching the search pattern, the regular expression between the first and the second slash, and when a match is found, the matching part of the text is replaced with the expression between the second and last slash. Parentheses in the search pattern create back references, that is the command remembers which part of the string matched in the parentheses; these strings can be reused in the replace pattern, referred to as `\1` for the first set of parentheses, `\2` for the second and so on.

  To search for a literal `(` or `)`, you need to escape it using a backslash character: `\(\)`

  If you put an `i` at the end of the expression, the matching will be case insensitive. If you put a `g` at the end, all occurrences of the pattern will be replaced, otherwise only the first occurrence is replaced.

  ##### Replacing text in the current line

  Your friendly compiler just stopped, telling you that the class `myClass` mentioned in line 3902 in your source file is not defined.

  "Buckle!" you think, it is of course `MyClass`. You go to line 3902, and instead of trying to find the word in the text, you launch the Editing Command Dialog, enter `s/myclass/MyClass/i`, hit the OK button, save the file and compile – successfully without the error.

  ##### Replacing text in the whole file

  Imagine that you have a file, in which you mention a Miss Jensen several times, when someone comes in and tells you that she just got married to Mr Jones. You want, of course, to replace each and every occurrence of Miss Jensen with Ms Jones.

  Enter the command line and issue the command `%s/Miss Jensen/Ms Jones/` and hit return, you are done.

  ##### A More Advanced Example

  This example makes use of back references as well as a character class (if you do not know what that is, please refer to the related documentation mentioned below).

  Suppose you have the following line:

      void MyClass::DoStringOps( String      &foo, String &bar, String *p, int  &a, int &b )

  Now you realize that this is not nice code, and decide that you want to use the `const` keyword for all address of arguments, those characterized by the & operator in front of the argument name. You would also like to simplify the white space, so that there is only 1 whitespace character between each word.

  Launch the Editing Command Dialog, and enter: `s/\s+(\w+)\s+(&)/ const \1 \2/g` and hit the OK button. The `g` at the end of the expression makes the regular expression recompile for each match to save the backreferences.

  Output: `void MyClass::DoStringOps( const String &foo, const String &bar, String *p, const int &a, const int &b )`

  Mission completed! Now, what happened? Well, we looked for some white space (`\s+`) followed by one or more alphabetic characters (`\w+`) followed by some more whitespace (`\s+`) followed by an ampersand, and in the process saved the alphabetic chunk and the ampersand for reuse in the replace operation. Then we replaced the matching part of our line with one whitespace followed by const followed by one whitespace followed by our saved alphabetical chunk (`\1`) followed by one whitespace followed by our saved ampersand (`\2`)

  Now in some cases the alphabetical chunk was String, in some int, so using the character class `\w` and the `+` quantifier proved a valuable asset.

    sort

- Sorts the selected text or entire document.

    natsort

- Sort the selected lines or entire document naturally.

  ##### `sort` vs. `natsort`

  ``sort`(a10, a1, a2)` results in `a1, a10, a2`

  ``natsort`(a10, a1, a2)` results in `a1, a2, a10`

    moveLinesDown

- Move selected lines down.

    moveLinesUp

- Move selected lines up.

    uniq

- Remove duplicated lines from the selected text or the whole document.

    rtrim

- Remove trailing space from the selected text or the whole document.

    ltrim

- Remove leading space from the selected text or the whole document.

    joinSTRING separator

- Join selected lines or whole document. Optionally takes a parameter defining a separator, for example: ``join` ', '`

    rmblank

- Remove all blank spaces from the selected text or the whole document.

    alignon

- This command aligns lines in the selected block or whole document on the column given by a regular expression given as an argument.

  If you give an empty pattern it will align on the first non-blank character by default.

  If the pattern has a capture it will indent on the captured match.

  Examples:

  `alignon -` will insert spaces before the first '-' of each lines to align them all on the same column.

  `alignon :\\s+(.)` will insert spaces before the first non-blank character that occurs after a colon to align them all on the same column.

    unwrap

- Unwrap the selected text or the whole document.

    eachSTRING script

- Given a &javascript; function as an argument, call that for the list of selected lines and replace them with the return value of that callback.

  ##### Join selected lines

  ``each` 'function(lines){return lines.join(", ")}'`

  Or, more briefly:

  ``each` 'lines.join(", ")'`

    filterSTRING script

- Given a &javascript; function as an argument, call that for the list of selected lines and remove those where the callback returns false.

  ##### Remove blank lines

  ``filter` 'function(1){return 1.length > 0;}'`

  Or, more briefly:

  ``filter` 'line.length > 0'`

    mapSTRING script

- Given a &javascript; function as an argument, call that for the list of selected lines and replace the line with the value of the callback.

  ##### Remove blank lines

  ``map` 'function(line){return line.replace(/^s+/,"");}'`

  Or, more briefly:

  ``map` 'line.replace(/^s+/,"")'`

    duplicateLinesUp

- Duplicate the selected lines above the current selection.

    duplicateLinesDown

- Duplicate the selected lines below the current selection.

#### Commands for navigation

    gotoINT line

- This command navigates to the specified line.

    grep STRING pattern

- Search the document for the regular expression `pattern`. For more information, see regular-expressions.

    findSTRING pattern

- This command navigates to the first occurrence of `pattern` according to the configuration. Following occurrences can be found using Edit Find Next (the default shortcut is F3).

  The find command can be configured by appending a colon followed by one or more options, the form is `find:options pattern`. The following options are supported:

  `b`

- Search backwards.

`c`

- Search from cursor position.

`e`

- Search in the selection only.

`r`

- Do regular expression search. If set, you may use `\N` where N is a number to represent captures in the replacement string.

`s`

- Do case sensitive search.

`w`

- Match whole words only.

    ifindSTRING pattern

- This command provides as-you-type searching. You can configure the behavior of the search by appending a colon followed by one or more options, like this: `ifind:options pattern`. Allowed options are:

  `b`

- Search backwards.

`r`

- Do regular expression search.

`s`

- Do case sensitive search.

`c`

- Search from cursor position.

#### Commands for Basic Editor Functions (These depend on the application the editor component is used in)

    w

- Save the current document.

    wa

- Save all currently open documents.

    q

- Close the current document.

    qa

- Close all open documents.

    wq

- Save and close the current document.

    wqa

- Save and close all currently open documents.

    x

- Save and close the current document only if it has changed.

    x

- Save and close all currently open documents only if they have changed.

    bp

- Go to the previous document in the documents list.

    bn

- Go to the next document in the documents list.

    new

- Open a new document in horizontal split view.

    vnew

- Open a new document in vertical split view.

    e

- Reload the current document if it has changed on disk.

    enew

- Edit a new document.

    print

- Open the Print dialog to print the current document.

## Using Code Folding

Code folding allows you to hide parts of a document in the editor, making it easier to overview large documents. In &kappname; the foldable regions are calculated using rules defined in the syntax highlight definitions, and therefore it is only available in some formats - typically program source code, XML markup and similar. Most highlight definitions supporting code folding also lets you manually define foldable regions, typically using the `BEGIN` and `END` keywords.

To use the code folding feature, activate the folding markers using View Show Folding Markers menu item if they are not already visible. The Folding Markers Pane on the left side of the screen displays a graphical view of the foldable regions, with triangle symbols to indicate the possible operation on a given region: a top down triangle means that the region is expanded, clicking it will collapse the region and a right pointing triangle will be displayed instead.

Three commands are provided to manipulate the state of folding regions, see the menu documentation.

The folded lines are remembered when a file is closed, so when you reopen the file the folded nodes will still be folded. This applies to reload operations as well.

If you do not want to use the code folding feature, you can disable the Show folding markers (if available) option in the Appearance page of the editor configuration.