Archive for the 'vim' Category

Vim Cheatsheet

Monday, October 27th, 2008

Today’s post is not really a post. It is an attempt to create a workable Vim cheat-sheet. I know that there hundreds vi/vim chat-sheets online, some better than other but naturally none of them is complete and includes all the commands that I care about.

By far the most popular sheet is probably this one which actually hangs over my desk. It is a very good one and I use it all the time to remind myself certain key-strokes. Still, it is not very comprehensive since you just can’t physically fit all the useful key-strokes in that format. I decided to organize mine into sections rather typographical groupings. The result is below.

Please note that I skipped the obvious stuff like ‘hjkl’ and some of the advanced stuff like marking, folding, and register operations. That’s because I don’t really use these commands that often, and furthermore a lot of Vi emulators (like the one in Komodo Edit) do not support them.

Your mission, should you choose to accept it is to add your favorite commands and/or indispensable shortcuts which I have omitted. And yes, I omitted a lot here. There are just to many things to list - this is why I’m relying on you guys!

Movements

w - next word
W - next white space delimited word
b - previous word
B - previous space delimited word
e - end of word
E - end of white space delimited word
0 or ^ - to the physical beginning of the line
$ - to the physical end of the line (ie. the newline char)
g0 - to the beginning of the virtual screen line
gm - to the middle of virtual screen line
g$ - to the end of the virtual screen line
gk - up screen line
gj - down screen line
fchar - till the next occurrence of character ‘char’ (inclusive)
Fchar - till the previous occurrence of character ‘char’ (inclusive)
tchar - till the previous occurrence of character ‘char’ (exclusive)
Tchar - till the previous occurrence of character ‘char’ (exclusive)
G - end of file
gg - start of file
gd - go to the definition (first occurrence of the word under cursor)
{} - begging/end of paragraph
() - begging/end of sentence
[{ ]} - beginning/end of a code block
[[ ]] - beginning/end of a method
[* ]* - beginning/end of a comment block
% - find next open block char ({[ and jump to it’s matching close char
‘. - go to the line with the latest change
* - search to the next occurrence of the word under cursor
# - search to previous occurrence of the word under cursor
/pattern - search to next occurrence of “pattern”
?pattern - search to previous occurrence of “pattern”
n - repeat last search (*,#,/,?) forward
N - repeat last search (*,#,/,?) backward

In the examples below I indicate a movement specified above as mov

Entering Insertion Mode

i - insert at the cursor
I - insert at the beginning of the line
gI - insert in column 1 of the line
a - append after the cursor
A - append at the end of the line
o - open a blank line below cursor
O - open a blank line above cursor
r - replace character under cursor and exit insert mode
R - insert at the cursor in overstrike mode
cmov - delete (change) movement and enter insert mode
cc - replace entire line
C - delete (change) line and enter insert mode
s - delete character under cursor and enter insert mode
S - delete line under cursor and enter insert mode

Deleting

x - delete char under cursor
X - delete char before cursor
dmov - delete range of movement m
dd - delete current line (text moves up)
D - delete to the end of line
J - join the line below to the current
gJ - append the line below to the end of current line

Block Modifiers

When dealing with blocks of text delimited by parentheses (), brackets [], braces {} or <> you can apply the command to either the contents of the block or the whole thing by following it with:

iblk - inner block (everything inside braces, parenthesis etc..)
ablk - all (whole parenthesized block)

Where ‘blk’ is ), ], } or >

Copy/Paste

yy - copy current line
ymov - copy movement m
p - paste after cursor
P - paste before cursor

Visual Mode
v - enter visual mode highlighting 1 char at a time
V - enter visual mode highlighting 1 line at a time
Ctrl+V - enter visual block mode
aw - highlight word
as - highlight sentence
ap - highlight paragraph
ab - highlight a parenthesized block
aB - highlight a {} blocks
ib - highlight contents of a parenthesized block
iB - highlight contents of a {} block

Special

< > - indent left/right
. - repeat last command
u - undo last command
U - undo all changes on the current line
Ctrl+R - redo last undo
Ctrl+O - jump outwards (up-back) to the previous cursor position
Ctrl+I - jump inwards (down-forward) to the previous cursor position

Completion

Ctrl+X - enter completion mode
Ctrl+N - auto complete word in insert and completion mode
Ctrl+P - auto complete word in insert and completion mode
Ctrl+L - auto complete whole line in completion mode
Ctrl+K - dictionary completion in completion mode
Ctrl+F - filename completion in completion mode

Useful Tricks

g~mov - toggle uppercase/lowercase on the range of ‘mov’
gumov - lowercase the range of ‘mov’
guu - lowercase the current line
gUmov - uppercase the range of ‘mov’
gUU - upercase the whole line
g?mov - rot13 on the range of ‘mov’

Regexp Replace

:ranges/foo/bar/arg - replace foo with bar in ‘range’ with

Values of ‘range’:

% - whole file
number - that particular line
none - apply to current line only

Values of ‘arg’:

none - apply to first occurrence
g - global (all occurrences)
i - ignore case
I - don’t ignore case
c - confirm each substitution
p - print the last line containing substitution
e - ignore errors

Macros

qchar - start recording macro storing it in register ‘char’
q - end recording
@char - replay the macro stored in ‘char’

That’s all I have for now. I will update this page with any useful suggestions. If we have enough perhaps I will take this, re-format it nicely and generate some sort of nice looking single sheet PDF that you could hang in your cube or above your fireplace or whatnot.

Vim LaTex

Monday, October 20th, 2008

I noticed that in the past I produced several posts about combining LaTex and Emacs. While I do like Emacs and respect it’s formidable power I do not consider myself an Emacs user. Most of my day-to-day work is done in Vim which brings different kind of editing power to the table. Emacs is a lisp based framework for building text editing applications - fully programmable, easily customizable and almost endlessly extensible. The power of Emacs comes from what it can do. Vim’s power stems from what it does - and that is providing very powerful and yet simple modal text editing environment. I wanted to say intuitive, but yeah - that would be a lie. Vim is powerful because it allows you to accomplish just about anything in 3-4 keystrokes by chaining and combining simple commands.

Using Vim makes editing text much simpler and easier. It removes a lot of key-presses and movements. Let me give you an example - in a typical coding session you may want to copy the current line, and paste it below. Assuming your cursor is in the middle of the line how would you go about accomplishing it? Typically you would do:

  1. Hit Home to skip to the beginning of the line
  2. Hold shift and hit End to highlight the line
  3. Press Ctrl+C to copy the line
  4. Hit End again to put the cursor at the end of the line
  5. Hit Enter to insert a new line
  6. Press Ctrl+V to paste the line

How do you do it in Vim?

  1. Press the following keys in a sequence: yyp

That’s it. You want to paste the line 3 times? Do yy3p. This is the power you get when you use Vim. Of course die-hard emacs fanboys will remind me that one could always use Emacs’ VIP mode which emulates vi’s modal editing features. And they would be absolutely right. As I said, Emacs is a framework for building editors rather than an editor. So when you launch it in VIP mode you are really using Vi with the lisp powerhouse underneath it.

In theory you could combine the neat features of AUCTex on Emacs and the power the VIP mode gives you to get the best of both worlds. But if you don’t need these features - if you are a Vim purist or if you just need a simple modal text editor I have a nice alternative for you. It’s called VimLaTex.

vim-latex.gif

While it does not have the nice rendering features of AUCTex, VimLaTex is a must-have if you are planning to use Vim to edit LaTex files. The package itself is tiny - you just drop few hundred KB into your .vim directory and add few lines to your .vimrc.

The suite will add bunch of useful menus to your toolbar:

croppercapture100.jpg

These menus are your standard IDE like features - for example options for inserting packages, or snippets of code for special environments and etc.. A lot of the entries come with predefined key-bindings which make your life much easier. For example:

  1. To compile your file do: \ll
  2. To view compiled file do: \lv

You can set the preferred format for both options above by invoking :TTarget. It works like a charm both on Windows and Linux when I tried it.

VimLaTex also uses an interesting concept of placeholders. Whenever you use it to generate snippet of code, it will put funny looking character sequences inside of it. They may look silly until you realize how useful they are. For example let’s say you want to insert a figure:

\begin{figure}[h]
    \centerline{\psfig{figure=«eps file»}}
    \caption{«caption text»}
    \label{fig:«label»}
\end{figure}«»

The placeholders are there to make your life easier. When you press Ctrl+J vim will jump to the first «» delimited sequence and select it. This way you can just type over it. Press Ctrl+J again (while still in insert mode) and you will jump to the next one. How awesome is that?

If you are a Vim lover who also happens to use LaTex or a LaTex user who loves Vim definitely check this one out.

Vim Tips

Wednesday, October 1st, 2008

Hey kids! Guess what time it is? It’s time for some Vim Tips!

What? Don’t make that face! Vim is awesome! Don’t give me that “B-b-but mistur Luke, we don’t use vim!” That won’t work with me again! You know damn well you should be using it. What is your lame excuse today? Is it “vim is to hard”? Suck it up you babies. Real men and women use vi and that’s that! There is only one excuse that I’m willing to accept, and that excuse is “I use Emacs”. So, how many of you whining babies use emacs? One person? Ok, you are excused - you can go sit over there. Your exercise is to write an elisp macro to do something interesting.

The rest of you, STFU and listen. No, I don’t care you use Eclipse on a daily basis. Just get the viPlugin and you will have the combined power of Vi and Eclipse at your fingertips. How awesome is that. No, don’t shrug - it’s fucking awesome, and you are gonna like it, right now! There, that’s better.

Ok, question from the back row. Yes, you - the guy with the stupid face. You use word? You know what - get the fuck out! Just get out and never come back. Actually, on the second thought wait. Sit your ass down and try the ViEmu thing. I tested it a while ago and it was pretty cool. Unfortunately it tad expensive and I could not justify purchasing it since I only use Word when I’m forced to. Still, it might work for you.

Anyways, this might be the first post of a series. I’m going to use it to drop little bits of vim lore that I want to remember for the future.

How do I use Vim input methodology for my text boxes in Firefox?

I think I mentioned it before but I use the It’s All Text! plugin. It puts a tiny little button next to all the textarea elements on the page. Pressing that button opens up the textarea contents in your editor of choice. For me that’s gVim but just about anything will do.

Hey, emacs guy - did you hear that? That one will be useful for you too. Write it down. MS Word guy, STFU! You don’t get to say anything!

There is another plugin out there called MozEx which seems to be doing something very similar. I haven’t tested it but it seems like it will also let you use vim for textareas.

I use It’s All Text! and my HTML doesn’t get highlighted

That’s because the plugin saves your buffer as a txt file. I got around this by simply forcing all text files to highlight as if they were HTML. It doesn’t really affect normal .txt files, but if you plug some HTML into one of them, it will look nice. If you want to do the same, just paste this into your .vimrc

au BufRead,BufNewFile *.txt setfiletype html

Luke, why is this post so long? How many words have you typed so far?

I don’t know, let me check…

g<Ctrl+g>

I typed 520 words so far. I’m like halfway done so stop whining. Anyway, this is how you do it. Select some text, then press g and then Ctrl+G and you will see bunch of useful information in the status line. For example, the number of words you have typed so far. Cool, eh? It’s especially useful if you are trying to meet some word count requirement. Which, I usually don’t do very often as you might have noticed. I start typing, and then stop when the post seems like it’s done.

Heh, I wonder how many times you typed the word “vim” in this post?

Again, not sure, but let me check…

:%s/vim //gin

I typed it exactly 7 times. Yep, it’s another nifty trick - you can use the good old regular expressions in a non-standard way. For example the n modifier will prevent the replacement taking place in the regex above. Instead vim will simply print out the number of matches on the status line.

Finally, last tip of the day:

How do I delete everything from the cursor up to but not including a specified character sequence?

A little specific, isn’t it? But it is useful when you want to delete a big chunk of text. I mean, yes there is always the good old dt command which deletes till the first occurence of a character that follows it. There is also d) which deletes the whole sentence and d} which deletes the whole paragraph but this let’s you be very, very specific. Here is how you do it:

d/char-sequence

That is, you press d (or c if you just want to switch to insert mode), then press ‘/’ to enter search mode, enter a character sequence, and hit return. This also works with other commands so you can also do:

v/char-sequence

That’s all I have for now. I hope you enjoyed this quick batch of Vim tips. And if you didn’t I don’t care. The emacs guy over there seems happy because he found out It’s All Text! plugin and the MS Word guy is crying cause I called him names. Eh…

There might be more of these. )