We considered the essential features in .vimrc required by a programmer editing in vim in Chapter 0. Time for some more features, more easy editing, and more horrible puns. The updated version has been uploaded on github on a new branch. Click here for the source.
Code folding
Code folding is a feature that is an integral part of almost all the editors. You do not want the burden of seeing all the functions, loops and blocks in your code all the time : the time to scroll through is too tiring. The answer is to fold those sections : that is substitute them view-wise with a single header describing the code block.
Eclipse allows you to fold functions through mouse button clicks. Since we assert our loyalty to the keyboard while using vim, lets see how we can manage that. For convenience sake, we will use key mappings for useful folding operations.
| :nmap <F4> zfa} | |
| :nmap <F5> /}<CR>zf%<ESC>:nohlsearch<CR> | |
| :nmap <F6> zE<CR> |
a. Lets look at the first mapping. The zfa} is used to fold a code block enclosed by { and }. For this, the cursor needs to be present between the two braces. Thus, to fold the block, all you need is to position your cursor between its two braces, and type the sequence zfa}.
b. The second mapping is slightly complex to understand, but it spares the user from manually having to move the cursor to the block of interest. This mapping allows you to directly fold the next block to be encountered. Lets break this down to simplify.
1. Search for the next occuring } using the “/}” command and take your cursor there. (<CR> stands for Enter)
2. Do a zf% to fold the block.
3. Do a nohlsearch to undo the effect of your /} command that highlights all the }’s. This is required only if you have set the hlsearch option in your .vimrc.
c. To remove any folds in the code, we can use the third mapping for zE.
More on folds can be found here.
The mkview-loadview cycle : Understanding the chakra-view
Once you are done setting your folds, you want them saved across different sessions, that is, the folds should be preserved after closing and re-opening a file. In general, you can create a view of your folds using the mkview command, and reload that view using the loadview command. Using the au command from Chapter 0, we can make the loading and saving automatic.
| :au BufWinLeave * mkview | |
| :au BufWinEnter * silent loadview |
More on views here.
NOTE : Before you set these options in your .vimrc, make sure that the $HOME/.vim folder is created. Any new view that you create using mkview is stored in the $HOME/.vim/view directory.
Vim and windows
Even with terminals allowing multiple split windows on the current screen, having multiple windows within a current session is helpful as well, when it comes to certain tasks. For example, you can easily copy-paste text the vim way between two open windows in the same vim session. Another use of multiple windows in vim is when you are browsing code using a tool like cscope, where searches for a declaration or a definition can be conveniently opened in new windows. Here‘s a basic tutorial for windowing in vim.
1. Initial window height
Suppose you have a single file in a window open in a vim session. Suppose you want to open another file in a split window (using the split command). How many lines should you display for the old and the new file? This is decided by the winheight parameter. By setting this parameter to a very high value, say 9999, we always ensure that the newly opened window will occupy most part of the vim session, whereas the older windows will be minimized. This parameter is set accordingly.
2. Changing window size
To control the size of the window, we can define additional mappings.
| : nnoremap <silent> <Leader>+ :exe "resize " . (winheight(0) * 3/2)<CR> | |
| : nnoremap <silent> <Leader>- :exe "resize " . (winheight(0) * 2/3)<CR> |
In the above mappings the <leader> corresponds to the ‘\’ character. Therefore, the mapping \+ will increase the window size to 3/2 the original, whereas the \- mapping will reduce the window size to 2/3 the original.
3. Cursor migrations across windows
And finally, window migrations. Vim defines ways for you to migrate your cursor between multiple windows opened up in the same session, but the keystroke sequence for them is a bit complex. We therefore provide some simple mappings for the same.
| " Move to the window below | |
| : nnoremap <C-J> <C-W><C-J> | |
| " Move to the window above | |
| : nnoremap <C-K> <C-W><C-K> | |
| " Move to the window on the right | |
| : nnoremap <C-L> <C-W><C-L> | |
| "Move to the window on the left | |
| : nnoremap <C-H> <C-W><C-H> |
The C-J stands for Ctrl+J key sequence. The same applies to other mappings.
More on windowing options in vim is available here.
A note on the new .vimrc structure
The latest .vimrc source is available on the github account, on the v1 branch. As of now, the main .vimrc file will only be a file which sources other files, each corresponding to one chapter. Therefore, in the current .vimrc file, we source 2 individual files :
| " Include individual files using the source command. | |
| " Part 1 : Basics of vimrc. | |
| source vimrc_0_basics.vim | |
| " Part 2 : Mapping views and manipulating windows. | |
| source vimrc_1_views_windows.vim |
Each .vim file contains the actual settings we discuss in the blog.
Any comments, questions, feedback highly appreciated. Please write to us at poppingthestack@gmail.com.
References:
1. On folding : http://vim.wikia.com/wiki/Folding
2. On views : http://vim.wikia.com/wiki/Make_views_automatic
3. Basic options for windowing : http://web.cs.swarthmore.edu/help/vim/windows.html
4. On more windowing : http://vimdoc.sourceforge.net/htmldoc/windows.html


