Getting efficient with vim and YADR

A couple weeks ago I posted my new working environment: YADR + iTerm2 . I am still loving it, and in fact it gets better and better as one gets used to the shortcuts and the new functionality that was simply not there before! The problem is that due to all that many new shortcuts + the chances of you messing up when typing something wrong in vim, I keep finding myself in google or egreping my YADR tree very often. This post is therefore as much for myself as for anybody in the same situation as me: A compilation of shortcuts and useful commands that make my/your life easier.

Using ctags

They generate an index (tag) file of names in the current directory based on information in source and header files. This allows doing lookups of functions and operations while coding in vim.

Depending on your system the process my vary. This is what I use:

  • Linux:
    #!/bin/sh
    find . -name “*.c” -o -name “*.cpp” -o -name “*.h” -o -name “*.hpp”  > cscope.files
    ctags -L cscope.files
    ctags -e -L cscope.files
    cscope -ub -i cscope.files
  • MacOS (YADR and zsh assumed):brew install ctags -R (in parent directory)

At this moment you can get your cursor on top of a function and press “Ctrl-]”. This will show you the code of the function itself.

Shortcuts can be added, erased or modified in: vim/plugin/settings/smart_jump_to_tag.vim

# VIM shortcuts

List of some useful VIM commands. There are many more, but these can be considered the base and should be enough to be agile when programming in VIM.

Insert / Append
i Inserts text to the left of the cursor.
I Inserts text at the beginning of the line, no matter where the cursor is positioned on the current line.
a Begins inserting after the character (append) on which the cursor is positioned.
A Begins inserting at the end of the current line, no matter where the cursor is positioned on that line.
Open
o Begins inserting text on a new, empty line that is opened for you, below the current line. This is the only command that will allow you to insert text BELOW the LAST line of the file.
O Begins inserting text on a new, empty line that is opened for you, above the current line. This is the only command that will allow you to insert text ABOVE the FIRST line of the file.
Deleting,copying and changing
d Delete text.
y Copy text
c Change text from one thing to another, which you will type.
! Filter text through a program.
< Shift a region of text to the left.
> Shift a region of text to the right.
Single Key Movements
h Move cursor to the left one character.
l Move cursor to the right one character.
j Move cursor down one line.
k Move cursor up one line.
^ Move cursor to the beginning of the line.
$ Move cursor to the end of the current line.
1G Move cursor to the first line of your document. Other numbers will move to the line specified by number (ex. 50G goes to the 50th line).
$G or G Move cursor to the last line of your document. Other numbers will move to the line specified by number (ex. 50G goes to the 50th line).
Ctrl-U Move cursor up in file 12 lines. Hold down the key marked CTRL (stands for control) and type U. CTRL is like another shift key.
Ctrl-D Move cursor down in file 15 lines.
w Move cursor forward to the next word, stopping at punctuation.
W Move cursor forward to the next word, ignoring punctuation.
e Move cursor forward to the end of the word, stopping at punctuation.
E Move cursor forward to the end of the word, ignores punctuation.
b Move cursor backwards to the previous word, stopping at punctuation.
B Move cursor backwards to the previous word, ignores punctuation.
H Move cursor to the top line of the screen, (as opposed to the top of the document which may not be the same place).
M Move cursor to the middle of the screen.
L Move cursor to the last line on the screen.
% Move cursor to the matching parenthesis, bracket or brace. Great for debugging programs.
( Move cursor to the beginning of the previous sentence (where a punctuation mark and two spaces define a sentence).
) Move cursor to the beginning of the next sentence.
{ Move cursor to the beginning of the current paragraph.
} Move cursor to the beginning of the next paragraph.
; Repeat the last f or F command (see below).
Move cursor to a previously marked location in the file. (ex. ma marks the location with the letter a, so a (apostrophe a) moves back to that location).
f Find the character corresponding to the next keystroke typed. Move the cursor to the next occurrence of that character (on the current line only).
F Repeat the last f or F command (see below).
Useful
u Undo
Ctrl-r Undo-undo, i.e., redo
x Delete character(s) to the right of the cursor, starting with the one beneath it.
r Replace the character under the cursor with the next character you type. This can be a very useful command. If you wanted to split up a line between two words, you might put the cursor on the blank space before the word you would like to go on the next line and type r . This would replace the space between the words with a carriage return and put the rest of the line onto a new line.
J Join lines; the opposite of the line splitting operation above. This will join the current line with the next line in your file. Also very useful.
R Replace lines; puts you in INSERT mode but types over the characters that are already on the current line.
p Paste line(s) you deleted (or yanked) back into the file. This is an excellent command if you want to move a few lines somewhere else in your file. Just type 3dd to delete three lines, for example, and then move to where you want those lines to be and type p to paste the lines back into your file below the cursor.
. The period . command repeats the last text modification command, whatever it may have been (insert, deletion, etc).
:r filename RETURN Read a file into the current file being edited. The file be added gets placed below the current cursor position. Please note the colon : before the r in this command.
Ctrl-L Redraw the screen. If somebody writes to you while you are in the middle of vi and junk appears all over your screen, dont panic, it did not hurt your file, but you will have to hold down the CTRL key and type L to clean it up (CTRL L).
d$ Delete (including the current character), to the end of the line.
d^ Delete (excluding the current character), to the beginning of the line.
dw Delete a word(s), stops at punctuation.
dW Delete a word(s), ignoring punctuation.
de Delete to the end of next word.
$number dd Delete $number line(s).
dG Delete from the current line to the end of the document. CAREFUL: Slightly dangerous.
dH Delete from the current line to the line shown at the top of the screen.
Search and Replace
/$word Finds the next occurrence of $word. This will also find $word as a sub-string in other words.
?$word Finds the previous occurrence of $word.
d/$word Deletes until the next occurrence of $word. This is to demonstrate how the delete prefix can be used with any cursor movement command.
:g/oldword/s//newword/gc This will find all occurences of oldword and replace them with newword. The optional c at the end of the command tells vi that you would like to confirm each change. Vi will want you to type in y to make the change or n to skip that replacement. Great for spelling fixes.
Exit
ESC :wq RETURN Save and exit VI
ESC :q! RETURN Exit WITHOUT saving changes

# YADR shortcuts

This list is VERY extensive, and what is more, it is dependent on the YADR and DOTFILES version. The best tip is to check the yadr repository and look for the shortcuts there.

The VIM shortcuts can be found at ~/.yadr/vim/plugin/settings.

(*update) Here are some of the shortcuts I find more useful. Please note that even though some of them are the default, others are customized. You should always refer to your .yadr directory and check the assigned shortcuts to be sure. You can also use :map $keycombo to see what it is assigned to what (e.g., :map ,z will open NerdTree ).

You can find my own configuration in own branch : https://github.com/javigon/dotfiles.git

YADR shortcuts
Ctrl-b Show different buffers to rapidly access files
,z Open NerdTree (this is just awesome!)
ss Open a panel horizontally (it actually splits the current panel)
vv Open a panel vertically (again, it splits)
Ctrl-{h,j,k,l} Move through the panels with the usual {h,j,k,l} mapping
,f (on top of a function) Instantly Find definition of class (must have exuberant ctags installed)
,F (on top of a function) Same as ,f but in a vertical split
,gg GitGrep command line, type between quotes
,gf (on top of a function) Same as vim normal gf (go to file), but in a vertical split. It works while in GitGrep panel
K (on top of a function) GitGrep the current word under the cursor and show results in quickfix window
,T Tag list (list of methods in a class)

# zsh shortcuts

The best is going to the official manual (http://www.cs.elte.hu/zsh-manual/zsh_14.html)

It is very useful to look at the names in the manual and then bind custom keys to them in /zsh/key-bindings.zsh 

(e.g., the zsh command to move backward at a work granularity  is forward-word. To map it to Ctrl-w add bindkey ‘^w’ backward-word in /zsh/key-bindings.zsh )

Screen Shot 2013-09-02 at 12.32.49

# iTerm2 shortcuts

The same applies to iTerm2. The best place to look at is: 

  • iTerm2 > Preferences > Keys
  • iTerm2 > Preferences > Profiles > Keys

# Some minor preferences

They say that the devil is in the details. Here you have one: MacOS – at least v. 10.8.4 – does not take the hostname from the sharing preferences. If you want your prompt to display the name YOU want to give to your machine, just type:

sudo scutil –set HostName $name-of-choice

I hope that you find this information useful. Definitely not a post to skim more than once, but at the same time, a place to come back for reference.

Enjoy!

–javier

# References

– http://www.keyxl.com/aaab462/105/VIM-Text-Editor-keyboard-shortcuts.htm

– http://skwp.github.io/dotfiles/

6 thoughts on “Getting efficient with vim and YADR

  1. Pingback: Setup for transparent SSH with iTerm and AppleScript | My Cellar Door

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s