CuteMachine

How To Add A Key Mapping To Clear The Current Editor In Vim For VSCode

Today I learned how to created a mapping in Vim for VSCode that clears the current editor contents.

Task: Create a keyboard shortcut <leader>-c that clears the current VSCode editor's contents.

Here is my answer on StackOverflow regarding the same concern.

To add the keyboard shortcut open settings.json and add the following code in the section vim.normalModeKeyBindingsNonRecursive.

    "vim.normalModeKeyBindingsNonRecursive": [
        {
        "before": ["<leader>", "c"],
        "after": ["g", "g", "d", "G"]
        }
     ]

In Vim gg moves the cursor to the first character in the first line. The command dG deletes everything from the current position to the movement command G. It tells Vim to move the cursor to the end of the editor.

As soon as you save the file settings.json the new key mapping will be available. You do not need to restart VSCode.

Vim Text Object Entire

A better solution is to use the built-in vim-textobj-entire

The text object ae represents the entire content of a buffer in Vim for VSCode. You can use this text object in combination with the d command.

dae will delete the entire content of the current buffer.

You can certainly use the same text object to yank the current buffer content to the pasteboard: yae

Posted on CuteMachine in vscode and vim.

Jo's Profile ImageWritten by Jo who lives and works in Frankfurt building digital doodah. Stalk him on Twitter.

TOS • Privacy • Disclaimer