smarttab.kak
is a plugin for Kakoune.
It provides three different ways for handling indentation and alignment with the tab key.
With plug.kak
Add this to your kakrc
:
plug "andreyorst/smarttab.kak"
Source your kakrc
, or restart Kakoune.
Then execute :plug-install
.
Or, if you don't want to restart Kakoune or source its config, simply run plug-install andreyorst/smarttab.kak
.
It will then be enabled automatically.
Clone this repo:
git clone https://github.com/andreyorst/smarttab.kak.git
You can put this repo in your autoload
directory, or else manually source
the smarttab.kak
script in your configuration file.
After that, you can use smarttab.kak
.
This plugin adds three commands to toggle between different policies when using the Tab and > keys:
noexpandtab
- usetab
for everything. Tab will insert the\t
character, and > will use the\t
character when indenting. Aligning cursors with & uses the\t
character.expandtab
- usespace
for everything. Tab will insert%opt{indentwidth}
amount of spaces, and > will indent with spaces.smarttab
- indent withtab
, align withspace
. Tab will insert the\t
character if your cursor is inside an indentation area, e.g., before any non-whitespace character, and insert spaces if the cursor is after any non-whitespace character. Aligning cursors with & usesspace
.autoconfigtab
- choose the above based upon one of the existing settings (see later section).
By default, smarttab.kak affects only the Tab and > keys.
If you want to deindent lines that are being indented with spaces when hitting Backspace, you can set the softtabstop
option.
This option specifies how many space
s should be treated as a single tab
character when deleting them with a backspace.
In order to automatically enable different modes for different languages, you can use hook
s like so:
hook global WinSetOption filetype=c smarttab
hook global WinSetOption filetype=rust expandtab
To adjust smarttab.kak related options, you need to use the ModuleLoaded
hook, because all options are defined withing the smarttab
module:
hook global ModuleLoaded smarttab %{
set-option global softtabstop 4
# you can configure text that is being used to represent curent active mode
set-option global smarttab_expandtab_mode_name 'exp'
set-option global smarttab_noexpandtab_mode_name 'noexp'
set-option global smarttab_smarttab_mode_name 'smart'
}
If you've used plug.kak for installation, it's better to configure smarttab.kak from within the plug
command because it can handle lazy loading the configurations for the plugin, as well as configure the editor's behavior:
plug "andreyorst/smarttab.kak" defer smarttab %{
# when `backspace' is pressed, 4 spaces are deleted at once
set-option global softtabstop 4
} config %{
# these languages will use `expandtab' behavior
hook global WinSetOption filetype=(rust|markdown|kak|lisp|scheme|sh|perl) expandtab
# these languages will use `noexpandtab' behavior
hook global WinSetOption filetype=(makefile|gas) noexpandtab
# these languages will use `smarttab' behavior
hook global WinSetOption filetype=(c|cpp) smarttab
}
In your kakrc
add:
hook global BufOpenFile .* _mode_
hook global BufNewFile .* _mode_
Where the _mode_
is one of the smarttab.kak
modes, described above.
If you just want to set the behavior based upon your editorconfig
settings, you can use the autoconfigtab
setting:
hook global BufCreate .* %{
editorconfig-load
autoconfigtab
}
This config will choose expandtab
or noexpandtab
based upon whether indent_style
is set as space
or tab
respectively.
If you'd prefer to use smarttab
instead of noexpandtab
for indent_style = tab
(without affecting indent_style = space
), you can manually override the aligntab
option to false
before running autoconfigtab
, as seen in the below config:
hook global BufCreate .* %{
editorconfig-load
set-option buffer aligntab false
autoconfigtab
}
Currently, autoconfigtab
does not cover the case where indentwidth
is nonzero but aligntab
is set to true
, as this would mean indenting with spaces and aligning with tabs.
In this particular case, tab alignment takes priority and noexpandtab
is chosen.