From Fedora Project Wiki

(fixing brackets around cats)
(Add mini how to about editing surrounding. Shamely copy/paste documentation of plugin and adapt it to mediawiki.)
 
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
[[Category:Draft Documentation]] [[Category:Docs Project]]
{{needs love}}{{draft}}
 
== Splits ==
 
== Tabs ==
 
To open all of the XML files within a book in tabs start VIM from the root of the book with the following command:
 
<pre>
vim -p en-US/*.xml
</pre>
 
Issuing <code>:tabc</code> closes the current tab, issuing <code>:tabn</code> switches to the next tab and issuing <code>:tabp</code> switches to the previous tab.
 
For more information on using tabs issue the following command in VIM:
 
<pre>
:help tab-page-intro
</pre>
 
== Folding ==
 
As of VIM 7.0 support for folds is included in the editor. Folds allow sections of a document which are not being actively worked on to be 'folded' into themselves and hidden. Programmers often use folds to hide and show functions, classes and structures but the functionality is equally useful when editing DocBook XML.
 
=== Fold methods ===
 
Whether VIM folds content and how it folds it depends on the <code>foldmethod</code> setting. There are a number of valid options:
 
* <code>manual</code> - Folds are created manually.
* <code>indent</code> - Folds are created based on indentation levels.
* <code>expr</code> - Folds are created based on the value of <code>foldexpr</code>.
* <code>marker</code> - Folds are created based on markers.
* <code>syntax</code> - Folds are created based on syntax highlighting items.
* <code>diff</code> - Folds are created based on whether text has/hasn't changed, text that hasn't changed is folded.
 
When working with consistently indented XML content for example the <code>indent</code> method is very easy to invoke and requires no additional setup. It can be accessed by issuing:
 
<pre>
set foldmethod=indent
</pre>
 
Issuing this command while a consistently indented XML file is open in the buffer will result in a number of folds being created. Folds can be opened by placing the cursor on the line denoting the fold. For example in the following XML block there is an appendix opened on line 4 and closed on line 184. The enclosed content is a fold which contains 179 lines of text.
 
<pre>
  4 <appendix id="chap-RedHat_Enterprise_Virtualization-Administration_Guide-Backup_and_Recovery">
  5 +--179 lines: <title>&PRODUCT; Manager backup and recovery</title>----------------
184 </appendix>   
</pre>
 
Information on the various fold methods and how they handle folds can be accessed by issuing <code>:help foldmethod</code> in VIM.
 
=== Work with folds ===
 
Some frequently used commands when working with folds are:
 
* Use <code>zo</code> to <b>open</b> the fold under the cursor.
* Use <code>zc</code> to <b>close</b> the fold under the cursor.
* Use <code>zR</code> to <b>open all</b> folds in the buffer.
* Use <code>zM</code> to <b>close all</b> folds in the buffer.
* Use <code>zf</code> to <b>create</b> a fold from a visual selection when <code>foldmethod</code> is set to manual.
* Use <code>zd</code> to <b>delete</b> a fold at the cursor when <code>foldmethod</code> is set to manual.
 
There are many other options to facilitate modification of the way folds behave in VIM. Further information on the use of folds in VIM can be accessed by issuing <code>:help fold</code> from command mode.
 
=== Save folds across sessions ===
 
By default folds are discarded when exiting. Folds can be explicitly written to disk by issuing <code>:mkview</code>. When the file is next opened <code>:loadview</code> can be issued to reload previously saved folds for the document. Alternatively the following lines can be placed in the users <code>.vimrc</code> file to ensure that folds are automatically loaded and saved when buffers are opened and closed respectively.
 
<pre>
au BufWinLeave * mkview
au BufWinEnter * silent loadview
</pre>
 
== Work with surround ==
 
A handy vim plugin exists that allows you to quickly edit surrounding XML, HTML tags or parenthesis etc. First [http://www.vim.org/scripts/script.php?script_id=1697 download latest surround] plugins. Then unzip it into /usr/share/vim/vimfiles, surround.vim file should be in /usr/share/vim/vimfiles/plugin directory now. Install it's done.
 
 
Surround.vim is all about "surroundings": parentheses, brackets, quotes,
XML tags, and more.  The plugin provides mappings to easily delete,
change and add such surroundings in pairs.
 
It's easiest to explain with examples.  Press `cs"'` inside
 
    "Hello world!"
 
to change it to
 
    'Hello world!'
 
Now press `cs'<q>` to change it to
 
    &lt;q>Hello world!</q>
 
To go full circle, press `cst"` to get
 
    "Hello world!"
 
To remove the delimiters entirely, press `ds"`.
 
    Hello world!
 
Now with the cursor on "Hello", press `ysiw]` (`iw` is a text object).
 
    [Hello] world!
 
Let's make that braces and add some space (use `}` instead of `{` for no
space): `cs]{`
 
    { Hello } world!
 
Now wrap the entire line in parentheses with yssb or yss)
 
    ({ Hello } world!)
 
Revert to the original text: ds{ds)
 
    Hello world!
 
Emphasize hello: ysiw&lt;em>
 
    &lt;em>Hello&lt;/em> world!
 
Finally, let's try out visual mode. Press a capital V (for linewise
visual mode) followed by `S&lt;p class="important">`.
 
    &lt;p class="important">
      &lt;em>Hello</em> world!
    &lt;/p>
 
This plugin is very powerful for HTML and XML editing, a niche which
currently seems underfilled in Vim land.  (As opposed to HTML/XML
&lowast;inserting*, for which many plugins are available).  Adding, changing,
and removing pairs of tags simultaneously is a breeze.


== Vi config file hacks ==
== Vi config file hacks ==
=== Set options per file ===
For convenient editing, I add the following lines to the bottom of any DocBook files I'll be editing in Vi(m):
<pre><!--
vim: softtabstop=2:shiftwidth=2:expandtab:textwidth=72
--></pre>
=== Block indentation ===
To support quick indentation of visually selected blocks keys can be bound to decrease and increase the indentation level. In the example <code>.vimrc</code> lines below the '&lt;' key is bound to decrease indentation and the '&gt;' key is bound to increase indentation.
<pre>
vnoremap &lt; &lt;gv
vnoremap &gt; &gt;gv
</pre>


== Quick key tricks ==
== Quick key tricks ==


== Misc. tips ==
== Misc. tips ==
[[Category:Documentation tools]]

Latest revision as of 21:25, 13 March 2012

This page needs some love
This page should be revised or reconstructed to be more helpful. Problems may include being out of step with current team or project status or process.
This page is a draft only
It is still under construction and content may change. Do not rely on the information on this page.

Splits

Tabs

To open all of the XML files within a book in tabs start VIM from the root of the book with the following command:

vim -p en-US/*.xml

Issuing :tabc closes the current tab, issuing :tabn switches to the next tab and issuing :tabp switches to the previous tab.

For more information on using tabs issue the following command in VIM:

:help tab-page-intro

Folding

As of VIM 7.0 support for folds is included in the editor. Folds allow sections of a document which are not being actively worked on to be 'folded' into themselves and hidden. Programmers often use folds to hide and show functions, classes and structures but the functionality is equally useful when editing DocBook XML.

Fold methods

Whether VIM folds content and how it folds it depends on the foldmethod setting. There are a number of valid options:

  • manual - Folds are created manually.
  • indent - Folds are created based on indentation levels.
  • expr - Folds are created based on the value of foldexpr.
  • marker - Folds are created based on markers.
  • syntax - Folds are created based on syntax highlighting items.
  • diff - Folds are created based on whether text has/hasn't changed, text that hasn't changed is folded.

When working with consistently indented XML content for example the indent method is very easy to invoke and requires no additional setup. It can be accessed by issuing:

set foldmethod=indent

Issuing this command while a consistently indented XML file is open in the buffer will result in a number of folds being created. Folds can be opened by placing the cursor on the line denoting the fold. For example in the following XML block there is an appendix opened on line 4 and closed on line 184. The enclosed content is a fold which contains 179 lines of text.

  4 <appendix id="chap-RedHat_Enterprise_Virtualization-Administration_Guide-Backup_and_Recovery">
  5 +--179 lines: <title>&PRODUCT; Manager backup and recovery</title>----------------
184 </appendix>     

Information on the various fold methods and how they handle folds can be accessed by issuing :help foldmethod in VIM.

Work with folds

Some frequently used commands when working with folds are:

  • Use zo to open the fold under the cursor.
  • Use zc to close the fold under the cursor.
  • Use zR to open all folds in the buffer.
  • Use zM to close all folds in the buffer.
  • Use zf to create a fold from a visual selection when foldmethod is set to manual.
  • Use zd to delete a fold at the cursor when foldmethod is set to manual.

There are many other options to facilitate modification of the way folds behave in VIM. Further information on the use of folds in VIM can be accessed by issuing :help fold from command mode.

Save folds across sessions

By default folds are discarded when exiting. Folds can be explicitly written to disk by issuing :mkview. When the file is next opened :loadview can be issued to reload previously saved folds for the document. Alternatively the following lines can be placed in the users .vimrc file to ensure that folds are automatically loaded and saved when buffers are opened and closed respectively.

au BufWinLeave * mkview
au BufWinEnter * silent loadview

Work with surround

A handy vim plugin exists that allows you to quickly edit surrounding XML, HTML tags or parenthesis etc. First download latest surround plugins. Then unzip it into /usr/share/vim/vimfiles, surround.vim file should be in /usr/share/vim/vimfiles/plugin directory now. Install it's done.


Surround.vim is all about "surroundings": parentheses, brackets, quotes, XML tags, and more. The plugin provides mappings to easily delete, change and add such surroundings in pairs.

It's easiest to explain with examples. Press cs"' inside

   "Hello world!"

to change it to

   'Hello world!'

Now press cs' to change it to

   <q>Hello world!

To go full circle, press cst" to get

   "Hello world!"

To remove the delimiters entirely, press ds".

   Hello world!

Now with the cursor on "Hello", press ysiw] (iw is a text object).

   [Hello] world!

Let's make that braces and add some space (use } instead of { for no space): cs]{

   { Hello } world!

Now wrap the entire line in parentheses with yssb or yss)

   ({ Hello } world!)

Revert to the original text: ds{ds)

   Hello world!

Emphasize hello: ysiw<em>

   <em>Hello</em> world!

Finally, let's try out visual mode. Press a capital V (for linewise visual mode) followed by S<p class="important">.

   <p class="important">
     <em>Hello world!
   </p>

This plugin is very powerful for HTML and XML editing, a niche which currently seems underfilled in Vim land. (As opposed to HTML/XML ∗inserting*, for which many plugins are available). Adding, changing, and removing pairs of tags simultaneously is a breeze.

Vi config file hacks

Set options per file

For convenient editing, I add the following lines to the bottom of any DocBook files I'll be editing in Vi(m):

<!-- 
vim: softtabstop=2:shiftwidth=2:expandtab:textwidth=72 
-->

Block indentation

To support quick indentation of visually selected blocks keys can be bound to decrease and increase the indentation level. In the example .vimrc lines below the '<' key is bound to decrease indentation and the '>' key is bound to increase indentation.

vnoremap < <gv
vnoremap > >gv

Quick key tricks

Misc. tips