# Refactoring in Skink: rename, move, extract, safe delete

> Refactor safely in Skink: rename a symbol everywhere, move files with import updates, extract a variable or function, safe delete, all reviewed in a preview.

Source: https://skink.cc/code/refactoring/

Press Alt+Shift+R to rename the symbol at the cursor everywhere. Any change that touches several files opens the **refactor preview** first, so you check it before anything is applied.

Other refactors are commands:

- `:mv` moves a file and updates its imports.
- `:extract var` or `:extract fn` pulls out the selection.
- `:safedelete` removes a declaration nobody uses.

Most of these use your language server. See [Code intelligence](https://skink.cc/code/code-intelligence/) for setup.

## The refactor preview

The preview lists the files and hunks a change will touch, each with a checkbox, and a side-by-side diff of the selected one.

| Key | Does |
|---|---|
| ↑ / ↓ | Move between files and hunks |
| Space | Check or uncheck |
| a | Check all |
| n | Check none |
| Enter | Apply the checked changes |
| Esc | Cancel |

Applying is one undo step per file. The changed files are left unsaved, so you get a last look and save them yourself.

The same preview is used for:

- rename and multi-file code actions
- file moves
- project-wide replace (`:greprep`) and structural rewrite (`:srep`)
- edits from AI agents

## Rename a symbol

Alt+Shift+R asks for the new name. Or give it straight away:

```text
:renamesymbol new_name
:rn new_name
```

The language server finds every real use, across files, so unrelated names that happen to match are left alone.

## Move or rename a file

```text
:mv src/util/strings.rs
```

This moves or renames the current file, relative to the project root. Open tabs follow the file.

Running language servers are asked first (`workspace/willRenameFiles`). The import and module-path changes they suggest open in the preview.

Renaming in the file tree (r or F2 on a file) does the same. New folders in the path are created for you.

## Extract a variable or function

Select an expression or some lines, then run one of:

```text
:extract var [NAME]
:extract fn [NAME]
```

This uses the language server's extract code action. `extract var` also works without a server for Rust, TypeScript/JavaScript and Python.

## Safe delete

Put the cursor on a function, struct or class and run `:safedelete`. Skink runs find-references first:

- **No uses outside the declaration:** the declaration, with its doc comments and attributes, is removed through the preview.
- **Uses found:** they are listed instead, so you can deal with them first.

`:safedelete!` skips the check and opens the deletion in the preview unchecked, for you to decide.

## Code actions

Alt+Enter (or `:fix`) shows the server's quick fixes and refactorings for the cursor position, such as "add missing import" or "inline variable".

A lightbulb in the sign column means some are available. Ones that touch several files open the preview.

## Structural search and replace

For changes by shape rather than by name, use ast-grep patterns:

```text
:sgrep foo($A, $$$REST)
:srep foo($A, $B) => bar($B, $A)
```

`:srep` rewrites every match through the preview. See [Project search](https://skink.cc/code/project-search/).

## Smaller edits in one file

| Key | Typed command | Does |
|---|---|---|
| Alt+O / Alt+I | `:expand` / `:contract` | Grow / shrink the selection to the enclosing syntax node |
| Alt+↑ / Alt+↓ | `:moveup` / `:movedown` | Move lines |
| Ctrl+D | `:addcursor` | Multiple cursors on the next match ([more](https://skink.cc/editing/multiple-cursors/)) |
| | `:SortLines`, `:JoinLines` | Sort or join the selected lines |
| | `:UpperCase`, `:LowerCase`, `:TitleCase` | Change case of the selection or word |

Every key also has a typed command. See [Typed commands](https://skink.cc/editing/typed-commands/).

**Note:**
How much a refactor can do depends on your language server. rust-analyzer and the TypeScript server offer a lot; others offer less. Skink is alpha, and more built-in refactors are planned.
