Vim:Frequently Used Bits and Doodads

I have a friend (yes, you Zeke) who is considering changing from a graphical editor to a CLI editor, namely vim, for his web developement. His primary hesitation though is the learning curve for vim, and rightfully so. Coming from a GUI with menus to an environment you can’t even exit unless you know what you’re doing already is pretty intimidating (or fun if that’s what you’re into).

I understand this well. When I first started in vim, I was quite taken back by the tremendous amount of documentation. Lots of documentation [hopefully] means lots of functionality. Lots of documentation is great but if you don’t know where to start, it doesn’t do you alot of good. Here I’m hoping to narrow things down a bit for you folks thinking of learning vim.

Two Modes

Before we start anywhere else, you need to know that there are two modes for vim: command mode and insert mode. Since vim is a command line editor, it doesn’t [technically] support a mouse (it kind of does, but we won’t get into that) which means all of your controls are keyboard-based. However, if you also use the keyboard keys to write text, you’re going to have a lot of conflicts - hence two modes.

When you open a text file, you are first put in command mode. That means that your whole keyboard won’t insert text. Instead, it interprets all key presses as commands. The first command you’ll want is the letter i. This will put you into insert mode. Once in insert mode, all key presses will insert text. To get/escape out of insert mode, hit the escape key a la top left of yonder kybard.

Frequently Used Features

Find

Finding in vim is actually fairly simple. You just have to remember that it searches in two directions: forwards and backwards. To search fowards in thedocument from the present position of the cursor, type the following in command mode

/searchterm
-----

To search backwards in the document from the present position of the cursor,
type the following in command mode <pre>?searchterm</pre>


[[replace]]
=== Replace

This one is pretty complex unfortunately. However, for those of you who like to
weild the power of the gods, find and replace in vim uses
http://www.regular-expressions.info/[regex].

I won't get heavily into this because this topic is so broad and can take so
much time to learn because of how robust it can be. You can actually put this
piece on your resume and you'll likely get quite a few bites.

A basic find and replace would perform a replace on the entire document.  For
example, we want to replace the word _foo_ with __bar__. To do this, do the
following (actually type the : at the beginning of the expression):

:%s/foo/bar/

That will replace all instances of foo with bar from the beginning to the end
of the document (the % here means the whole file), unless there is more than
one instance of foo on a particular line. That will only replace the first
instance found on each line. To replace all for real this time, just append the
letter "g" for "global" (at least I think that's what it stands for).

:%s/foo/bar/g

[[code-folding]]
=== Code Folding

Depending on the distribution of Linux you are using, code folding may or may
not be enabled already. To enable it however, you need to type exactly the
following command in command mode: +:set foldmethod=indent+. You could also put
that in your +\~/.vimrc+ file and it will enable it for all future vim
sessions. Next up, here's how you open and close folded code...

**za/A**: Toggles code folding. Capital A toggles all folds inside of the
          currently selected fold.

**zc/C**: Closes the folded code. Capital C closes all folds inside of the
          currently selected fold.

**zo/O**: Opens the folded code. Capital O opens all folds inside of the
          currently selected fold.

**zr/R**: Opens the first level of folded code throughout the file. Capital R
          will open all folds, including nested.

**zm/M**: Closes the first level of folded code throughout the file. Capital M
          will close all folds, including nested.

I pretty much only every use zR (open all), zM (close all), and zA (toggle all
nested under currently selected).


[[syntax-highlighting]]
=== Syntax Highlighting

Depending on what distro of Linux you use, syntax highlighting may or may not
be enabled for you by default. If it isn't already, there are two ways to turn
'er on.

When in command mode, type exactly (with the preceeding colon) <code>:syntax
on</code>. That should enable syntax highlighting for you.  If it doesn't, it's
possible either the file you are editing doesn't have a known extension (eg:
.pl for Perl, .php for PHP, .cpp for C+\+, etc) or it doesn't start with a
http://en.wikipedia.org/wiki/Shebang_%28Unix%29[shebang] that indicates what
the language is (eg: #!/usr/bin/perl).


[[line-numbers]]
=== Line Numbers

I don't typically put this piece into my .vimrc file because I don't always
like to see line numbers, especially when I need to copy and paste code.
However, they do come in handy occasionally. For instance, when you're working
on a really big perl script and you want to know what line you are presently on
so when you close the file you can come back and hit ":742" and you're back
where you left off. To show or hide line numbers respectively, try the
following commands

.Turn on line numbers

:set nu

.Turn off line numbers

:set nonu

[[reading-in-the-output-of-a-command]]
=== Reading in the Output of a Command

This is super handy for me very often. When I'm editing a file and I need to
put the the output of a command (typically data that I need) into my document
for further parsing, this really saves the day. Without this, I'd have to close
vim (or open a new terminal), then run the command and redirect the output to
append to the end of my file. If I need it somewhere else in the file though, I
then have to reopen the file in vim and and move the data around. Here's how we
can read in output from a command. For this example, we'll use the output of
__ifconfig -a__. Put the cursor where you want the data to go and then in
command mode, type

:read !ifconfig -a

[[visual-mode]]
=== Visual Mode

This one is a real live saver. If any of you have used vi, you likely know the
wonders of this bit of functionality. For those of you who don't know, in old
vi, to make a change to more than one line, you had to perform the action and
tell vim to apply it to the current line and however many lines forwards. That
means that you have to count lines. If you're going to delete say, 87 lines,
that's a really big pain. With visual mode, we can highlight the lines we want
to modify (delete, shave some characters off the front, indent, unindent, etc)
and simply perform the action and it will be applied to all highlighted lines.

To do this, in command mode, hit <code>Shift+v</code> (or capital V for short).
Move the cursor up or down and you will see lines being highlighted. To delete
those lines, hit the _d_ key. To indent them, hit the _>_ key. To unindent, hit
the _<_ key. To copy/yank them, hit the _y_ key.

To escape visual mode without making changes, just hit the escape key.


[[an-awesome-cheatsheet]]
=== An Awesome Cheatsheet

http://tnerual.eriogerg.free.fr/vimqrc.html

Category:Linux Category:Unix Category:Vim Category:Editors


// vim: set syntax=asciidoc: