/vim-wordchipper

Power tool for shredding text in Insert mode

Primary LanguageVim ScriptOtherNOASSERTION

vim-wordchipper

Power tool for shredding text in Insert mode

Features

  • Conveniently delete words (and other text objects) while in Insert mode
  • Automatically forces a format on current paragraph when Vim’s autoformat is active
  • Useful with writing plugins like vim-pencil and when composing code comments
  • Pure Vimscript with no dependencies

Introduction

Though Vim’s Insert mode is typically used for entering new text, it nevertheless offers a limited capability to delete existing text with single keystrokes:

Key Description
<BS> or <C-h> Delete the character before the cursor
<Del> Delete the character under the cursor
<C-w> Delete the word before the cursor
<C-u> Delete all characters before the cursor in the current line

In addition, with Ctrl-o you can execute a command, which will then return to Insert mode. For example <C-o>dw can delete the word after the cursor.

For more details, see:

:help ins-special-keys

This plugin can extend that capability, such as to delete the word after the cursor with a single keystroke.

Installation

Install this plugin using your favorite Vim package manager.

Configuration

No keys are mapped by default. You must define the behavior of this plugin in your .vimrc.

For example, to re-map the Ctrl-E key to delete to the end of the next word:

inoremap <expr> <C-e> wordchipper#chipWith('de')

where de deletes text forward to the end of the next word, preserving any trailing space. (As an alternative, dw would consume the trailing space.)

By default, Ctrl-W will delete the word before the cursor. You can change this behavior to work with WORDS instead (i.e., including punctuation):

inoremap <expr> <C-w> wordchipper#chipWith('dB')

where dB deletes backwards to the end of the previous WORD.

wordchipper also can work with motions against non-word text objects, such as remapping Ctrl-y to delete to the end of the sentence:

inoremap <expr> <C-y> wordchipper#chipWith('d)')

This will use Vim’s default sentence text object, or an alternative like vim-textobj-sentence if installed.

Buffer-local mappings

You can also specify buffer-local mappings, such as initialized only for the Markdown and text file types...

augroup wordchipper
  autocmd!
  autocmd FileType markdown,mkd,text
      \   inoremap <buffer> <expr> <C-e> wordchipper#chipWith('de')
      \ | inoremap <buffer> <expr> <C-w> wordchipper#chipWith('dB')
      \ | inoremap <buffer> <expr> <C-y> wordchipper#chipWith('d)')
augroup END

Word motions

Vim’s standard word motions are available:

  • e - forward to the end of word, inclusive
  • E - forward to the end of WORD, inclusive
  • w - word forward, exclusive
  • W - WORD forward, exclusive
  • ge - backward to the end of word, inclusive
  • gE - backward to the end of WORD, inclusive
  • b - word backward, exclusive
  • B - WORD backward, exclusive

word’ is defined by iskeyword option, and generally consists of letters, digits, and underscores. ‘WORD’ generally consists of non-blank characters, including punctuation. For more details, consult the documentation:

:help word-motions

Recommended settings

In your .vimrc, ensure backspace can consume whatever is in its way:

set backspace=indent,eol,start

For more details, see:

:help 'backspace'

Prevent autoformat

If Vim’s autoformat is active during the Insert, such as when using HardPencil mode in the pencil plugin, wordchipper will force a format to the end of the paragraph when invoked. (Without wordchipper there would be no force of a format when deleting text, despite autoformat being active.)

You can disable the forced format in your .vimrc with

let g:wordchipper#autoformat = 0    " default is 1

See also

If you find this plugin useful, check out these others originally by @reedes:

Future development

If you’ve spotted a problem or have an idea on improving this plugin, please post it to the GitHub project issue page.