Everything I need to remember about Vim

This blog post is completely useless to all of you. I’m writing it for the sole purpose to help me remember important vim commands to make my life easier. These commands all take special VIMRC files. Suckafree 2003!

As of 1/23/16, one of my biggest misunderstandings with vim was the c vs d command. I thought c stood for cut, but it actually stands for change. c and d actually do the same thing, except c will put you in insert mode. If I delete a line using d, it will be paste-able with the p command. That’s a huge difference!

Basic Configurations

Where are my VIM Bundles stored?~/.vim/bundle/ I have Vundle, but I found it to be hit and miss. Basically I don’t know the difference between a Plugin or a Bundle. But whenever I drop git clone VIM plugins/bundles into this folder, it works.

How do I edit my VIMRC file? From within Vim, :e $MYVIMRC

How do I refresh my VIMRC file when I’m making changes? :so $MYVIMRC or :so % so stands for source, % returns current file name (see :h current-file).

Basic Understanding

Operators and Motions…most commands are comprised of first an operator, then a motion d is an operator. The delete operator A motion is like $ which will move you to the end of the line

Motions with Counts 2w will more 2 words forward

Basic Commands

Splits :sp to do a horizontal split :vsp to do a vertical split

leader h [find file] ctrl+v for quick vertical split

Within a rails app, us gf to travel to a method. Can be used on so many things.

Indent a line to it’s normal indention

=ip will properly indent an entire paragraph! Damn this is actually cool.

Delete Letter under Cursor x

Delete a word dw daw -> will delete word no matter where cursor is, it will also delete any space after the word diw -> will delete word no matter where cursor is, it will leave the space after the word

Delete until the end of the line d$

Undo Undo entire line, U Undo last action, u

Replace r -> replacement letter

Whan -> re -> When

% % will jump between opening ( and closing ) for the following (), {}, []

Changing inside a ()[]{}“”“ ci) will delete everything within the parenthesis, and put you in insert mode… ci] ci} ci’ ci” are all options (damn that did work, try it out… maneauver within these parens and type ci))

Got HTML Problems? I feel bad for you son, vim got your back.. cit -> change inner tag…

Wholly fucks I can change this line, just by typing cit… wow so cool.

Centering the current line zz

Bring current line to the top of vim window zt

Bring current line to the bottom of vim window zb

Jump to the bottom of the vim window L

Multi Line Comment

C-v highlight down, -> I -> # -> <esc>

Window Management VIM

(leader is equivalent to hitting the space bar) leader - will maximize the window leader = will go back to normal

Move to top of file gg

Move to line 30 30gg

Find and Replace 4. To change every occurrence of a character string between two lines, type :#,#s/old/new/g where #,# are the line numbers of the range of lines where the substitution is to be done. Type :%s/old/new/g to change every occurrence in the whole file. Type :%s/old/new/gc to find every occurrence in the whole file, with a prompt whether to substitute or not.

Leader Commands

<leader>h -> opens up :FZF I type for the file I’m looking for… then once on top, I can hit ctrl-v to do a verticle split. or enter to open in current window, or ctrl-t to open in a new tab

Restructuring Window views

Move Window Panes C-w H,J,K,L H will move current window to the left J will move current window down… etc

Resize Window Panes C-w+ —> increases height C-w- —> decreases height 10C-w- —> decreases height by 10 C-w< –> decreases width C-w> —> increases width C-w= —> equalize windows again

Send Terminal Command using Vim :!ls

will list the directory

Save Highlighted text as new file named TEST Use v to enter visual mode, highlight the text. Hit : -> w TEST

Add a file into your current vim workspace :r

Open new lines O -> above o -> below

a vs i a will get you into insert mode on the next letter i will get you into insert mode on the current letter

Really Beneficial Commands for all of Rubyland

These commands are from the ruby-text-objects plugin to access the commands granted by this method do :h ruby-text-objects I learned about these commands at https://upcase.com/videos/navigating-within-ruby-files

var -> will highlight the entire block of code within a method, do, or module v - select a - around r - ruby-block

vir -> will select inside a ruby block v - select i - inside r - ruby-block

after you are in visual mode, you can then use ir or ar to select smaller or large ruby code segments

Select an entire method from deep within the method vam

Select the internals of an entire method from deep within the method vim

To Sum up…

method class / module rubyblock
inside im iM ir
v [select]
d [delete]
c [change]
y [yank]
around am aM ar

% -> will jump between the beginning of a method or the end of a method

File Naviation with Vim

Highlight over a require ‘appraisal/otherthing’, and hit the command gf to see the source file. to go backwards, CTRL + O

The other important command is with tags. CTRL + ] -> to go find those files. For instance in Ruby on Rails, < ActiveRecord::Base, highlight over ActiveRecord and hit CTRL + ].

Or if you have a method definition within a method, I believe you can use that command to jump to the method.

To go backwards it is CTRL + T

The problem lies within it not automatically tagging projects. If you’d like to manually tag a project, use ctags -R . That creates a tags file within your project directory. This should be git ignored. But there is a way to do it automatically when git is updated.

Fuzzy Files to Search for Files using VIM

this plugin is freakin remarkable…

https://github.com/junegunn/fzf

Now from within VIM do :FZF and it will do a file search for anything you type into the box.

Color scheme…

Where the hell did I save the color scheme? ~/.vim/colors/

autocmd Filetype

These type of configurations are good for setting tab widths for various file types. For instance a Python Vim file will use a different tab length than say a ruby file.

Vim Surround

https://upcase.com/videos/onramp-to-vim-plugins#surround.vim Vim surround is another awesome plugin written by Tpope. Guy is a plugin writing P-I-M-P!

( Anyways )…

dst -> delete surrounding tag -> html tags ysw( -> yes, surround word in ()

cst<\p - change surrounding tag to paragraph yss<\p -> add surrounding paragraph tag to line

in visual mode, highlight the lines and use a capital S<\p> to create a paragraph tag surrounding

gello!!

OTHER IMPORTANT VIM SURROUND THING… Let’s say I have the following (Link Text) And I realized instead of () I want [].

I would type cs([ enter. That will change it to () to []

Vim Rails

If I’m in the model, I can use :Vunittest to open up the corresponding model.

PARTIAL EXTRACTION Visually highlight some text in a view, :Rextract [partial_name] Absolutely fucking amazing!

Commenting

gcc -> comment a line gc2j -> comment 2 lines gcam -> comment around methods

Post Content