Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 17, 2022 04:30 am GMT

Vim keybindings for efficient text editing

Hello, I am Taras. I am working as a backend developer.

If youre a software engineer, you probably heard about Vim, a free, open-source, screen-based text editor program.

In this article, Ill show you how you can radically speed up your development workflow by leveraging one of the most potent things about Vim its keybindings without even having to use Vim.

You might be thinking, ugh, keybindings, too nerdy and difficult, I dont need that Hold on! Its not as difficult as you might think, and you dont need to memorize all of them. Once you understand the core logic behind keybindings, the following experience should come naturally. Let me show you!

What is so special in Vim keybinding?

Vim has different editing modes and it lets you use easy-to-remember keybinding without needing to add a couple of key modifications (ctrl+shift+, alt+shift+). It has it's language for editing text. And once you get the basics you can combine them to do different actions. Everything you can think about editing text there is probably a keybinding for it, if not you can easily create it. Let's start with Vim modes.

Normal Mode

You cant write text in this mode. By default, Vim starts in this mode. In Normal mode, key presses dont work as one would expect. This mode is responsible for editing and moving around. Remember, this is what makes Vim different.

Basic movements that I use on a daily basis

Cursor movement:

  • hmove one character left
  • jmove one row down
  • kmove one row up
  • lmove one character right

If you are a touch typist like me you will like these movements because you dont have to leave a home row. Keys h and l were easy to remember for me but jk took me some time to used to.

Word movements:

  • wmove to the beginning of the next word
  • bmove to the previous beginning of the word
  • emove to the end of the word
  • gemove to the end of the previous word
  • Wmove to the beginning of the next word after a whitespace
  • Bmove to the beginning of the previous word before a whitespace
  • Emove to the end of a word before a whitespace

Beginning/End of line movement:

  • 0move to the beginning of the line
  • $move to the end of the line

targeted movements:

  • move to the next occurrence of the current word under cursor: ``
  • move to the previous occurrence of the current word:#
  • move (un)til character:t<char>
  • move back (un)til character:T<char>
  • move to found character:f<char>
  • move back to found character:F<char>
  • move to match:/<regex>
  • move back to match:?<regex>
  • move to the next occurrence of a match:n
  • move to the previous occurrence of a match:N

Most keybindings are mnemonic (w - word, e - end) and some of them have regex expression philosophy beyond.

Insert Mode

This mode works the way text editing works throughout the OS. Same shortcuts (cmd+c,cmd+p, etc), same movements (arrow keys, page up/down,ctrl+a,ctrl+e, etc). To exit this mode you need to press escape

There are many movement commands that can result in switching to Insert Mode:

  • insert before letter under cursor:i
  • insert after letter under cursor:a
  • insert at the start of the line:I
  • insert at the end of the line:A
  • insert on a new line:o
  • insert on a new line above:O
  • substitute:s
  • change:c

Visual Mode

A mode for creating selections. This mode can be extremely useful for identifying chunks of text to be manipulated (e.g. apply reg expression for selected text).

To enter Visual Mode (starting from Normal Mode):

  • vto select by character
  • Vto select by line

A Language of Text Editing

I really enjoy mnemonic keybindings, because they are easy to remember. Vim has them! Many keybindings can be conveniently thought of as the "verbs", "nouns" and "prepositions" that make up the "language".

Examples of "verbs":

  • change (c)
  • replace (r)
  • substitute (s)
  • delete (d)
  • yank (y)
  • put (p)

NOTE: 'change', 'replace', AND 'substitute' aren't the same thing!

  • Change deletes everything in the next movement and puts you in Insert Mode (though with a selection, it behaves like a substitute).
  • Substitute immediately deletes the current character (or selection) and puts you in Insert Mode.
  • Replace never enters Insert Mode at all, but just replaces the current character (or every character in a selection) with the provided character.

Examples of "nouns":

  • word (w)
  • end (e)
  • beginning (b)

Examples of "prepositions":

  • inside (i)
  • around (a)
  • (un)til (t) You can combine these parts of the "language" to perform editing tasks:
  • delete to the end of a word:dw(orde)
  • delete the current word:diw
  • yank to the end of word:ye
  • change inside parentheses:ci)(orci()
  • delete around brackets:da](orda[)

Of course, Vim has more editing and movement commands and I want you to give it a try. I am sure that it can be a life-changer for your workflow!

Afterword

Awesome personal remaps that I think are really helpful

  1. RemapCAPSLOCKtoESC if pressed alone, and to CTRL/CMD if pressed with the key. For me personally, it's easier to reach out.
  2. Remap WIN/OPTION + hjkl to arrow keys
  3. Remap WIN/OPTION + N go to the next word and WIN/OPTION + P go to the previous word

Where I use vim keybindings

  • Neovim - my main text editor.
  • Terminal I am using zsh-vi-mode plugin for zsh.
  • Obsidian for writing notes
  • Google Chrome I am using Surfingkeys plugin for browsing

Original Link: https://dev.to/taraslis453/vim-keybindings-for-efficient-text-editing-ke2

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To