Difference between revisions of "Short Notes on ViM"

From PaskvilWiki
Jump to: navigation, search
(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...")
 
(Directly type HTML entities)
 
Line 15: Line 15:
  
 
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 <tt>:imap</tt> command):
 
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 <tt>:imap</tt> command):
<pre>imap Č &#268;
+
<pre>imap Č &amp;#268;
imap č &#269;
+
imap č &amp;#269;
imap Š &#352;
+
imap Š &amp;#352;
imap š &#353;
+
imap š &amp;#353;
imap Ž &#381;
+
imap Ž &amp;#381;
imap ž &#382;
+
imap ž &amp;#382;
 
" ... and so on ...</pre>
 
" ... and so on ...</pre>
 
Then, when you type ''Č'', ViM will automatically insert <tt>&amp;#268;</tt> instead.
 
Then, when you type ''Č'', ViM will automatically insert <tt>&amp;#268;</tt> instead.

Latest revision as of 16:11, 30 August 2013

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 Č &#268;
imap č &#269;
imap Š &#352;
imap š &#353;
imap Ž &#381;
imap ž &#382;
" ... and so on ...

Then, when you type Č, ViM will automatically insert &#268; 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.