Thursday, September 19, 2013

some tricks in vim

1. Editing multiple files
(1) open two files
vim -o file1 file2
will open the files in two windows instead of needing to flip between the files in one window

or:
vim -p foo bar
And traverse the tabs using gt and gT


open another file

open file under cursor                   gf                                    
  (absolute path or relative) 

When you use split to split windows, and you want to get back to original window, type :only
If you want to switch between windows, you can use :C-W C-W or :C-W-j, k, h, l

Cancel a split window, 
The command :hide will hide the currently focused window. I think this is the functionality you are looking for.
if you want to quit the current window, you may use :ZZ or ZQ or :wq

To close all splits, I usually place the cursor in the window that shall be the only visible one and then do:on which makes the current window the on-ly visible window. Nice mnemonic to remember.


Edit: :help :on showed me that these commands are the same:
  • :on
  • :only
  • CTRL-w CTRL-o
  • And yes, also CTRL-W o has the same effect (as Nathan answered).
Each of these four closes all windows except the active one.
While using Vim I'll sometimes want to look at a function definition or a struct definition, so I'll use C-] to jump to it. However, there are a few problems I run into. First off, I don't know how to jump back easily. It appears the previous file I was in closes and I'm now in the new one. Is there a way to jump back, or keep a stack of open files that I can pop back to or something?
Another thing I've noticed that when I have a change in the current file I need to save it because, like a mentioned a moment ago, my current file is being closed before the next one opens.
And sometimes I want to view my current code and my header at once. Is there a way open the tag definition in a split?

set hidden
to you vimrc. It'll allow you switch files without saving them. I think this is one of 'must have' options.
Use C-o to jump back to previous locations which were autosaved in a jumplist.
:h jumplist
To jump back from C-], use C-T. The :tags command shows the current stack.
Set the autowrite option to automatically save what you're doing before jumping to a new file.
Use C-W C-] to open the tag in a new window.
Finally, :help tags is the section in help that explains all this and more.

Another useful feature that comes handy is the uppercase marks. These marks are not local to a buffer and can be used to jump to them from across files. If you mark the line as say "A" using mA command before starting a long and arduous jumping around task, you can finally return back to the original position quickly by typing 'A or `A.

Vim 7 lets you have multiple tabbed pages, and each tab can be split into multiple windows. Vim shows the tabs along the top so you can select them with the mouse.
The commands I use most frequently are:
start vim with the -p option to open each file argument in a new tab.
:tabe <filename> edit a file in a new tab page. If the filename is left out then edit a new buffer.
:tabc closes the current tab page.
:tabo makes this the only tab page by closing all the others.
:tabs lists the tabs
tabdo <cmd> executes a command on every tab page.
gt cycle forward through the tab pages, gT cycles backwards.
See :h tab-page for more information.

No comments:

Post a Comment