Share a VIM tip weekly!

Use CTAGs to make vim more like an IDE.

brew install ctags

Then in the root of your project:

ctags -R --languages=ruby --exclude=.git --exclude=log .

Now in Vim:

If you position cursor over the method and hit CTRL+] it will take you into the method.
CTRL-T will take you back from that method.
CTRL-I and CTRL-O will take you In and Out from the method.

More here: Supercharge your VIM into IDE with CTags - sensible.io Blog

1 Like

Capitalize everything TO EMPHASIZE IMPORTANCE! :wink:

In visual mode highlight what you want to change. Then press

SHIFT U
2 Likes

I use a setup similar to this so I can have ctags generated for me when I commit stuff to git.

http://tbaggery.com/2011/08/08/effortless-ctags-with-git.html

1 Like

I think this has not been mentioned:

shift + v: visual selection for line

After a block has been selected, move it under the line_number:

:’<,’>move line_number

or copy it under the line_number

:’<,’>copy line_number

Some interesting examples:

Move the block at the current line using the dot

:2,17move .

Copy the block at the start of the file (above the first line)

:15,19copy 0

Move the current line under the line 7

:move 7

2 Likes

Starting this upcoming month I’ll be a paid blogger. One of the requirements is word count. Add the following to your .vimrc file to calculate your current word count on *Posix systems such as Linux/Mac.

nmap <silent> <leader>w :w !echo Word Count: `wc -w`<CR>

If your leader key is the backslash key then in normal mode you can press \w to run the command and see you current buffer’s word count.

Add spellcheck to Vim. Vim documentation: spell

:setlocal spell spelllang=en_us
1 Like

Targets looks interesting if you like surround but wanted more features. GitHub - wellle/targets.vim: Vim plugin that provides additional text objects

2 Likes

In visual mode whatever you highlight can be lower cased with u. Also you can toggle the case of what you’ve highlighted with ~. I like to use tilda after highlighting a column of lined up text.

3 Likes

Save current file to another and keep current one open

:w path/to/other/file.rb

Save current file to another and switch to other (closes the current one)

:sav path/to/other/file.rb
2 Likes

:28d will delete the 28th line of a file.

You can combine this with '' and p to grab line from anywhere in a file and paste below your current cursor position.

:28d<Enter>''p
1 Like

g; goes back to the location of the last edit. This is “pick up where I left off before going somewhere else”. It’s gold. But that’s not all — it keeps track of your edit history so that you can go back five edits ago. And g, moves you back forward in the edit history.

3 Likes

Today I learned :j will merge the line below to the current line at the end with a proper space between. So if your cursor is at the beginning of line one in normal mode and you enter :j and hit enter then line two gets appended to the end.

From:

qwer
asdf

To:

qwer asdf
1 Like

I’ve remapped Q,W, and E for quality control (npm’s write-good), word count, and spell checking (npm’s markdown-spellcheck) .

nmap <silent> <leader>q :w !write-well %<CR>
nmap <silent> <leader>w :w !echo Word Count: `wc -w`<CR>
nmap <silent> <leader>e :w !mdspell -r -n -a --en-us %<CR>

I didn’t like the poor grammar of the write-good command so I symbolically linked it as write-well.

2 Likes

New gem released! read_source This gem has a VIM feature which will let you open any Ruby method (written in Ruby) directly into VIM! You can do this without interupting your irb session or Ruby process if you start a VIM session with the --servername flag. Now you can learn the code you’re working with much more quickly! :smiley:

2 Likes

Comment-out an entire file.

In normal mode: gg0CTRL-vSHIFT-GSHIFT-I#ESC

2 Likes

I just learned about folding in vim. You can expand and collapse sections of code and documents.

http://vimcasts.org/episodes/how-to-fold/

2 Likes

Select a paragraph and sort. (in normal mode)

vip:sort

2 Likes

Unicode Characters

In insert mode do CTRL-v SHIFT-U and then type the numbers for the character and hit ENTER.

em dash ( — ) is 2014

In Linux to type it (not in VIM) you need to do CTRL-SHIFT-u2014ENTER

2 Likes

“Don’t put any lines in your vimrc that you don’t understand.”
https://dougblack.io/words/a-good-vimrc.html

1 Like
1 Like