4

I want to achieve the same thing as S ESC without entering insert mode. Is there a way to do this?

The best I've come up with is d$, but then you have to make sure you're at the front of a line which makes it ^ d$.

Visually, with cursor as |; before:

Beautiful sentence that I wa|nt to destroy.

After:


3
  • What about dd?
    – DDS
    Mar 25, 2022 at 14:16
  • 1
    @DDS: That deletes the line, OP wants the line to remain but empty.
    – Ben Voigt
    Mar 25, 2022 at 15:34
  • 1
    Just curious: is there any problem with entering insert mode? Is it slower? Is it even noticeable? Mar 28, 2022 at 15:46

4 Answers 4

9

To save one keystroke, instead of ^d$, do ^D

Also faster because you keep the Shift key down for both ^ and D

7
  • 2
    cc<esc> in another way to do it (or S<esc>).
    – Biggybi
    Mar 25, 2022 at 7:40
  • "Also faster because you keep the Shift key down for both ^ and D". On Qwerty keyboards. On mine, I have to press ^ twice (without Shift) in order to get ^. So it would be ^^+Shift-D. cc<esc> is indeed faster. Mar 25, 2022 at 11:58
  • 1
    @Biggybi S<esc> was specifically called out in the question as undesired because OP doesn't want to enter insert mode. The same thing would apply to cc<esc>.
    – 8bittree
    Mar 25, 2022 at 15:56
  • @8bittree indeed you're right (that's why I only bring it in a comment, not an answer).
    – Biggybi
    Mar 26, 2022 at 0:57
  • 1
    @EricDuminil Probably better to ask young_souvlaki. All I can think of is maybe to avoid triggering an autocommand. Or to avoid having to reach for the <esc> key.
    – 8bittree
    Mar 28, 2022 at 15:45
2

Another approach is to set a :h omap for a line.

For example :

onoremap il :<c-u>normal! $v0<cr>

Then you can use dil to delete inside line. Of course, yil would yank the line, and so on.

1

As far as I know there is no blanking line command. I imagine you would need to make your own mapping

nnoremap gS :<c-u>call setline('.', '')<cr>

I am not sure how useful that would be

1

Well, to add to all the answers:

  1. :pu_|-d<CR> which you can map to, for instance, :nnoremap dr :<C-u>pu_|-d<CR>
  2. create a text object "a line" and delete a line with dil or dal (and this could be a fancy one to operate on screenlines for example)
" line text object
func ObjLine(inner)
    if a:inner
        " for non-wrapped line following works:
        " normal! g_v^
        " but I want to handle wrapped
        normal! g$
        search('\S', 'cb')
        normal! vg^
    else
        normal! $v0
    endif
endfunc
xnoremap <silent> il :<C-u>call ObjLine(1)<CR>
onoremap <silent> il :<C-u>normal vil<CR>
xnoremap <silent> al :<C-u>call ObjLine(0)<CR>
onoremap <silent> al :<C-u>normal val<CR>

enter image description here

3
  • 1
    Distinguishing il and al for leading spaces is quite smart!
    – Biggybi
    Mar 27, 2022 at 6:30
  • 1
    @Biggybi not only leading, trailing too.
    – Maxim Kim
    Mar 30, 2022 at 8:50
  • Indeed -- good thing you point this imprecision.
    – Biggybi
    Mar 30, 2022 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.