VIM Zone: a programmer's quest to do everything with VIM
My compiled Vim, everywhere in the system, on Ubuntu
Due to the unavailability of the latest vim (7.3) in latest released Ubuntu (they're stuck at 7.2), I have compiled my own vim binary. Even more, I don't want the system 7.2 even to be installed in the system. I use Midnight Commander as a shell for my console tasks and I want my vim to be integrated it it. Fortunately, MC uses the Debian alternatives system, so it was easy for me to configure my system to use Vim everywhere.
First, I have compiled vim with the prefix as $HOME, so it is installed in ~/bin and automatically available in $PATH. Next, I ran:
sudo update-alternatives --install /usr/bin/editor editor /home/tibi/bin/vim 10
This created a new alternatives option. Next I've selected it as default with
select-editor
Installing and selecting a view alternatives was just as easy:
sudo update-alternatives --install /usr/bin/view view /home/tibi/bin/view 10 sudo update-alternatives --config view
Pentadactyl, the Vimperator replacement
If you haven't got the memo yet, the cool kids no longer run Vimperator, but instead have forked it and run it under the name of Pentadactyl. The reasons, apparently, are that Vimperator received slow updates and didn't have a nice environment for the community to get involved.
Well, I'm trying Pentadactyl and there are a few visible things that are improvements from Vimperator, but the workflow remains the same. I guess I'm not a Vimperator power user. One thing I miss, though, is the name. Vimperator had a certain resonance that I don't find in the five fingers pun that is Pentadactyl.
Setting the window title for GVim
One recent annoyance I had was that I couldn't recognize the multiple GVIM windows opened on the taskbar. Their title usually said "NERDTree", or "configure.zcml", which is not at all significant to me. Some searching led me to this solution
:set title titlestring=Some significant string
My workflow, lately, consists of opening a gvim window, running:
:cd ~/work/project
and there I hit ctrl+f to open the NERDTree window, already preselected on the project folder. It works very well, it feels very much like a project. Now, if only I could also change the title from this step... well, that's a good idea for a plugin. I might try to create it.
Buffer management nirvana: settings for tabbar
I've rebuilt my .vim folder to take advantage of splitting everything in pathogen bundles, so I took the opportunity to comb through the plugins that I use. For a week I missed the tabbar plugin, which I've replaced with buftabs. Not the same thing. Today I replaced it back with tabbar, and these are the settings that I came up with:
let g:Tb_SplitBelow=1 " Put the tabbar at the bottom let g:Tb_MaxSize=0 " I want it to automatically resize if I have buffers that fill more then one line let g:Tb_MoreThanOne=0 " I want the tabbar to be always visible let g:Tb_ModSelTarget = 1
These are the best settings for me. All I need left to do is to change the colors of the tabs, which is a long standing task that I should do. Based on the tabbar documentation, this is what I need to change:
" Tb_Normal ............. for buffers that have NOT CHANGED and are NOT VISIBLE. " Tb_Changed ............. for buffers that HAVE CHANGED and are NOT VISIBLE " Tb_VisibleNormal ....... buffers that have NOT CHANGED and are VISIBLE " Tb_VisibleChanged ...... buffers that have CHANGED and are VISIBLE
Hands down, the best colorscheme for Vim
By virtue of being the most consistent across terminal Vim and GVim and also having a different color to highlight the buffer tabs in the status bar, I nominate calmar256-dark as the best Vim/GVim color scheme. Even more, those green shades look really good and provide a good environment to work with even the longest hours.
Vim mode for the readline
I admit it, I'm clueless when it comes to bash editing. I know very few tricks with it: <C-r> for search in history, <C-u> to erase the current command line, but besides this, nothing.
But that will change from now on: I've configured my ~/.inputrc to have a vi mode as default, so now I can use most of my vim knowledge to navigate long command lines. Even better, I can hit Esc-v to enter "visual mode", aka vim. This and more is explained on this blog post
# Be 8 bit clean. set meta-flag on set input-meta on set output-meta on set convert-meta off # mode in everything that uses readline set editing-mode vi set keymap vi
Vim has builtin spellcheck!
Today I've discovered that vim has builtin spellcheck. We're spoiled by spell checkers everywhere: Firefox has it, Thunderbird has it and it's a given that every word processor has it. But I've never used it so far with VIM. A quick search on the web lead me to a page on How to use Vim's Spellchecking Feature. Exactly what I needed!
Tabbar, better then buftabs
Lately I've started to feel that Buftabs has certain inconveniences:
- in the recommended setup it takes over the status bar
- it can't display a list of all the tabs, just what fits in a line on the screen
The status bar is not something that I really have to see, but I like to see the entire list of opened buffers, especially when editing a lot of files. Vertical space is not as important as seeing a whole list of buffers, so the buffers list can take 2 or 3 lines, if needed.
My searches led me to Tabbar, a plugin that builds on the legacy of Minibufexplorer. It solves both issues I had with Buftabs: it keeps the statusbar and it creates its own window where it displays the list of buffers. Another nice point, I can quickly change buffers using the numbers listed in the tabbar, using alt+num. The only issue I have with it right now is that the active buffer is not so visibly highlighted, but I assume I can fix this with enough care.
Go to file: really nice when editing po files
I was editing a po file, actually just checking its validity, when I saw a message id that looked strange and I wanted to check it in the source file. Well, it was a long path to the source file, but when the po file has a builtin helper for vim users, it becomes very easy. On a line like this:
#: templates_pinax/microblogging/friends_tweets.html:12
just go to the name of the file and hit gf which means "go to file", and presto! the file is opened in a new buffer.
Python autocompletion and syntax check with vim
I finally had the time and drive to make the editing of Python files easier with Vim. The main issues are syntax check (but also other general problems, like unused imports, undeclared variables, etc) and autocompletion (actually, in vim terms, omnicompletion. I had to follow two guides: the first guide uses Pysmell to enable python omnicompletion support in vim, while the second uses Pyflakes to check the file, pretty much on the fly, for common mistakes and Python programming errors.
I'm pretty excited that I got this working. Now I only need to generate the tags file, to be able to do a "go to definition" on the foreign imports. Still on my wishlist is to have an auto-import for foreign methods and classes as they are referenced in the source file (Eclipse is able to do this).
Comfortable buffer handling with buftabs
Some of the many questions that a newbie asks when starting to use vim are: how can I edit multiple files? Once I edit those files, how do I switch between them, and so on. The vim tutorial does, indeed, a good job at introducing the new users to this subject, but I feel that there needs to be more guidance on this. For example, when should one use tabs, and when should you use buffers? When using gvim, tabs are the most obvious way of handling multiple files and switching between them. But, according to some, it's also wrong, as tabs should be used as "views", a group of windows related to each other, something akin to Eclipse's perspectives. Once I've started to use the buftabs plugin, I too have come to the same conclusion.
Some details about my setup: as recommended by the documentation, I have the following settings in my .vimrc:
:noremap <C-left> :bprev<CR> :noremap <C-right> :bnext<CR> set laststatus=2 :let g:buftabs_in_statusline=1
This means that I can use the ctrl+right or left arrows to switch between buffers and that I can see a nice list of buffers in the status bar of vim. What's better, it integrates perfectly with NERDTree (which is actually why I started using buftabs in the first place).
Edit Firefox textboxes with VIM and other cool stuff that Vimperator brings
For the past months I've been using Vimperator as an interface over the regular Firefox. It is a great way to extend the vim knowledge (key shortcuts and all) outside the editor interface and into something I use even more then editing: browsing. Suddenly, all those two key commands that Firefox has have become one key, just as I like them. Closing a tab is not "Ctrl+w", it's just "d", opening one is "t", reopening a closed tab is "u", reload a page with "r", I can navigate through links using the "f" shortcut and so on. Another cool feature is that I can edit the text in textboxes in GVIM by hitting "ctrl+i". There are lots of other cool things about Vimperator and a few things I don't like about it, but overall I'm very happy I've started using it.
One finger editing files with vim
When thinking over the laptop over a piece of code, I tend do adopt this pose: supported by my left hand, using the mouse or scrolling with the arrows with my right hand. While working on "heavy" pieces of code, I tend to do very few changes in the code. VIM, being the modal editor that it is, makes it extremely easy to edit code in this posture: I only need one finger to bang the keyboard and make the small editing that I need, without leaving "my thinking pose".
Reusing the opened GVIM window when editing files
I've upgraded my Eclipse installation to the latest and greatest so I took this occasion to also cleanup the Eclipse workspace profiles and plugins by starting from scratch. This time I didn't add VIM as a new external editor because I have already made it as a default system editor for Gnome, so that I can open any file from Eclipse in an existing GVIM window just by opening with the "system editor" from the right-click menu.
To achieve this I did the following:
- make a copy of /usr/share/applications/gvim.desktop in ~/.local/share/applications
- edit this copy to have a command of gvim --remote %F instead of the default. This make vim reuse an existing window when editing the file
- edit defaults.list in the ~/.local/share/applications folder. Right now I have the following content, which associates gvim with several relevant mimetypes:
[Default Applications] text/plain=gvim.desktop text/xml=gvim.desktop text/x-python=gvim.desktop text/html=gvim.desktop
References:
Using NERDTree, buffers and BufTabs to edit files
NERDTree, sessionman and the BufTabs plugin make for a good part of an IDE together, but there's one problem: when deleting a buffer with :bd, it closes the NERDTree split window, and that's not what I want. A little bit of researching on Google came up with the following recipe to close files without quiting VIM which I have changed to take into account that the buffer tabs need to be updated (and also to use noremap instead of map, so you don't have to hit Enter after using the fc/fq shortcuts):
noremap fc <Esc>:call CleanClose(1)
noremap fq <Esc>:call CleanClose(0)
function! CleanClose(tosave)
if (a:tosave == 1)
w!
endif
let todelbufNr = bufnr("%")
let newbufNr = bufnr("#")
if ((newbufNr != -1) && (newbufNr != todelbufNr) && buflisted(newbufNr))
exe "b".newbufNr
else
bnext
endif
if (bufnr("%") == todelbufNr)
new
endif
exe "bd".todelbufNr
call Buftabs_show()
endfunction
After adding the above lines to my .vimrc, I can use "fc" or fq to close the current opened file and delete it from the buffers list.
View local changes made in a file
Eclipse and, in a way, TortoiseSVN allow viewing the local changes in a file before you commit it for eternity, in the repository. But I wanted a way to view just the changes I have done to the file, before saving it. Fortunately VIM provides this in a nice and easy way, as it was pointed to me by someone on irc://vim@freenode.net. You can define a new command, DiffOrig, which splits the current buffer window and shows you the diffs to the previously saved version.
The command to add in your vimrc is (according to the 'diff' chapter in the help file):
command DiffOrig vert new | set bt=nofile | r # | 0d_ | diffthis | wincmd p | diffthis
To use it, just do :DiffOrig in the desired buffer window.
Running VIM on Windows and the beginnings of an IDE
I'm testing a wxPython application on Windows image running inside a VirtualBox environment. I didn't want to be bothered with setting up a full Eclipse instalation, so I've started the development in GVim (the edits I need to do are short so I don't need to depend on the full IDE). So far, I have installed and used the following scripts and plugins:
- EasyGrep, to search through the "project". I like the quick search based on the current word (\vv) and the scratch and options window, accessible with the :GrepOptions command
- NerdTree, although there are numerous alternatives. VimCommander is another alternative that I've been using. I have created a bookmark to the root of my project and I use "cd" to change the current directory to it (for use with EasyGrep). I open the files in new tabs (using t, or T, if I want them opened in the background). I like the fact that the basic info for the commands are easily accessible (using ?), and it can be configured to filter files (for example, I don't want .pyc files showing up).
- sessionman, to save the opened files for quick access. SessionSave, SessionList, etc. It just works.
Now, on the topic of Windows development: for me, this desktop environment is clearly not as productive as Linux. The lack of desktops and the stupidity of the open dialog (no bookmarks!) makes it cumbersome. There is a bug in the vimball plugin, which means I also can't install vimballs and I have to hunt for the source or zips. The console (cmd.exe) is plain stupid and the console2 replacement is no match to Konsole or yakuake. The default gvim package doesn't come with python bindings (autocompletion doesn't work), so I'll have to hunt for one that does, if I want more advanced integration with python.
Configuring gvim as an editor for Eclipse
Not until long ago, I didn't knew how easy it is to configure Eclipse to have gvim as an editor. I've relied on associating GVIM as the default editor for the system and relied on opening the files with the "System Default" in Eclipse. The steps to add Gvim as an editor are quite easy:
First, navigate to Window > Preferences > General > Editors > File associations. Here, either add a new file extension or edit an existing one.
Add a new external editor in the Associated editors region. Click Browse and search for the GVIM binary. Optionally, you can make GVIM the default editor.
A rant over the faults of Eclipse as a good text editor
Right now the editor of choice for my daily programming is Eclipse + Pydev, mainly because of the superb job Pydev is doing as a Python IDE. With Pydev I get:
- syntax highlight (including highlight of syntax and programming errors)
- code formating
- auto-import
- import declaration sorting
- syntax check
- intelligent autocomplete
- go to definition
- management over a project's working set of python packages (for autocompletition)
- ability to comment/uncomment code
Various plugins for and builtin capabilities of Eclipse will give me:
- XML editing (for Zope Page Templates) - this is through the WTP
- syntax highlight
- syntax check
- code formating
- ability to toggle comments
- CSS editor (code highlight, autocompletition, code formating) - thanks to Aptana
- Javascript editor
- Another XML editor
- Integration with SCM (in my case subversion)
- Project and file management
Despite the fact that the Eclipse setup covers all my development needs, I am unhappy with it, mostly due to the faulty and annoying XML and CSS editors from Aptana and the Web Tools Project. I edit just as much templates as I edit Python code, so a good XML, JS and CSS editor is a must. The most important step towards getting rid of those broken editors is to use GVim as the text editor, which I've accomplished today by following this advice on how to associate GVim in Gnome with various filetypes.
The main issues that I'm unhappy with Aptana and Eclipse WTP are:
- the CSS and XML editors in Aptana don't have the Toggle Comments comment
- the CSS Editor in Aptana does a crazy job at formatting the code
- I can't change the settings for the Aptana XML Editor formatting settings (I can change them, but they won't have any effect)
- the XML editor from WTP is doing a miserable job at formatting text paragraphs (splitting, in some cases, long lines of text into lines with just 2 words on them, even if there's a ton of space to the wrapping border)
On the other hand, Pydev is briliant in its Python integration so I'll need an environment in vim that will allow me to be just as productive editing Python files.
Introduction
Hi! My name is Tiberiu Ichim, I'm a Python web developer (with a preference for Zope and Plone). For some time I've been using on and off Vim, but lately I've been looking to migrate all my development tasks to Vim. To help me track ideas and have a "scratchpad" to hold my discoveries, I've started this blog. I hope it will grow into a useful resource for me and others.
The software used for this blog is called "Plone", a full blown CMS with a lot of extensibility, one of my specialties as a programmer. It's featureset as a blog might not be up to the standards of other blogging software, but it has a very good rich text editor, I'm very familiar with it and I already have the infrastructure to host it for it, so I'll be using it instead of others. First thing on the todo list is to make sure that my blog entries are available for comments.
Also, I'm not a native English speaker but I'll try my best to make as few mistakes as possible.
