Difference between revisions of "Short Notes on ViM"
(Created page with "== Delete all lines matching a pattern == Delete all empty lines: <pre>:g/^\s*$/d</pre> Delete all single-line comments in a C++ file: <pre>:g/^\s*\/\//d</pre> Delete all line...") |
(No difference)
|
Revision as of 22:40, 1 June 2012
Delete all lines matching a pattern
Delete all empty lines:
:g/^\s*$/d
Delete all single-line comments in a C++ file:
:g/^\s*\/\//d
Delete all lines that do not match given pattern (:g! is equivalent to :v):
:g!/pattern/d
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
vipgq
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.