In the margin order form, switch the trading mode to Repay. The form shows exactly how much you owe in that pair and caps your maximum order size at that total.
How it works
Repay mode shows the borrowed amount plus accrued interest in that pair. When your order fills, everything you receive is automatically used for repayment: interest first, then the borrowed amount.
Advantage over a regular order
You get the same flexibility as a normal order, including limit orders so you can choose your price, with a built-in guardrail: you can never repay more than you owe, so there's no risk of accidentally opening a position in the opposite direction. Repay mode is only available when you have something to repay in that pair.
Example: you owe 200.58 USD on the BTC/USD pair. In Repay mode, the form shows Total to repay: 200.58 and limits your order to that amount. You place a limit sell of BTC at your chosen price; when it fills, the USD you receive goes directly to repayment and your position is closed.