vim, switching between files rapidly using vanilla Vim (no plugins) -


i understand limiting myself vanilla vim (not using plugins) limits power of editor, switch between different machines frequently, trouble move environment around everywhere. want stay in vanilla vim.

something holds me ability switch between files. (believe @ least) have understanding of buffers, windows, tabs, netrw (vex, ex, etc).

but in editor such sublime text, can type ctrl-p , instantly @ file.

i know can drop down shell, wonder if there other "hidden" secrets rapidly switching between files in vim based off more filename.

the closest equivalent ("closest", not "exact") st2's ctrl+p plugin called, ready… ctrlp. there other similar plugins command-t or fuzzyfinder.

i use ctrlp , love wholeheartedly support decision go "plugin-free". it's not easiest way go pay off in long run.


opening files

the basic way open file :e /path/to/filename. thankfully, tab-completion , wildcards: classic * , special one, **, stands "any subdirectory".

combining of that, can do:

:e **/*foo<tab> 

to choose files containing foo in name under working directory or:

:e **/*foo/*bar<tab> 

to choose files containing bar in name under subdirectory containing foo in name, anywhere under working directory.

of course, works :tabe[dit], :sp[lit] , :vs[plit], too.

those commands limited 1 file, though. use :next open multiple files:

:next **/*.js 

and take @ :help arglist.


jumping between buffers

:b[uffer] basic buffer-switching command:

:b4         " switch buffer number 4 :bn         " switch next buffer in buffer list :bp         " switch previous buffer in buffer list :bf         " switch first buffer in buffer list :bl         " switch last buffer in buffer list :b foo<tab> " switch buffer name tab-completion :b#         " switch alternate file 

note many of these commands , relatives accept count.

the :ls command shows list of loaded buffers. bit "special", though: buffers assigned number when created can have list looks 1 2 5 if delete buffers. bit awkward, yes, , makes switching buffer number bit troublesome. prefer switching partial name, :b foo<tab> or cycling, :bn :bp.

anyway, here cool mapping lists loaded buffers , populates prompt you, waiting type number of buffer , press <enter>:

nnoremap gb :ls<cr>:b<space> 

with mapping, switching buffer simple as:

gb (quickly scanning list) 3<cr> 

or:

gb (quickly scanning list) foo<tab><cr> 

the idea comes image taken bairui's collection of vim infographics:

flying vs cycling

vim has <c-^> (or <c-6> on keyboards)—the normal mode equivalent of :b#—to jump between current buffer , previous one. use if alternate between 2 buffers.

read buffers in :help buffers.


go declaration

within file, can use gd or gd.

within project, vim's "tags" feature friend you'll need external code indexer ctags or cscope. basic commands :tag foo , <c-]> cursor on method name. both tools integrated vim: see :help tags, :help ctags , :help cscope.

for it's worth, use tag navigation extensively move within project (using ctrlp's :ctrlptag , :ctrlpbuftag commands, mostly, buit-in ones too) , favorite "generic" buffer switching method name.


deploying config

a lot of vim users put config under version control makes very quick , easy install own config on new machine. think it.


edit

a few months ago, had work on remote machine outdated vim. have installed proper vim , cloned own beloved config decided travel light, time, in order "sharpen saw". built minimalist .vimrc , revisited couple of half forgotten native features. after gig, decided ctrlp wasn't necessary , got rid of it: native features , custom mappings not sexy job done without dependencies.


juggling files

set path=.,** nnoremap <leader>f :find * nnoremap <leader>s :sfind * nnoremap <leader>v :vert sfind * nnoremap <leader>t :tabfind * 

:find great command set path correctly. settings, ,ffoo<tab> find files containing foo under current directory, recursively. it's quick, intuitive , lightweight. of course, benefit same completion , wildcards :edit , friends.

to make process quicker, following mappings allow me skip entire parts of project , find files recursively under directory of current file:

nnoremap <leader>f :find <c-r>=expand('%:h').'/*'<cr> nnoremap <leader>s :sfind <c-r>=expand('%:h').'/*'<cr> nnoremap <leader>v :vert sfind <c-r>=expand('%:h').'/*'<cr> nnoremap <leader>t :tabfind <c-r>=expand('%:h').'/*'<cr> 

warning! path option extremely powerful. value above—.,**—works for me languages use don't have standard library. proper value depends entirely on your needs.


juggling buffers

set wildcharm=<c-z> nnoremap <leader>b :buffer <c-z><s-tab> nnoremap <leader>b :sbuffer <c-z><s-tab> 

the mappings above list available buffers in "wildmenu" empty prompt, allowing me either navigate menu <tab> or type few letters , <tab> again narrow down list. file mappings above, process quick , friction-less.

nnoremap <pageup>   :bprevious<cr> nnoremap <pagedown> :bnext<cr> 

those mappings speak themselves.


juggling tags

nnoremap <leader>j :tjump / 

this mapping uses regex search instead of whole word search can ,jba<tab> find tag foobarbaz().

yes, fuzzy matching addictive can productive without it. , fraction of cost.


more edit

a couple of additional tips/tricks…


wildmenu options

the "wildmenu", enabled set wildmenu, makes file/buffer navigation easier. behavior governed bunch of options worth investigating:

wildmode tells vim how want "wildmenu" behave:

set wildmode=list:full 

wildignore filters out cruft:

set wildignore=*.swp,*.bak set wildignore+=*.pyc,*.class,*.sln,*.master,*.csproj,*.csproj.user,*.cache,*.dll,*.pdb,*.min.* set wildignore+=*/.git/**/*,*/.hg/**/*,*/.svn/**/* set wildignore+=tags set wildignore+=*.tar.* 

wildignorecase allows search foo , find foo:

set wildignorecase 

file marks

augroup vimrc   autocmd!    autocmd bufleave *.css  normal! mc   autocmd bufleave *.html normal! mh   autocmd bufleave *.js   normal! mj   autocmd bufleave *.php  normal! mp augroup end 

i found gem in else's ~/.vimrc. creates file mark @ exact position of cursor whenever leave buffer that, wherever are, 'j jumps latest javascript buffer edited. awesome.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -