vim (Blog)http://www.turnkeylinux.org/blog/term/115/0
enVim smartopen plugin adds Vim support for CDPATHhttp://www.turnkeylinux.org/blog/vim-smartopen
<div class="section" id="overview">
<h2>
Overview</h2>
<p>
Ever since I discovered CDPATH last year I&#39;ve been thinking wouldn&#39;t it be great if Vim could access files using CDPATH without having to chdir anywhere first. In other words, why can&#39;t Vim understand that it has to look for tklbam/restore.py in the CDPATH instead of telling me that it doesn&#39;t exist in the current working directory?</p>
<p>
I started experimenting with Vim hooks and eventually figured out how to implement this as a generic mechanism that defines a more useful way for Vim to access the filesystem.</p>
<p>
Throw in a Python implemented autocomplete algorithm, and in a nutshell, thats how my <a href="https://github.com/lirazsiri/vim-smartopen">Vim smartopen</a> plugin works.</p>
</div>
<div class="section" id="configuration">
<h2>
Configuration</h2>
<p>
In your bashrc file:;</p>
<blockquote>
export _CDPATH=$CDPATH</blockquote>
</div>
<div class="section" id="path-lookup-algorithm">
<h2>
Path lookup algorithm</h2>
<ol class="arabic">
<li>
<p class="first">
Try to lookup path in cdpath</p>
</li>
<li>
<p class="first">
If no such file exists, try looking up path as a tag</p>
</li>
<li>
<p class="first">
If no such tag exists, assume its a new file</p>
<p>
Lookup the path of this new file by trying to find its parent directory in the cdpath. If it doesn&#39;t have a parent directory, assume the new file should be created in the current working directory.</p>
</li>
</ol>
<p>
Special cases:</p>
<pre class="literal-block">/path path is absolute (not looked up)
./path path is relative to the current working directory (not looked up)
</pre>
</div>
<div class="section" id="usage">
<h2>
Usage</h2>
<p>
commands:</p>
<pre class="literal-block">:O[pen] [ &lt;path&gt; ]
:Ta[bOpen] [ &lt;path&gt; ]
:Sp[litOpen] [ &lt;path&gt; ]
if no &lt;path&gt; argument provided:
defaults to taking &lt;path&gt; from word under cursor
</pre>
<p>
Note: shell-style autocomplete is supported, but only for filesystem paths, not tags.</p>
<p>
key bindings:</p>
<pre class="literal-block">gf open file or tag (under cursor)
CTRL-] open file or tag (under cursor)
&lt;C-W&gt;f open file (under cursor) in split window
&lt;C-T&gt; go back
</pre>
<p>
Note: overloaded vim native keybindings, with new enhanced functionality.</p>
<p>
mouse bindings (browser-inspired):</p>
<pre class="literal-block">left double click to open link
doubleclick open
shift-doubleclick split open
ctrl-doubleclick tab open (like in a browser)
ctrl-rightclick go back
</pre>
</div>
http://www.turnkeylinux.org/blog/vim-smartopen#commentshackvimMon, 17 Nov 2014 05:10:01 +0000Liraz Siri2845 at http://www.turnkeylinux.orgIntroduction to Vimhttp://www.turnkeylinux.org/blog/vim-tutorial
<div class="contents topic" id="table-of-contents">
<p class="topic-title first">Table of contents</p>
<ul class="simple">
<li><a class="reference internal" href="#background" id="id1">Background</a></li>
<li><a class="reference internal" href="#getting-started" id="id2">Getting started</a></li>
<li><a class="reference internal" href="#random-tips" id="id3">Random tips</a></li>
<li><a class="reference internal" href="#vim-flavors" id="id4">Vim flavors</a></li>
<li><a class="reference internal" href="#getting-help" id="id5">Getting help</a></li>
<li><a class="reference internal" href="#motion-commands" id="id6">Motion commands</a><ul>
<li><a class="reference internal" href="#amplifying-motion-commands" id="id7">Amplifying motion commands</a></li>
</ul>
</li>
<li><a class="reference internal" href="#scrolling-commands" id="id8">Scrolling commands</a></li>
<li><a class="reference internal" href="#making-changes" id="id9">Making changes</a><ul>
<li><a class="reference internal" href="#replace-mode" id="id10">Replace mode</a></li>
<li><a class="reference internal" href="#insert-mode" id="id11">Insert mode</a></li>
<li><a class="reference internal" href="#deleting" id="id12">Deleting</a></li>
<li><a class="reference internal" href="#delete-insert" id="id13">Delete + Insert</a></li>
<li><a class="reference internal" href="#cut-and-pasting" id="id14">Cut and Pasting</a></li>
<li><a class="reference internal" href="#indenting" id="id15">Indenting</a></li>
<li><a class="reference internal" href="#format-text" id="id16">Format text</a></li>
<li><a class="reference internal" href="#repeating-changes" id="id17">Repeating changes</a><ul>
<li><a class="reference internal" href="#recording-sequences-of-changes" id="id18">Recording sequences of changes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#global-changes" id="id19">Global changes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#visual-mode" id="id20">Visual mode</a><ul>
<li><a class="reference internal" href="#visual-block-mode" id="id21">Visual block mode</a></li>
</ul>
</li>
<li><a class="reference internal" href="#saving-and-exiting" id="id22">Saving and exiting</a></li>
<li><a class="reference internal" href="#window-management" id="id23">Window management</a></li>
<li><a class="reference internal" href="#tabs" id="id24">Tabs</a></li>
<li><a class="reference internal" href="#folding" id="id25">Folding</a></li>
<li><a class="reference internal" href="#file-explorer" id="id26">File explorer</a></li>
<li><a class="reference internal" href="#spell-checker" id="id27">Spell checker</a></li>
<li><a class="reference internal" href="#taglist-extension" id="id28">Taglist extension</a></li>
<li><a class="reference internal" href="#auto-completion" id="id29">Auto-completion</a><ul>
<li><a class="reference internal" href="#omni-completion" id="id30">Omni-completion</a></li>
</ul>
</li>
<li><a class="reference internal" href="#integrated-search" id="id31">Integrated search</a></li>
<li><a class="reference internal" href="#hidden-buffers" id="id32">Hidden buffers</a></li>
<li><a class="reference internal" href="#configuring-vim" id="id33">Configuring Vim</a></li>
<li><a class="reference internal" href="#extending-vim" id="id34">Extending Vim</a></li>
<li><a class="reference internal" href="#file-types" id="id35">File types</a></li>
<li><a class="reference internal" href="#modelines" id="id36">Modelines</a></li>
<li><a class="reference internal" href="#misc-neat-tricks" id="id37">Misc neat tricks</a></li>
</ul>
</div>
<div class="section" id="background">
<h2><a class="toc-backref" href="#id1">Background</a></h2>
<p>There are tons of tutorials and mountains of excellent documentation for
Vim. There is also quite a bit of outdated mediocre material one could
easily waste a lot of time on (like I have). Instead of reinventing the
wheel and repeating all of it I'll just do the sensible thing and
increase the signal to noise ratio by pointing you to the good stuff and
giving you my perspective on what I think is most important for you to
learn and understand. Hopefully this will speed things a long. The point
isn't to try and learn <em>everything</em> about Vim, just whatever 10% of the
features will be most useful to you. The problem is that 10% is
different for each type of user and if you don't have a guide you won't
necessarily know what features even exist.</p>
<p>Vim stands for Vi iMproved. Its basically an improved and extended
version of Vi, the standard Unix editor. Vi itself is a bit weird and
unintuitive at first, since it is a modal editor and most people are
used to modeless editing nowadays. However after you <em>get</em> Vi, you'll
understand why it was a natural choice for the standard Unix editor. Its
design and user interface mirrors Unix philosophy perfectly: combining
small and work simple well defined tools in flexible ways to achieve
powerful results.</p>
<p>The modality of Vim that can cause so much initial confusion is also its
source of power. You have to pay a price for that power because you
can't just sit down with a modal text editor for the first time and get
things done. If you're not in &quot;easy mode&quot; (evim or vim -y), and you
don't anything about Vi or modality, Vim will not do what you expect and
that will scare most people away, which is a shame, because modality is
a great idea if you are a power user that does a lot of text editing,
but to gain the benefits you have to be willing to invest in acquiring a
new skill.</p>
<p>In a nutshell a modal text editor turns your entire keyboard into an
insanely feature rich gamepad for sifting through, transforming and
inputting text.</p>
<p>For example, in most modeless editors you'll move around with the cursor
keys and maybe a page up, page down or the mouse. Vim users call this a
hunt and peck motion. With Vim there is a much more powerful set of
motion commands, and these motion commands can be combined with other
commands to manipulate and transform text.</p>
<p>The keybindings for all of these commands seem somewhat arbitrary at
first but as you start using them you'll find that they do in fact make
sense from an ergonomics point of view, since your hands tend to stay in
the same place and don't have to shift position for the cursor keys, or
the mouse and so forth, or get twisted into uncomfortable positions like
they do in Emacs and eventually develop Repetitive Strain Injury. I've
read reports that for laptop users with cramped keyboard this can be an
especially significant advantage.</p>
</div>
<div class="section" id="getting-started">
<h2><a class="toc-backref" href="#id2">Getting started</a></h2>
<p>After you install the vim-python package, start the 30 minutes
interactive vimtutor program. This will give you an understanding of how
to perform basic text editing tasks.</p>
<p>Of course basic text editing is just the tip of the iceberg, but in
order to develop more advanced skills you'll need to be properly
motivated, so here are few links to wet your appetite:</p>
<blockquote>
<p><a class="reference external" href="http://www.viemu.com/a-why-vi-vim.html">http://www.viemu.com/a-why-vi-vim.html</a></p>
<p><a class="reference external" href="http://www.moolenaar.net/habits.html">http://www.moolenaar.net/habits.html</a></p>
</blockquote>
<p>You'll want to read the manual, at least the Getting Started section:</p>
<pre class="literal-block">
|usr_01.txt| About the manuals
|usr_02.txt| The first steps in Vim
|usr_03.txt| Moving around
|usr_04.txt| Making small changes
|usr_05.txt| Set your settings
|usr_06.txt| Using syntax highlighting
|usr_07.txt| Editing more than one file
|usr_08.txt| Splitting windows
|usr_09.txt| Using the GUI
|usr_10.txt| Making big changes
|usr_11.txt| Recovering from a crash
|usr_12.txt| Clever tricks
</pre>
<p>After that my notes should provide a decent reference for the most
important commands and features and with a little practice and the
occasional query into the documentation you should be set to go.
Naturally I may miss some things so if you have any questions after that
you can send them my way. Don't try to do it all at once. Just switch to
Vim, start using it and build up your skills by experience.</p>
<p>My experience was that the documentation was easy to skim/search through
for the important/interesting stuff once I had a good overview
understanding.</p>
</div>
<div class="section" id="random-tips">
<h2><a class="toc-backref" href="#id3">Random tips</a></h2>
<ul class="simple">
<li>Vim can open compressed files</li>
<li>Vim can open/save files directly from the network (multiple protocols)</li>
</ul>
</div>
<div class="section" id="vim-flavors">
<h2><a class="toc-backref" href="#id4">Vim flavors</a></h2>
<ul>
<li><p class="first">vim: console version</p>
</li>
<li><p class="first">gvim: GUI version</p>
<blockquote>
<p>By default, forks into the background. If you want the process to
stick around (e.g., needed when configured as an external editor)
you'll need to use the --nofork option.</p>
</blockquote>
</li>
<li><p class="first">evim: easy version (behaves like a modeless editor)</p>
</li>
</ul>
<p>For further information, see the vim(1) man page. Note that you don't
have to install these separately. They are simply different front-ends
to the same software and are packaged together.</p>
</div>
<div class="section" id="getting-help">
<h2><a class="toc-backref" href="#id5">Getting help</a></h2>
<p>:h</p>
<p>Most commands in Vi have abbreviated forms for convenience. If you want to
know what :h is short for:</p>
<pre class="literal-block">
:h :h
</pre>
<p>And it turns out that :h is short for :h[elp]</p>
<p>:help isn't the only command that starts with :h but its the author figured
would be most commonly called so it got a really nice abbreviation.</p>
<p>What other commands start with :h?</p>
<blockquote>
:h :h&lt;CTRL-D&gt;</blockquote>
<p>Gives you a nice list, like in the bash shell, but unlike the shell this
form
of listing (by default) is more like grepping through all available
topics that contain your search term rather than topics that <em>start</em>
with your
search term.</p>
<p>So what help topics have the string help in them?</p>
<blockquote>
:h help&lt;CTRL-D&gt;</blockquote>
<p>You'll notice different topics have different forms. This makes their type
easy to identify:</p>
<pre class="literal-block">
'topic' # a Vim configuration option
-topic # a Vim command line flag
:topic # a Vim command
topic() # a Vim function
&lt;Topic&gt; # a Vim key binding
i_topic # an insert mode key binding
v_topic # a visual mode key binding
topic.txt # a help file
</pre>
<p>Of course you may not find what you're looking for if you don't know what it
is and your just guessing according to keywords, so searching through
the help
can be extremely useful:</p>
<p>:h helpgrep gives you a description of the helpgrep command</p>
<p>You'll notice that it tells you that you can navigate through matches
with the
quickfix commands, and that quickfix is highlighted with a different color.
Thats a tag. If you move the cursor to that tag and press CTRL-] it will
take
you to that topic. A double mouse click will also work, but isn't nearly as
fun once you know how to move the cursor to any point in the document
quickly
and without taking your hands off the keyboard. A CTRL-T will take you
back to
the page where you came from.</p>
<p>Since the documentation can be at times very verbose, I'll spare you the
suspense. To pop up a window with the results of your :helpgrep search do:</p>
<pre class="literal-block">
:copen
</pre>
<p>You can start out by navigating between windows with the mouse until
you learn how to do it the proper way:</p>
<pre class="literal-block">
:h windows
</pre>
<p>When you find what you're looking for, close the search buffer with:</p>
<pre class="literal-block">
:cclose
</pre>
<p>You may find it useful to search through the quickref page listing the most
commonly used commands:</p>
<pre class="literal-block">
:h quickref
</pre>
</div>
<div class="section" id="motion-commands">
<h2><a class="toc-backref" href="#id6">Motion commands</a></h2>
<p>:h motion.txt</p>
<p>The best way to understand what these motion commands do is to try and
navigate a document with them.</p>
<p>The usual motion commands that work in any editor:</p>
<pre class="literal-block">
the cursor keys: left down up right
PageUp
PageDown
</pre>
<p>I find that often the easiest way to jump straight to a certain position
in a file is by searching for a keyword. In the configuration I use I've
turned on incremental search so the cursor shifts position as you are
typing the search term. To make the current position stick, complete the
search by pressing &lt;Enter&gt;.</p>
<p>If you jump to the wrong position CTRL-O will bring you back to where you
started. CTRL-I will take you forward again.</p>
<p>search commands:</p>
<pre class="literal-block">
/keyword&lt;Enter&gt; search forward
?keyword&lt;Enter&gt; search backwards
* search forward for the word under the cursor
n next match
N previous match
</pre>
<p>hunt and peck commands:</p>
<pre class="literal-block">
h j k l: back down up forward
gj down one screen line (not real line)
gk up one screen line
</pre>
<p>Its bit weird at first, but you get used to it quickly and its very easy on
your hands and fingers.</p>
<p>to a specific letter:</p>
<pre class="literal-block">
f&lt;key&gt; forward to &lt;key&gt;
F&lt;key&gt; back to &lt;key&gt;
t&lt;key&gt; forward one character before &lt;key&gt;
T&lt;key&gt; back one character after &lt;key&gt;
</pre>
<p>by word:</p>
<pre class="literal-block">
w forward
W forward including punctuation characters
e forward to end of word
E forward to end of word including punctuation characters
b back
B back including punctuation characters
</pre>
<p>by sentence:</p>
<pre class="literal-block">
) forward
( back
</pre>
<p>by paragraph:</p>
<pre class="literal-block">
} forward
{ back
</pre>
<p>by line position:</p>
<pre class="literal-block">
0 start of line
^ start of line not counting whitespace
$ end of line
</pre>
<p>to a specific line:</p>
<pre class="literal-block">
gg start of file
G end of file
&lt;N&gt;G specific line
e.g., 10G # jump to line 10
</pre>
<p>by screen position:</p>
<pre class="literal-block">
H top
M middle
L last
</pre>
<p>by mark:</p>
<pre class="literal-block">
:h mark-motions
m&lt;letter&gt; sets a mark
`&lt;letter&gt; jumps back to that mark
`. jump to last line edited (very useful)
</pre>
<p>by parenthesis:</p>
<pre class="literal-block">
% find next ([{}]) and jump to its match
</pre>
<div class="section" id="amplifying-motion-commands">
<h3><a class="toc-backref" href="#id7">Amplifying motion commands</a></h3>
<p>Note that you can prefix any motion command with a number to multiply its
effect, for example:</p>
<pre class="literal-block">
2) # move 2 sentences forward
5w # move 5 words forward
</pre>
</div>
</div>
<div class="section" id="scrolling-commands">
<h2><a class="toc-backref" href="#id8">Scrolling commands</a></h2>
<p>:h scrolling</p>
<p>scroll:</p>
<pre class="literal-block">
CTRL-E scroll one line down
CTRL-Y scroll one line up
CTRL-U scroll up
CTRL-D scroll down
</pre>
<p>redraw current line so that its:</p>
<pre class="literal-block">
zt at the top of the screen
zz at the middle of the screen
zb at the bottom of the screen
</pre>
</div>
<div class="section" id="making-changes">
<h2><a class="toc-backref" href="#id9">Making changes</a></h2>
<p>Vim has several basic operations for making changes. Inserting text,
deleting text, cut and pasting text, indenting / unindenting text, etc.</p>
<p>A special command is '.' which repeats the last change whatever it was.
This doesn't seem very interesting at first glance but it can be quite
useful when you are making repetitive changes such as search and
replace, or transforming text, etc.</p>
<p>Tiny changes:</p>
<pre class="literal-block">
x delete one character
r&lt;key&gt; replace one character
~ toggle lowercase/uppercase of letter under cursor
</pre>
<div class="section" id="replace-mode">
<h3><a class="toc-backref" href="#id10">Replace mode</a></h3>
<p>Usually you'll want to use insert mode. Use replace mode if you really
want to overwrite the current text instead.</p>
<p>Entering replace mode:</p>
<pre class="literal-block">
R
</pre>
</div>
<div class="section" id="insert-mode">
<h3><a class="toc-backref" href="#id11">Insert mode</a></h3>
<p>You're not supposed to stay in insert mode. You move the cursor around
in normal mode. You enter insert mode insert text and then exit it with
&lt;Escape&gt; or &lt;CTRL-C&gt;. I prefer CTRL-C because I don't have to shift the
position of my hands.</p>
<p>Entering insert mode:</p>
<pre class="literal-block">
i current cursor
a after cursor
o insert new line below cursor
O insert new line above cursor
I insert at beginning of line
A insert at end of line
</pre>
<p>Useful key bindings in insert mode:</p>
<pre class="literal-block">
CTRL-W delete word
CTRL-U delete entire line
</pre>
<p>Remember that you can/should combine motion commands with insert commands.</p>
<p>For example:</p>
<pre class="literal-block">
ea move to end of word and append to it
</pre>
</div>
<div class="section" id="deleting">
<h3><a class="toc-backref" href="#id12">Deleting</a></h3>
<p>Commands:</p>
<pre class="literal-block">
dd delete current line
D delete until end of line
d + {motion} delete from current cursor to where motion would take us
</pre>
<p>examples:</p>
<pre class="literal-block">
dw delete next word
d2w delete next two words
d2b delete previous two words
d} delete next paragraph
d{ delete previous paragraph
d) delete until end of sentence
dG delete until end of file
</pre>
</div>
<div class="section" id="delete-insert">
<h3><a class="toc-backref" href="#id13">Delete + Insert</a></h3>
<p>Change is usually more useful than delete because it does two things,
and creates a useful repeatable operation.</p>
<p>Commands:</p>
<pre class="literal-block">
c + {motion}
C Change till the end of line
S Substitute entire line
</pre>
</div>
<div class="section" id="cut-and-pasting">
<h3><a class="toc-backref" href="#id14">Cut and Pasting</a></h3>
<p>:h usr_04.txt</p>
<p>yanking and deleting copy/move text into registers. See :registers for a
list and to examine their contents.</p>
<p>To move content out of the registers you use paste commands in normal mode:
p paste after current line
P paste before current line</p>
<p>You can also paste text without exiting insert mode:</p>
<pre class="literal-block">
:h i_CTRL-R
</pre>
</div>
<div class="section" id="indenting">
<h3><a class="toc-backref" href="#id15">Indenting</a></h3>
<p>How indenting works depends on Vim's particular configuration. I've
configured it to indent 4 spaces when you press &lt;Tab&gt; and unindent by 4
spaces when you press &lt;Backspace&gt; (if 4 continuous spaces exist)</p>
<p>Also I've turned auto-indent on so that Vim starts the next line with
the same indent as the current line.</p>
<p>Normal mode commands:</p>
<pre class="literal-block">
&gt;&gt; indent this line
&lt;&lt; un-indent
&gt; + {motion}
&lt; + {motion}
</pre>
<p>For example:</p>
<pre class="literal-block">
&gt;} indent paragraph
&gt;G indent to end of file
&gt;/^\S indent to first unindented line
</pre>
<p>In insert mode:</p>
<pre class="literal-block">
CTRL-T indent current line
CTRL-D unindent current line
</pre>
<p>How to insert a real tab (useful for Makefiles):</p>
<pre class="literal-block">
CTRL-V&lt;Tab&gt;
</pre>
</div>
<div class="section" id="format-text">
<h3><a class="toc-backref" href="#id16">Format text</a></h3>
<p>Commands:</p>
<pre class="literal-block">
J join lines
gq format lines
</pre>
</div>
<div class="section" id="repeating-changes">
<h3><a class="toc-backref" href="#id17">Repeating changes</a></h3>
<p>The '.' command is one of the most powerful. It repeats the last
previous change.</p>
<p>search and replace example:</p>
<pre class="literal-block">
/foo&lt;Enter&gt;
cbar&lt;CTRL-C&gt;
n # next foo match
. # repeat replace command if you want to replace this match
</pre>
<p>Note: at least in some cases you may find it easier to use the
:substitute command instead, but the above example gives you more
control in the sense that you can choose which matches to replace
interactively.</p>
<div class="section" id="recording-sequences-of-changes">
<h4><a class="toc-backref" href="#id18">Recording sequences of changes</a></h4>
<p>:h recording
:h usr_10.txt</p>
<p>Recording:</p>
<pre class="literal-block">
q&lt;key&gt;
bunch of changes
q
</pre>
<p>Playback:</p>
<pre class="literal-block">
&#64;&lt;key&gt;
</pre>
</div>
</div>
<div class="section" id="global-changes">
<h3><a class="toc-backref" href="#id19">Global changes</a></h3>
<p>:h :global</p>
<p>Extremely powerful command, that applies arbitrary commands to any line
that matches a regular expression.</p>
<p>For example, the following deletes all lines with 2007 in them:</p>
<pre class="literal-block">
:g/2007/normal dd
</pre>
</div>
</div>
<div class="section" id="visual-mode">
<h2><a class="toc-backref" href="#id20">Visual mode</a></h2>
<p>:h visual-mode</p>
<p>Entering visual mode:</p>
<pre class="literal-block">
V # line visual mode (select whole lines)
v # select arbitrary positions on screen
gv # reselect the previous Visual area
</pre>
<p>Inside visual mode use motions commands to select text</p>
<p>Commands in visual mode:</p>
<pre class="literal-block">
d # delete
y # yank (I.e., copy)
&gt; # indent
&lt; # unindent
J # join lines and paragraphs
gq # format selected
</pre>
<div class="section" id="visual-block-mode">
<h3><a class="toc-backref" href="#id21">Visual block mode</a></h3>
<p>Note that there is also a strange but quite useful visual block mode
that can be very useful but is a bit difficult to explain. You need to
experiment and see the effect for yourself.</p>
<p>For further details see:</p>
<pre class="literal-block">
:h blockwise-operators
:h blockwise-examples
</pre>
<p>Gotcha: blockwise Insert and Append won't work if you escape with
CTRL-C. You need to escape with the &lt;Esc&gt; key.</p>
</div>
</div>
<div class="section" id="saving-and-exiting">
<h2><a class="toc-backref" href="#id22">Saving and exiting</a></h2>
<p>:h save-file</p>
<p>Commands:</p>
<pre class="literal-block">
ZZ # save and exit
ZQ # exit without saving
CTRL-S # custom key binding I added to save file
</pre>
</div>
<div class="section" id="window-management">
<h2><a class="toc-backref" href="#id23">Window management</a></h2>
<p>:h windows</p>
<p>Like Emacs and screen, Vim support window splitting. You can split
windows horizontally, vertically, resize windows, minimize them,
maximize them, etc.</p>
<p>Even if you don't split your windows explicitly many commands (e.g.,
:help), split the window for you so you have to know how to control
them.</p>
<p>To begin with, if you don't know or don't remember the key bindings, you
can use the GUI &quot;Window&quot; menu. In GUI mode you can also resize the
windows with the mouse by dragging their borders.</p>
<p>Commands:</p>
<pre class="literal-block">
:sp split current window
:sp path/to/file split window by opening file
</pre>
<p>Key bindings:</p>
<pre class="literal-block">
CTRL-W S split current window
CTRL-W o keep only this window open (close all others)
CTRL-W c close this window
CTRL-W _ maximize this window
CTRL-W = make all windows equal in length
CTRL-W w toggle between windows
CTRL-W p switch to the previous window
CTRL-W t switch to top most window
CTRL-W b switch to the bottom window
CTRL-W hjkl move to the window in the desired direction
CTRL-W HJKL move window to the desired location
e.g.,
CTRL-W J will shift the position of a window to the
bottom
</pre>
</div>
<div class="section" id="tabs">
<h2><a class="toc-backref" href="#id24">Tabs</a></h2>
<p>:h tabpage.txt</p>
<p>Tab commands:</p>
<pre class="literal-block">
:tabnew
:tabnew +Ex
:tabnew path/to/file
gt next tab (also CTRL-&lt;PageDown&gt;)
gT previous tab (also CTRL-&lt;PageUp)
</pre>
</div>
<div class="section" id="folding">
<h2><a class="toc-backref" href="#id25">Folding</a></h2>
<p>:h folding</p>
<p>:h 'foldmethod'</p>
<ul class="simple">
<li>folds can be manually created or automatically created or a
combination of both.</li>
<li>if you create folds manually, they will be lost when you abandon the
file unless you save them explicitly with the :mkview command</li>
<li>folds can be nested (recursively)</li>
</ul>
<p>manually creating a fold:</p>
<pre class="literal-block">
zf + {motion}
e.g.,
zf} fold next paragraph
in visual mode: zf
</pre>
<p>mnemonic: z looks like a folded piece of paper (from the side)</p>
<p>Commands:</p>
<pre class="literal-block">
zo # open fold
zc # close fold
zr # open one level of folds (globally)
zR # open all folds
zM # close all folds
</pre>
</div>
<div class="section" id="file-explorer">
<h2><a class="toc-backref" href="#id26">File explorer</a></h2>
<pre class="literal-block">
:h Explore
:h pwd
:h cd
</pre>
<p>I find the file explorer to be a very useful and efficient way to open
files.</p>
<p>Opening a file explorer window:</p>
<pre class="literal-block">
gvim path/to/dir
:e path/to/dir
:Ex [ path/to/dir ]
Without arguments, opens a file explorer in the same directory as the
currently open file.
</pre>
<p>Explorer-mode commands:</p>
<pre class="literal-block">
F1 for commands
c chdirs to current open directory
&lt;Enter&gt; open in current window
o open in new split window
t open in new tab
</pre>
</div>
<div class="section" id="spell-checker">
<h2><a class="toc-backref" href="#id27">Spell checker</a></h2>
<p>:h spell</p>
<p>Underlines mis-spelled words (like Thunderbird).</p>
<p>:set spell</p>
<p>Commands:</p>
<pre class="literal-block">
z=
]s move to next mis-spelled word
[s move to previous mis-spelled word
</pre>
</div>
<div class="section" id="taglist-extension">
<h2><a class="toc-backref" href="#id28">Taglist extension</a></h2>
<p>:h taglist.txt</p>
<p>Taglist is the most popular Vim extension. It depends on exuberant ctags
package to scan source code (many languages are supported) for <em>tags</em>.
What a <em>tag</em> is depends on the language being scanned. For example, in
Python a tag is a class or function definition. In vimscript, this
includes variable assignments.</p>
<p>Taglist uses ctags to generate a side-pane that lists all the tags in
all buffers by default. This is a very convenient way to quickly jump
between software structures in the same file or across multiple files.</p>
<p>Commands:</p>
<pre class="literal-block">
F8 # toggle side-pane
</pre>
<p>Taglist window commands:</p>
<pre class="literal-block">
&lt;CR&gt; # jump to tag location
o # jump to tag in new window
t # jump to tag in new tab
# (if tab already exists, jump to existing tab or window)
u # update tags
+ # open fold
- # close fold
* # open all folds
= # close all folds
</pre>
</div>
<div class="section" id="auto-completion">
<h2><a class="toc-backref" href="#id29">Auto-completion</a></h2>
<p>:h popupmenu-completion</p>
<p>Commands:</p>
<pre class="literal-block">
keywo&lt;CTRL-P&gt;
in completion popup
CTRL-P up one value
CTRL-N down one value
CTRL-E cancel auto-completion
CTRL-H remove character from completion (find more matches)
CTRL-L add character to completion (find less matches)
</pre>
<div class="section" id="omni-completion">
<h3><a class="toc-backref" href="#id30">Omni-completion</a></h3>
<p>:h new-omni-completion</p>
<p>A special form of auto-completion that understands context (e.g., Python
context)</p>
<p>Usage example:</p>
<pre class="literal-block">
s = &quot;&quot;
s.&lt;CTRL-X&gt;&lt;CTRL-O&gt;
CTRL-W z # Close preview pane
</pre>
</div>
</div>
<div class="section" id="integrated-search">
<h2><a class="toc-backref" href="#id31">Integrated search</a></h2>
<p>Various Vim built-in commands and extensions use Vim's capability to store
file locations.</p>
<p>:h vimgrep
:h grep
:h helpgrep</p>
<p>Usage examples:</p>
<pre class="literal-block">
:helpgrep grep # search help for grep
:vimgrep foo **/*.py # search the cwd recursively for all python foo
:grep foo % # search the current file for foo
</pre>
<p>Vim has two mechanisms for storing lists of file locations, the error
list and the local list. The local list is local to a specific window,
while the error list is global.</p>
<p>For more than you ever wanted to know about navigating results local
lists and error lists check out :h quickfix</p>
<p>If you just want to open a window with the results:</p>
<pre class="literal-block">
:copen # open error list
:cclose # close error list
:lopen # open local list
:cclose # close local list
</pre>
<p>Note that to put search results into local lists you'll need to use the
local version of that command:</p>
<pre class="literal-block">
:lvimgrep
:lgrep
:lhelpgrep
</pre>
<p>Cheatsheet:</p>
<pre class="literal-block">
:cn[ext]
jump to the next error
:cp[revious]
display previous error
:colder
switch to older error list (Vim keeps a history)
:cnewer
switch to a newer error list
</pre>
<p>Note that all commands have a local version and a global version. For
example, to jump to the next error in the global list you'll use :cnext,
and to jump to the next error in the local list you'll use :lnext.</p>
</div>
<div class="section" id="hidden-buffers">
<h2><a class="toc-backref" href="#id32">Hidden buffers</a></h2>
<p>:h buffer-hidden</p>
<p>If you open a file Vim keeps it in memory even if you subsequently move
on to editing another file. This is called the hidden buffers list. You
don't have to have the file open in a window or a tab. This is useful
for moving between a history of last locations without having to keep
all of these files open simultaneously in a bunch of windows or tabs.</p>
<p>Another way to populate your buffers is to specify multiple files on the
command line.</p>
<p>You can move between buffers with the GUI menu or with keyboard commands:</p>
<pre class="literal-block">
:ls # list buffers (not files)
:b[uffer] N # jump to buffer N
</pre>
</div>
<div class="section" id="configuring-vim">
<h2><a class="toc-backref" href="#id33">Configuring Vim</a></h2>
<p>:h set</p>
<p>Vim has too many configuration options to count. Most of these have
reasonable defaults so you don't need to tweak them, but if you're
interested in seeing whats configuration try:</p>
<pre class="literal-block">
:options
</pre>
<p>Take a look, consult the docs, and experiment with different settings to
understand how they work.</p>
<p>Commands:</p>
<pre class="literal-block">
:set {option}? # show value of {option}
:set {option} # switch option on
:set no{option} # switch option off
:set inv{option} # toggle option
</pre>
<p>Note that most options have short versions so if you are configuring
your Vim session interactively to suit a particular file/session you can
do that without too much trouble.</p>
<p>For example:</p>
<pre class="literal-block">
:set number # switches on line numbering in the current buffer
</pre>
<p>Is equivalent to:</p>
<pre class="literal-block">
:set nu
</pre>
<p>If you want to find what the short version of an option is, simply query
the help system:</p>
<pre class="literal-block">
:h 'number'
</pre>
</div>
<div class="section" id="extending-vim">
<h2><a class="toc-backref" href="#id34">Extending Vim</a></h2>
<p>:h usr_41.txt</p>
<p>:h if_pyth.txt</p>
<p>Vim, especially the most recent versions (7+) is highly extensible. Its
native Vimscript is basically a real programming language you can do
pretty much everything in, but its not nearly as powerful or easy to
develop in as Python. For a specialized extension languages, its not
too bad, but it kinds of a kludge when compared to a <em>real</em> programming
language like Python (e.g., it doesn't support OOP).</p>
<p>Thankfully Vim can be embedded with Python as the extension language
such that you can do most of the heavy lifting in native Python with
only minor references to Vim API's itself in specialized glue code that
interfaces your code with Vim's vimscript internals. This way you can
call on the full power of Python's standard library, object oriented
programming and interactive testing and development facilities for most
of the development cycle and you only have to deal with the less elegant
Vim parts in the glue code.</p>
<p>That doesn't mean you can ignore vimscript completely, but you don't
have to deal with it much if you're careful about making your Python
code logically independent.</p>
</div>
<div class="section" id="file-types">
<h2><a class="toc-backref" href="#id35">File types</a></h2>
<p>:h filetype</p>
<p>Vim doesn't have modes in the Emacs sense, but it does have a concept of
a filetype that is recognized either by suffix (e.g., *.py), content,
or location, and Vim can be configured to run special code when loading
a particular type of file.</p>
<p>This special code can provide syntax highlighting, additional key
bindings, change the behavior of indenting, etc.</p>
</div>
<div class="section" id="modelines">
<h2><a class="toc-backref" href="#id36">Modelines</a></h2>
<p>:h modeline</p>
<p>You can configure vim options on a per-file basis using a modeline.
Thats a funny string at the start or the end of a document. You can use
this to configure special indenting options, syntax highlighting, etc.</p>
<p>Example:</p>
<pre class="literal-block">
vim:noai:sw=3 ts=6
</pre>
<p>This sets turns off autoindent, sets shiftwidth to 3 and tabstop to 6.</p>
</div>
<div class="section" id="misc-neat-tricks">
<h2><a class="toc-backref" href="#id37">Misc neat tricks</a></h2>
<p>filtering text through a unix shell pipe:</p>
<pre class="literal-block">
visually select text (e.g., a list of numbers)
!sort -n
</pre>
<p>defining abbreviations:</p>
<pre class="literal-block">
:h abbreviations
</pre>
<p>text arithmetic (increment decrement numbers in place):</p>
<pre class="literal-block">
CTRL-A CTRL-X
</pre>
</div>
http://www.turnkeylinux.org/blog/vim-tutorial#commentstutorialvimTue, 15 Jul 2014 05:20:02 +0000Liraz Siri2849 at http://www.turnkeylinux.orgVim file exploration tipshttp://www.turnkeylinux.org/blog/vim-file-exploration
<h2>
Find filename patterns</h2>
<p>
Using Vim&#39;s built-in file explorer, it&#39;s possible to find specific filenames by pattern. For example:</p>
<pre>
# :Ex is short for :Explore
# search all subdirectories
:Ex */Makefile
:Ex */*.c
# search recursively
:Ex **/Makefile
:Ex **/*.c
:Ex **/*tpl*
</pre>
<p>
This will take you straight to the first match. If you want the next match after that:</p>
<pre>
&lt;Shift-DownArrow&gt;
</pre>
<p>
And back:</p>
<pre>
&lt;Shift-UpArrow&gt;
</pre>
<h2>
Bookmarking locations in the filesystem</h2>
<p>
When I use sshfs to mount a development VM&#39;s filesystem I sometimes find it tedious to type in the same long sub-directory paths over and over again. I&#39;ve been setting up symbolic links to make my life somewhat easier but that gets tedious quickly too.</p>
<p>
So I searched Vim&#39;s documentation a bit and it turns out that Vim&#39;s file explorer allows you to bookmark locations in the filesystem like this:</p>
<pre>
{cnt}mb # make bookmark
{cnt}gb # goto bookmark
q # list bookmarks
</pre>
<p>
For example:</p>
<pre>
1mb # set the first bookmark
1gb # go to the first bookmark
</pre>
<p>
Quite handy.</p>
http://www.turnkeylinux.org/blog/vim-file-exploration#commentsubuntuvimMon, 23 May 2011 06:06:07 +0000Liraz Siri1411 at http://www.turnkeylinux.org