Share a VIM tip weekly!

A few control combos

  • CTRL-d jumps down 16 lines
  • CTRL-u jumps up 16 lines
  • CTRL-f jumps to end of document and makes that the top of your display
  • CTRL-g displays how many lines are in the document and what percentage your current line is at
2 Likes

Using the \1, \2, … \9 operator to substitute the 1st matched pattern in parens, 2nd matched pattern in parens, etc. I was able to quickly substitute some rspec tests in place of some has_many associations. The power of regex and grouping/backreferences!

has_many :tanks, dependent: :destroy

it { should have_many(:tanks) }

:22s/\s.*has_many (:[a-z]*),.*/it { should have_many(\1) }/g
3 Likes
'.         jump to last modification line (SUPER)
`.         jump to exact spot in last modification line
CTRL-o     retrace your movements in file (backward)
CTRL-i     retrace your movements in file (forward)
:ju        list of your movements
:history   list of all your commands
1 Like

Not really a tip - but upgrade to the latest version of MacVim as it contains a fix for the native full-screen mode problems on Yosemite.

Map common keyboard operations using your <Leader> key.

" Basic key bindings
nnoremap <Leader>w :w<CR>
nnoremap <Leader>q :q<CR>
nnoremap <Leader>x :x<CR>

" Fugitive mappings
nnoremap <Leader>g :Gstatus<CR>

" NERDTree
nnoremap <Leader>n :NERDTreeToggle<CR>

" CtrlP
nnoremap <Leader>o :CtrlP<CR>

Also, consider mapping your leader to a more convenient key. I like the spacebar myself.

let mapleader = "\<SPACE>"
2 Likes

Pair program with CoVim. It’s your tool for Collaborative Editing for Vim.

Use ngrok to open a port for external access and share your session to pair program with others!

Another tool that looks like it has more options is wemux.

2 Likes

Switching buffers! You can open multiple files and switch between buffers with :bn and :bp for next and previous. This gets tedious as you open many files. Likewise you can write part of the file name you want with :b filena and hit enter to open it. You can press tab before enter if you want autocomplete and see the file name, but that is not necessary to open the file. You can toggle between files by extension by typing it and then pressing tab until you get to your desired file :b .txt tab - tab - tab

1 Like

Also :b# will switch to the last buffer you were on.

I don’t like typing the : for all these buffer commands, so I mapped buffer commands using my Leader (which I have set as the SPACE). So SPACE b filena for example.

3 Likes

seeing is believing

I’ve installed it with gem install seeing_is_believing and add the following to my .vimrc file:

augroup seeingIsBelievingSettings
  autocmd!

  autocmd FileType ruby nmap <buffer> <Enter> <Plug>(seeing-is-believing-mark-and-run)
  autocmd FileType ruby xmap <buffer> <Enter> <Plug>(seeing-is-believing-mark-and-run)

  autocmd FileType ruby nmap <buffer> <F4> <Plug>(seeing-is-believing-mark)
  autocmd FileType ruby xmap <buffer> <F4> <Plug>(seeing-is-believing-mark)
  autocmd FileType ruby imap <buffer> <F4> <Plug>(seeing-is-believing-mark)

  autocmd FileType ruby nmap <buffer> <F5> <Plug>(seeing-is-believing-run)
  autocmd FileType ruby imap <buffer> <F5> <Plug>(seeing-is-believing-run)
augroup END

If you use Vundle as your package manager add

Plugin 'hwartig/vim-seeing-is-believing'

in your plugins sections and then install it. Now you can auto-compute the Ruby code inline!

3 Likes

Speaking of buffers, here’s what I wrote about them on my blog:


I don’t use them so much now - split windows is probably my most used/favourite feature.

2 Likes

Killer thread.

I still have “trouble” with buffers, it’s amazing how powerful they are.

I’ll just share one I dealt with a while back that was a kind of eureka moment, which I find funny because it’s so simple and dumb. Equalize the size of split windows:

ctrl-w ctrl-=
3 Likes

Great thread! Although, I’m fairly new to ViM I thought I would share my personal favorite. I’m Not sure if I overlooked this one, so I apologize if it’s a duplicate.

while in a document, just type a number and press Enter in order to skip that many lines from your current location - no need to preceed with colon:

100 Enter  #skips 100 lines from current location
20  Enter  #skips 20 lines from current location

I think you get the drift.

2 Likes

Have fun while brushing up your skillz :stuck_out_tongue:

1 Like

Case sensitive replacement with Vim (substitute lowercase hello string with goodbye)

:%s/hello/goodbye/gI
2 Likes

You can use

:r!

as the shorthand to read from an external command and write it into the buffer. Run some Ruby command with code to get a random number.

:r! ruby -e 'puts Random.rand(100000)'
2 Likes

Automatically make directories when you edit. Use this plugin to skip the intermediate part of manually creating a directory every time you want to edit a file in a new directory.

Plugin 'DataWraith/auto_mkdir'
1 Like

hrm… I may install this. That’s a thing I miss from EMACs.

2 Likes

There are a few quick tips in this video (anyone bought the book btw? Any good?)

1 Like

It doesn’t look like it was mentioned before. If you use vim and you use git, you should install GitHub - tpope/vim-fugitive: fugitive.vim: A Git wrapper so awesome, it should be illegal.

2 Likes

Add to your vimrc so Nerdtree opens automatically with each tab and the same state as the last nerdtree:

autocmd BufWinEnter * NERDTreeMirror