Skip to content

Indentation and Tabulation

Andrey Orst edited this page Nov 19, 2018 · 20 revisions

Tabs versus Spaces (Silicon Valley)

Paste some text from the clipboard without triggering indentation

Use \ before entering insert mode to prevent hooks from being executed in this insert session (\ i for example, then use your terminal pasting capability like Shift Insert or mouse middle click).

Indentation and Tab handling — Through a mapping

map global insert <tab> '<a-;><gt>'
map global insert <s-tab> '<a-;><lt>'

Indentation and Tab handling — Through hooks

hook global InsertChar \t %{ try %{
  execute-keys -draft "h<a-h><a-k>\A\h+\z<ret><a-;>;%opt{indentwidth}@"
}}
hook global InsertDelete ' ' %{ try %{
  execute-keys -draft 'h<a-h><a-k>\A\h+\z<ret>i<space><esc><lt>'
}}

Use Tab for both indenting and completion

When the completion menu is visible in insert mode, Tab and Shift Tab will cycle through the available options. When the completion menu is not shown, or when the character before the cursor is whitespace, Tab and Shift Tab are just handled normally, so the previous tab-expansion example still works.

hook global InsertCompletionShow .* %{
    try %{
        # this command temporarily removes cursors preceded by whitespace;
        # if there are no cursors left, it raises an error, does not
        # continue to execute the mapping commands, and the error is eaten
        # by the `try` command so no warning appears.
        execute-keys -draft 'h<a-K>\h<ret>'
        map window insert <tab> <c-n>
        map window insert <s-tab> <c-p>
    }
}
hook global InsertCompletionHide .* %{
    unmap window insert <tab> <c-n>
    unmap window insert <s-tab> <c-p>
}

Smarttab, expandtab and noexpandtab

You can use smarttab.kak plugin. It provides these commands to toggle different policy when using Tab and > keys:

  • noexpandtab - use tab for everything.
    Tab will insert \t character, and > will use \t character when indenting. Aligning cursors with & uses \t character.
  • expandtab - use space for everything.
    Tab will insert %opt{tabstop} amount of spaces, and > will indent with spaces.
  • smarttab - indent with tab, align with space.
    Tab will insert \t character if your cursor is inside indentation area, e.g. before any non-whitespace character, and insert spaces if cursor is after any non-whitespace character. Aligning cursors with & uses space.

See also

You can’t perform that action at this time.