vdegenne
vdegenne

Reputation: 13308

How can I select all the text on a line without the indentation in Vim?

I want to replace all the text of the current line under the cursor and change the text so I start typing the new code. But I want to keep the indentation.

Currently I am using ddO. This will delete the line and open a line before the cursor in insert mode, it's good because vim will take notice of the previous indentation and place the cursor as expected.
But I want to use c similar to ciw (change inner word) because it feels natural to think "change the line". Vc is almost what I want but it will lose the indentation.

Any idea ?

Upvotes: 0

Views: 558

Answers (2)

yolenoyer
yolenoyer

Reputation: 9465

You can try this (to put in your vimrc):

:onoremap ii :<c-u>normal! v^o$h<cr>
:xnoremap ii ^o$h
  • The first line defines the mapping ii which will work with any command expecting a motion (cii, dii, yii...).
  • The second mapping allows to use it in visual mode (e.g., vii).

Brief explanation of the :normal! command:

  • v : visual mode, ^ : go to 1st non-blank char, o : go to the opposite side of the selection, $h : go past to the end of line then go 1 char left.

Upvotes: 1

Arjen Dijkstra
Arjen Dijkstra

Reputation: 1559

Maybe you are looking for cc?

:h cc

["x]cc Delete [count] lines [into register x] and start insert |linewise|. If 'autoindent' is on, preserve the indent of the first line.

Upvotes: 5

Related Questions