Category Archives: tools

make vim awesome with plugins

.vimrc

Vim is a great lightweight editor as it is. But after setting it all up on your windows box and tweaking your _vimrc it still might lack that bit of awesome you’re looking for in a modern text editor.

Time to spice things up with plugins!

Vim plugins are written in viml or vimscript, an internal script language in the vim editor, and are plain .vim files containing scripting code which extend vim in all sorts of wonderful ways. There are tons of vim scripts out there so finding the right ones for your needs takes a bit of time. There are however some helpful guides out there and blog posts like this one to help you on your way. I’ll list some of those and links to more plugins at the end.

Installing those scripts and plugins can be tedious though. Download a zip, unpack, copy files, yada-yada-yada. Since we’re into package managers these days we want things to go automatically with a few keystrokes.

Enter Vundle.

You’ve probably guessed by now this is a vim plugin manager (and a plugin by itself). It allows you to install, update and search for available vim scripts among other things. I like this one in particular because it does this all from vim itself with a number of specific commands.
To get started you’ll have to install this one manually though, but it only takes a few command line statements and some .vimrc edits. Once you have this up and running, you’ll be able to install most plugins using it so it’s worth the hassle.

Check out the info on the Vundle github page on how to install and then come back here. ;)

Alright. Now, what plugins should we get?
Well it depends on what you want to do of course, but here are some general purpose ones you might like.

vim-airline

A pretty looking status bar you’ll see in a lot of vim screenshots. It’s tweakable so you choose what kind of info you want it to show.

vim airline status bar plugin

ervandew/supertab

Adds tab completion to vim using the tab-key. That might sound odd but the default use a bunch of control keys so this just comes more “natural”.

The-NERD-tree

This is a directory browsing plugin which is just better than Netrw which comes out of the box. Visually you can fold/unfold folders, search (use any vim command in the window), manipulate files etc. Very handy to keep track of a project when programming or just see what other files are in a folder without having to exit vim.

vim nerdtree plugin

CtrlP

A fuzzy file search plugin. Press Ctrl-P and you’ll get list at the bottom of files in your current directory. Type in some characters of the filename you are looking for and it will filter the files matching those characters. So you don’t need to know the full name, and you can skip parts. Check out this video to get an idea of how it works.

One note on this. If you have a folder with a lot of files in the sub-folder tree (like a C# application with build files in the sub-folder) be sure to exclude any non relevant types like object & dll files. CtrlP has a maximum file limit and those irrelevant files can stop you from finding those you actually want to see.

In my _vimrc I use this to exclude the .NET build artifacts and some more irrelevant file types:

set wildignore+=*\\obj\\*,*\\bin\\*,*.swp,*.zip,*.exe,*.dll

vim-fontzoom

vim-fontzoom is a simple plugin that allows you to increase or decrease your vim font size using the plus or minus key when you are in command mode. Note that this doesn’t work with the +/- on your numeric pad, just with the regular keys on your keyboard. But you can remap the keys if you want to change this.

chriskempson/base16-vim themes

Not really a functional plugin but hey, you want your editor to look pretty right? I’ve tried a ton of themes already but lately I’m sticking with the chriskempson/base16-vim set. In this package you get a bunch of nicely crafted and balanced color themes which will definitely have something you like. Dark and light themes, monokai, solarized and other classics, it has it all. The last theme plugin you’ll ever need.

Moar!

Depending on you workload there are plenty of more specific plugins out there. Google is your friend, but here’s a few places to get started:

  • The easiest to use and most awesome Vim plugin directory is called VimAwesome. Great to find new plugins, or great to find old ones and how to install them. Each plugin lists how to install it with Vundle or another plugin manager, which is super handy.
  • The 15 best vim plugins according to Steve Francia who made a vim distro called The Ultimate Vim Distribution, so I guess he knows what he’s talking about. :)

 

indent selected lines with tab in vim

.vimrcOne thing that annoyed me about using Vim was how much keystrokes it took to indent or un-indent a few selected lines of code. My (probably less than ideal) way of doing that was to go into visual mode, select the lines with the movement keys J or K, then use the keys to change the indenting which are < or >.
To indent another level, pressing dot after this would work.

In Visual Studio or a typical Windows text editor I’m used to simply selecting the lines by holding shift & moving the cursor keys up or down, then pressing TAB to indent and shift-TAB to un-indent.

I’m so used to using the cursor keys for text manipulation that it’s hard to unlearn this, so I was looking for key mappings to do the same thing in Vim.
Luckily this turned out to be rather easy. If you add the following to your vimrc file, you can shift-tab away to indent your code:

" TAB-mappings to allow indenting of selected text instead of using < & >
vnoremap <Tab> >
vnoremap <S-Tab> <

chocolatey package update quick reference

gingerbread2011_18

Chocolatey rocks when it comes to updating a bunch of installed software from the command line. If you’re not doing that often however it can be hard to remember exactly what commands you can use to do that quickly. So here’s a little run-down on the most helpful commands when you are updating your system.

First you might want to check what’s installed on your machine.
You can get the list of the local package Chocolatey installed like this:

chocolatey list -localonly

or in short:

clist -localonly

To check if any package have updates available, we can run the update all statement, but not quite for real yet. By adding the -whatif switch, Chocolatey only pretends to update:

chocolatey upgrade all -whatif

or:

cup all -whatif

Ready to update all packages at once? Nice. So let’s disable those confirmation prompts while we’re at it too by adding the -y switch.

chocolatey upgrade all -y

or

cup all -y

Edited 2017-02-26 : replaced deprecated update command with the new Chocolatey 1.0 upgrade command.

Photo by elidr, cc-licensed.

the ultimate android space clearing guide

If you’re running into that annoying problem where you can’t install yet another awesome app on your Android phone because you are running out of space, here’s the ultimate guide to freeing up app space *dramatic music*

1. Clean up app cache

Bansky street cleaner - Chalk Farm

Your phone stores apps on its internal memory card (not RAM, but the disk) including some temporary data for each app. That cached data is the first thing you can go and remove to free up space. Depending on the app, this can be quite some data. Think apps that download resources like video or images, create thumbnails etc.

You can do this manually with the internal app settings screens and go over each app individually. I bet you have better things to do though. Instead you can install ES Task Manager, which has a cache cleaner built-in and does the job for you. Sweet. There are plenty of alternative cache cleaning tools available if you don’t like the ES one.

2. Move apps to the external memory card

Still not enough space? Damn. To free up space on the internal memory card, you can also move some apps to the external card. If you have that option, you can use the application tools to move apps individually. Not all apps support this and it usually doesn’t free up all the space either. There’s always some core files that stay on the internal drive, so don’t expect any miracles. The best way to go about it, is to sort the apps by size and try to move the biggest ones first. But if that doesn’t do the trick you might want to…

3. Uninstall some apps

Yep. Makes sense doesn’t it. The bigger the better too. It sucks, but there’s probably some stuff in there you haven’t used in months. Time to say goodbye and press the delete button. Aah, instant free space.

4. When all else fails.

Still not working? I had that. My internal memory was showing 250MB of free space and I couldn’t get a 40MB app like Chrome to update anymore. Same thing with any other app around that size. They all failed to update.
It doesn’t make sense when you look at the numbers, but my guess is that it’s like with a fragmented disk drive on a PC. At some point there isn’t a large enough open space to fit the update file in one piece. Or that 250MB of free space isn’t just for apps. I’m not sure. But what I am sure is that resetting the phone wipes the internal disk space, and frees everything up again.

Photo by Dan Brady, cc-licensed.

configuring vim: tweaking your _vimrc file

.vimrc

In my earlier post on setting up Vim on Windows I already talked about a few vimrc changes to make vim behave on windows and look a bit better.
I learned that checking out other people’s vimrc files is a great way to learn how you can configure vim to become the kick-ass text editor you always wanted. In this post I’m going to run over the settings I have in my _vimrc file to make vim work better for me. Feel free to copy bits your find useful.
If you don’t know what that vimrc file is, or how to edit it, check out my previous post on how to set things up.

I tend to document my vimrc changes with some comments. That way I still know what I was trying to do months later and or what lines can be removed when a wicked plugin makes them obsolete. Most of it is self-explanatory, so I’ll just paste them right here.

First some system settings.

" Activate all the handy Windows key-bindings we're used to.
source $VIMRUNTIME/mswin.vim

" Have gvim behave properly on Windows.
behave mswin

" Use unicode/utf-8 encoding by default for keyboard, display and files.
set encoding=utf-8

" Set a more convenient leader key on an AZERTY layout than the default backslash
let mapleader = ","

Now let’s make things prettier.
First of all, we need to set a nicer fixed-width font, Consolas in this case. Then I hide the toolbar which I never use. Then, of course, a nice theme.

if has("gui_running")
  " Set a nicer font.
  set guifont=Consolas:h11:cDEFAULT
  " Hide the toolbar.
  set guioptions-=T
endif

" Set theme.
color badwolf

Then a number of visual things to make programming, editing and searching text easier.

" Display line and column number in bottom ruler.
set ruler
" Display the line numbers.
set number
" Turn sounds off.
set visualbell
" Shows a horizontal highlight on the line with the cursor.
set cursorline

" Activate highlighting search pattern matches & incremental search.
" Incremental search means your cursor will jump to the first match as you
" type.
set hlsearch
set incsearch
" Allow using  to kill the current search highlighting.
nnoremap   :nohlsearch

" Activate case-insensitive & smart case search (if a capital letter is used
" in your search query, Vim will search case-sensitive).
set ignorecase 
set smartcase

" Set wildchar visual completion awesomeness.
" This is enhanced command line completion and it rocks.
set wildmenu 
set wildmode=full

" Turning on line wrapping and line-break for easy text-file editing.
" Line-break wraps full words at the end of a sentence for readability.
set wrap
set linebreak

Programming settings. Who would have thought!
Tweak tab settings according to your religious preference of course. I can’t say I have used the folding a lot, but these settings come from another .vimrc somewhere and they seem sensible.

" Activate syntax highlighting.
syntax enable

" Set tabs to 4 characters and expand to spaces, activate smart indentation.
" See tabstop help for more info.
" Setting tabstop & softtabstop to the same value to avoid messy layout with mixed tabs & spaces.
set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set smartindent

" Enabled folding on indent level. That way it works on any code & html, xml
" etc. 
" Setting foldlevelstart ensures that for newly opened files folds are open
" unless they are 10 levels deep.
set foldmethod=indent
set foldenable
set foldlevelstart=10
set foldnestmax=10      " no more than 10 fold levels please

At the end of my vimrc I add my custom tweaks. They might not be useful for everybody, but they work well for me.

First, some syntax highlighting for file types vim doesn’t know about by default for .NET development.

" Set syntax highlighting for some .NET file types as XML files, cause that's what they are really.
autocmd BufNewFile,BufReadPost *.config set filetype=xml
autocmd BufNewFile,BufReadPost *.csproj set filetype=xml
autocmd BufNewFile,BufReadPost *.sln set filetype=xml

Then, some key mappings to access plugin features or plain vim commands quicker.

" Add a mapping to open a new tab with CTRL-T.
map  :tabe 
" F11 toggles menu visibility
nnoremap  :if &go=~#'m'set go-=melseset go+=mendif

" cd sets path to the path of the file in the current buffer.
nnoremap cd :cd %:p:h
" Open the NERDTree on the path of the file in the current buffer.
nnoremap t :NERDTree %:p:h

That’s about it. I hope this helps getting your own .vimrc set up the way you want it to. I’m not a vim expert so there are probably better ways to do some of these things. I you know how, feel free to let me know in the comments. I’m eager to learn some nice vim hacks.

As I said before, don’t just copy paste stuff in your vimrc you don’t understand. Take a look at the vim help first. Also check out other people’s vimrc and see how they do things. There are plenty of good blog posts around on the subject and a github search turns up some good stuff too.

setting up vim on windows

So you want to make the plunge and check out what that awesome Vim editor is all about on a Windows machine? I did about a year ago, and this time I actually stuck with it. I tried it 2 times before already, but I gave up every time after a few hours or days because of the steep learning curve and the fact that nothing seemed to work the way I’m used to.

The goal here is to get Vim set up on a Windows box, make it work like a Windows app and make it look like something you aren’t afraid of being seen with (that default layout, euhm, seriously…).

installing Vim on Windows

The easiest way to install gVim (the non-console version) on Windows is by using the Vim Chocolatey package. It installs the Cream Windows flavored Vim for you from Sourceforge and sets up your system path so you can run “vim” and “gvim” to start the editor from the command line if you want to.

Chocolatey itself is like apt-get for Windows. It’s a NuGet based software package manager and if you’re a software developer or have to install systems a lot, I’m sure you’ll love it.

To install using Chocolatey, type this from a command shell (run in administrator mode):

    choco install vim

Easy isn’t it?
If you really don’t want to setup Chocolatey (but you should really) you can also get the Windows binaries from vim.org, or the Cream ones. You’ll get the Cream editor with that based on Vim, but I didn’t want that cause it’s not quite pure Vim anymore (not modal for example).

first steps

Before you start configuring Vim you should start with getting to know it. Whatever comes next will be a lot easier once you know how to move around, save files etc. You know, those typical things you do in a text editor anyway.
So basically you have to RTFM. Or at least, the short version of it.

Find the Vim tutor in your start menu and run it. Once you’ve completed that, you know enough to get around in Vim and find out how to get help for other stuff. Also, don’t bother with doing it the hardcore way. Just learn Vim at your own pace, use the cursor keys and the mouse if you want. You’ll pick up more advanced stuff as you go along.
Just like this guy said.

configuring Vim

Vim uses vimrc configuration files to store settings. There is a default vimrc file in your Vim installation folder, but you shouldn’t touch that one because it can be overwritten when you install a Vim update. Your own settings go into a new _vimrc file containing only the things you want. You can “source” or include other vimrc files, to avoid duplication or organize your configuration.

On a Windows machine this goes into your user folder (c:\users\username\_vimrc).

To see and edit your current vimrc (the one use by vim when it starts) type:

    :edit $MYVIMRC

Once you’ve created your own config file, the above command will automatically open it. So let’s do just that, like this. “e” is short for “edit” btw:

    :e ~\_vimrc

That tilde character (~) stands for the path to you home folder, which on Windows is normally set to c:\users\<username>.

On Windows the folder  ~\vimfiles is used to store custom plugins. I put my vimrc in ~\vimfiles and source it from the ~\_vimrc file. That way I easily sync my whole setup to another machine by simply copying the whole vimfiles folder. So my ~\vimrc file contains only this line.

source ~\vimfiles\nj-vimrc.vim

You can keep everything in _vimrc of course if you want.

You can test Vim statements from the command bar before you put them in your vimrc file. A handy way to test and see if it really does what you want it to without having to restart Vim and reload your configuration.

making Vim behave like a Windows editor

If you install the Windows gVim version this will normally be setup in your vimrc configuration file automatically, but it’s always handy to know what settings are making the magic happen.
Vim doesn’t use the typical Windows key binding like CTRL-S to save, CTRL-C,V,X to copy, paste and cut. As a hardcore Windows user you expect these to work for all applications, no matter how geeky they are. If you check out the original vimrc in the Vim installation folder you’ll see the same statements as below.
You can copy these into your own vimrc file.

" Activate all the handy Windows key-bindings we're used to.
source $VIMRUNTIME/mswin.vim

behave mswin

Or you can source the original config in yours:

source $VIM/_vimrc

Now your Vim should (still) behave pretty much like a regular Windows editor, making things a lot easier to use it for all your basic editing.

making it look good

gvim_fugly

Man, it does look ugly doesn’t it? All white and with that terrible font? Now let’s make it look pretty so you don’t have to be ashamed of all those hipsters sporting their fancy Sublime.

This depends on your personal preference of course, but I like it when my editor doesn’t look like it’s from the 80’s. I pasted some of my esthetic config changes below. Each has some comments on it explaining what they do. Use the help feature (:h ) if you want to find out more, and remember to only put things in your vimrc that you understand.

" Display line and column number in bottom ruler.
set ruler

" Display the line numbers.
set number

" Activate syntax highlighting.
syntax enable

" Set a nice theme.
color slate

Check out all the themes you have installed by using CTRL-D (auto-complete) after you typed in :color on the command bar.

if has("gui_running")
  " Set a nicer font.
  set guifont=Consolas:h11:cDEFAULT
  " Hide the toolbar.
  set guioptions-=T
endif

gvim looking pretty

You can exclude hiding the toolbar, but I just find it ugly and never use those buttons anyway. This is very minimal, and there are plenty of other themes and plugins available to change Vim’s appearance, but that’ll be for another post. For more vim customization tips, check out what I have in my _vimrc file.

Hopefully this gets you going in the wonderful world of the Vim editor on Windows. Just keep at it I’d say and feel free to drop any questions or remarks in the comments.

Edit: replaced the KicksassVim Chocolatey package with the Vim one because the first doesn’t seem to be updated any longer.

how to update chocolatey

I use Chocolatey all the time to quickly install software on Windows machines. At some point Chocolatey itself also gets an upgrade, which just happened recently and then I can never remember how to get Chocolatey itself upgraded.

It’s in the documentation somewhere I’m sure, but since Chocolatey is about easily installing and upgrading Windows software, it was bound to work “recursively”.

So here’s how you do that from a Windows shell prompt, for (my) future reference:

    c:\> chocolatey upgrade chocolatey