Vim: Buffers, Windows, and Tabs
blog.sanctum.geek.nz
blog.sanctum.geek.nz
The rest of us get along fine (and have for many years thanks) without your new-fangled hoos-its, or even old-fangled hoo-zits. Chirping about how useless vim is without your nerdsboggler jim-crickey is a little... yeccch.
The author of the article is right when he notes elsewhere in this thread that one should learn vim from the ground up. If one does so, one may just find not much climbing is required to get pretty damned high.
My 15 year old vimrc file has 52 lines.
Now i'm going to pop my monocle and ear-horn back in and get back to what I was doing.
Nowadays I do load a few plugins myself on the machines where I do a lot of coding or writing, but the only one that's useful every single day without fail is Tim Pope's Surround [1]. It's so useful so often that I've often thought Bram should simply ask to make it part of Vim's core to supplement the very cool idea of text objects [2]. Even if you're a crotchety old raw-Vim man, I really would say that you've got to try this out.
There are other very good plugins out there. Fugitive [3], also by Tim Pope, comes to mind if you do a lot of Git work and care about crafting really nice commits.
[1]: https://github.com/tpope/vim-surround
The power of Vim is, first and foremost, Vim. A good set of plugins is simply for augmenting Vim and/or tailoring it to your desired workflow.
The plugins I use could be broken up into groups. The first group are the ones that add truly valuable core functionality to Vim. This is the smallest group of plugins by far. I consider the "fuzzy finder" plugins to be in this group (Command-T is the one I use). These plugins add meaningful, widely-applicable functionality to the editor.
Then, there are the ones that are convenience plugins. An example is ack-vim. It lets me run ack from within Vim, and provides the ability to jump directly to files in the result list. I am, of course, perfectly capable of running ack in my terminal. But this makes using ack slightly more convenient.
Next are purely aesthetic ones, like colorschemes. Not important, but pleasant.
Finally, there are the updated language configurations - the things that provide language-specific syntax highlighting, indention, and so on. Vim already comes bundled with these. Adding them as plugins simply allows you to pull the very latest versions of them. Nice to keep these up-to-date, but unless your Vim build is old, you'll already have pretty recent versions of these.
I would miss these plugins if I could never use them again, but when I use a different editor, I rarely find myself missing plugins, but I find myself missing core Vim very badly.
vim's customizability is one of its strongest assets, I have to question the frequently occurring meme that it is somehow unsafe or questionable to actually use it.
Here's a fun little snippet from my .vimrc. Enjoy ;-)
" tab navigation like firefox
nmap <c-s-tab> :tabprevious<cr>
nmap <c-tab> :tabnext<cr>
map <c-s-tab> :tabprevious<cr>
map <c-tab> :tabnext<cr>
imap <c-s-tab> <esc>:tabprevious<cr>i
imap <c-tab> <esc>:tabnext<cr>i
nmap <c-t> :tabnew<cr>:e<space>
imap <c-t> <esc>:tabnew<cr>:e<space>"Window groups" would have been a far more accurate though potentially less familiar name for the feature, in my opinion.
I don't actually use tabs much, personally. I seldom have more than three or four files open at a time.
1. The mappings with TAB wont work in terminal.
2. <C-t> mapping is default map for ctags jump back.
And if you are going to be a long term Vim user, better configure Firefox like Vim than Vim like Firefox. I use Pentadectyle extension to get Vim like bindings in FF and I love it.
For switching tabs I use <c-n> and <c-p> for next/previous in command mode. Under insert mode, its default map for autocompletion which again I don't prefer remapping. Since I don't prefer to switch tabs in insert mode, that's ok.
It's strange how many people (even on HN!) don't know how to use Google's cache. You just need to type "cache:[url]" and press enter.
Ctrl-w < make vertical window narrower
Ctrl-w > make vertical window wider
(You can put a numeric count before the < or >)Also:
Ctrl-w = make windows of equal size
And, when I want two vertical windows each of width 80: se co=161
:vsplit
Ctrl-w =
I gotta use 161 there because apparently one column is taken by the vertical separator.Tons of other useful articles about terminal and vim and things like that...
My opinion is that such an "always on" list is a useless waste of place. My prefered way to switch buffers is to use CtrlP [3]. When it's not available,
:sb <Tab>
is very nice. I've also recently added this line to my ~/.vimrc which is remarkably effective: nnoremap ,ls :ls<CR>:sb<Space>
[1] http://vim.wikia.com/wiki/Easier_buffer_switching#ScriptsFortunately, there's a remedy for this (which should really be default in the Janus distro). A simple plugin that makes sure NERDTree is always present and always in the same state across all tabs. https://github.com/jistr/vim-nerdtree-tabs
Besides, anything you want to do outside of choosing files to edit in your editor should be done in a shell: http://blog.sanctum.geek.nz/unix-as-ide-editing/
I do shell out a lot, but I still think it's more convenient to perform operations against nodes in NERDTree. Saves having to navigate/down/a/really/long/hierarchy.
Yes, like Zork, the journey of solving Emacs or Vi with rapid keying is part of the fun, but sometimes, having a well written hint file (and yes, :help isn't it) can be really welcome.
Same goes for browser tabs. Just a kludge for the rarity of good window managers.
And most other applications that use tabs inside them.
I try to keep a balance between the amount of tiling that vim and my TWM do by clumping similar parts of a project into one instance of vim. Even then I'll have the different instances of vim reside in their own WM tags. I use window splits in vim extensively and only use tabs when I'm working on small monitors (netbook) - even then I'll have at least two windows per tab.
$ gvim filename
Open another file in the same GVim instance $ gvim --servername GVIM --remote otherfilenameInstead learn how to use bare Vim and add stuff to your ~/.vimrc and you ~/.vim/ as and when you need it.
That's the kicker. If you don't actually learn how Vim works, you're missing out on most of its power.
You don't need Janus, you need Vimcasts: http://vimcasts.org/
When a new user has a better command of vi basics and gets a better grasp on the actual limits of Vim's built-in behaviour, and has invested some time in crafting a configuration that suits them, personally, very well for their work and workflow they have, then they're in a much better position to select plugins judiciously and don't have to use a configuration they don't actually understand.
So far, everyone who's recommended Janus to me or asked me to write about it turned out to have a pretty poor understanding of vi-like editors in general.
For perspective, I see the oh-my-zsh [2] project much the same way. I firmly believe that it really is better to start from scratch so that you force yourself to develop an understanding of the applications you use.
Learning vanilla vi has a slightly less obvious side benefit, too -- it works everywhere. You can log into pretty much any Unix-like system and type vi, and something understandable will happen. POSIX [3] is intended to ensure that.
[1]: http://vimuniversity.com/samples/your-first-vimrc-should-be-...
[2]: https://github.com/robbyrussell/oh-my-zsh
[3]: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/vi...
I learned Vim the hard way because I was a student at the time, I was required to do so (or Vi at least) and I had the time to invest in it. For people learning Vim on the job, the choice is either use expedient shortcuts or go back to TextMate. Better to start this way and gradually work towards the bare metal.
If you just want to use vim as a simple editor, it doesn't seem very hard. I mean, I don't really remember a difficult learning curve.
I still only use vim that way. I let my window manager handle the complex stuff, like how to arrange windows and then how to have multiple sets of window arrangements with different things in them (i.e. tabs).
You don't have to learn it at work either. I find it's helpful to make Vim the only text editor you ever use with a dark background. The visual cue seems enough to put you in "Vim mode" so you press i before you start writing. When the background is white in your whizbang C# IDE or a Hacker News comment box, you just use your old editing habits.
See also `:help windows-intro`.
Also gt and gT, but I still like to map these to single-key shortcuts.
map Y gt
map T gTAdmittedly I wouldn't personally recommend those mappings, because both already correspond to commands I use quite frequently. By default Y is synonym for yy, to yank a whole line, and T<char> moves back to the nearest instance of <char> on the current line. The first one I think people could live without, but the latter is kind of important for good vi.
nnoremap <Tab> gt
nnoremap <S-Tab> gT
Normal mode tab switches through tabs. One (and two) key goodness.These two lines in your ~/.vimrc are enough to be able to work efficiently with buffers and windows and make tabs more useable:
set hidden
set switchbuf=useopen,usetab
The first makes it possible to replace the current unsaved buffer with another one.The second allows you to jump to a buffer where it is (in another window, another tab) instead of replacing the current buffer with :b buffername.
Only buffer related split commands like :sbuffer, :sbnext
In retrospect this probably wasn't wise of me; I don't use it myself, and I still run into the occasional Vim guru online who doesn't either.
I've edited the post with a clarifying note and given you a footnote. Thanks.
It only goes away when you explicitly delete it with a call to :quit or :bdelete.
This isn't true. :bdelete makes the buffer an unlisted-buffer. :bwipeout actually removes the buffer completely.Also, as far as buffer handling goes, I really like the SelectBuf ( http://www.vim.org/scripts/script.php?script_id=107 ) plugin for listing and sorting open buffers ( including unlisted! ). CtrlP ( http://kien.github.com/ctrlp.vim/ ) works well for searching them.
Now I'm just using ctrl-p. It does everything for me and it doesn't require ruby or navigating file trees. I can see my MRU files, open buffers, create new files, etc., etc. all in the same area.
I'm not sure if I'm revolting against plugins, but I am attempting to remove all the extra crap I don't use but feel like I needed at some point. I recently got rid of powerline because it does nothing extra for me. It did look pretty, but that was really it.
Less is more :D
tl;dr: ctrl-p is real nice
no step 2. Vim will behave like any other tabbed editor. Be it using tabs or buffers. Done. Why it's not the default? Well i don't care as vim is painful in all sorts of ways without customization anyway :)
So. I will just pretend i read a rant about why it all should be only buffers and tabs and windows be just presentation. And be glad someone took the time to complain about this. Was about time
""press xxx to create a new tab. press xxx to switch to the new one you created. now press xxx to create a new horizontal window. press xxx to switch to it. xxx to create a vertical one...
close vi. type vi *.php to open all php files into separate buffers. press xxx to open 1.php and 2.php side-by-side...""
I'll write and post it sometime in the next week or so, if you're still interested by then. Thanks for the suggestion.