Short Notes on ViM

From PaskvilWiki
Revision as of 17:11, 30 August 2013 by Admin (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Delete all lines matching a pattern

Delete all empty lines:


Delete all single-line comments in a C++ file:


Delete all lines that do not match given pattern (:g! is equivalent to :v):


Directly type HTML entities

Main power of ViM is that all of it's commands and macros are represented by the "normal" keystrokes. That way, you can map certain characters to direct input (or to commands, etc).

To have accented characters directly translated to HTML entities, you can just add mappings like this to ~/.vimrc (or define them on when needed using :imap command):

imap Č Č
imap č č
imap Š Š
imap š š
imap Ž Ž
imap ž ž
" ... and so on ...

Then, when you type Č, ViM will automatically insert Č instead.

Reformat a plain text paragraph

ViM includes full support of plain text editing tools.

One of the most helpful ones is textwidth variable, that tells ViM how long the lines should be, and while typing it automatically breaks lines to the given width (of course, preserving full words).

But when you edit text within already formatted paragraph, or if you have existing paragraphs and want to reformat them, you have to do this yourself. Fortunately, ViM helps you with this also - simply place cursor within the paragraph, and in normal mode (press Esc if you're in insert mode) press


The vip combination simply selects the paragraph (visual inner paragraph), and gq reformats it.

For ease of use, you can define a shortcut for use from insert mode:

imap <C-v>r <Esc>vipgqi

Now, pressing Ctrl-v and r in insert mode will reformat current paragraph, and return to insert mode.