Power tool for shredding text in Insert mode
- 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
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.
Install this plugin using your favorite Vim package manager.
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.
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
Vim’s standard word motions are available:
e
- forward to the end of word, inclusiveE
- forward to the end of WORD, inclusivew
- word forward, exclusiveW
- WORD forward, exclusivege
- backward to the end of word, inclusivegE
- backward to the end of WORD, inclusiveb
- word backward, exclusiveB
- 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
In your .vimrc
, ensure backspace can consume whatever is in its way:
set backspace=indent,eol,start
For more details, see:
:help 'backspace'
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
If you find this plugin useful, check out these others originally by @reedes:
- vim-pencil - Rethinking Vim as a tool for writers
- vim-colors-pencil - color scheme for Vim inspired by IA Writer
- vim-lexical - building on Vim’s spell-check and thesaurus/dictionary completion
- vim-litecorrect - lightweight auto-correction for Vim
- vim-textobj-quote - extends Vim to support typographic (‘curly’) quotes
- vim-textobj-sentence - improving on Vim's native sentence motion command
- vim-thematic - modify Vim’s appearance to suit your task and environment
- vim-wordy - uncovering usage problems in writing
- vim-wheel - screen anchored cursor movement
If you’ve spotted a problem or have an idea on improving this plugin, please post it to the GitHub project issue page.