Posts Tagged ‘ vim

Oops, I forgot to sudo in Vim

It’s a huge annoyance when I’m hammering away in Vim, being extremely productive as always ;) , and I get to a proper saving point and I get the ‘readonly’ option is set (add ! to override) message.  This message drives me bonkers because it means I am going to have to take a few extra steps in order to save my hard work. Whether it be a quick copy-paste-copy-paste, or whatever method you prefer, it is still a very annoying process that I wish there was a quick fix for.

Lo and behold, there is a knight in shining armor for this very issue. Meet my friend “tee”. Since Vim has the ability to run commands from within itself, we can use tee to take our file from Vim and write to the file with sudo, which is what we forgot to do in the first place. All you need to do is place this small snippet in your .vimrc:

cmap w!! w !sudo tee %

What this does is map the vim command :w!! to the command that follows it, which uses tee to take the file from stdin and write to the file with root privileges. Once the command runs you can reload the file you were writing, although you will still not have write privileges so every time you want to write you can just use this command again or drop out real fast and open up Vim with sudo. Not a perfect solution, I should probably just remember to sudo when I need to :p , but it works in a pinch and makes my file editing slightly less annoying.

How To Remap Caps Lock To Escape

So I think we can all agree that whoever decided the caps lock key should get prime real estate should be stoned.  Seriously, it’s right where an extremely useful key could be placed…like escape!  Keys that can be toggled on and off, especially by software means, should not be given such huge buttons placed where other useful buttons should be in the first place.  I am a heavy Vim user, so I use escape A LOT to change modes.  If you’re an emacs *shuddersss* user you can remap this key to control but I am not going to tell you how as you should be using Vim anyway :p  I’m just kidding, it’s a simple word substitution I will show in a minute.  In order to get this setting to effect both console and X usages we will need to edit two different sets of files, both of which I will demonstrate.  This is a Linux/UNIX specific tutorial so apologize to you poor Windows and OS X users who are stuck out in the cold.  If it is demanded, I can show how to do this in OS X as it is a pain in the ass, but unfortunately I have no idea how to do this in Windows.  Sorry about your luck   :-|   Anyways…

In order to get this to work in the console for all users, stick this in your /etc/rc.local (this is an Arch Linux example, you just need this to be in a file that will be ran as root during startup):

(echo `dumpkeys | grep -i keymaps`; echo keycode 58 = Escape) | loadkeys -

This should make it so Caps Lock is remapped to escape at all times in the virtual consoles. If you wanted it to be remapped to control, just substitute “Control” for “Escape” there towards the end…simple enough.  Or you could just use Vim :)

In order for this to take effect in X, I use Xmodmap to remap the keycodes. The way I do it is to stick these two lines in the file ~/.Xmodmap:

remove Lock = Caps_Lock
keysym Caps_Lock = Escape

Then in my .xinitrc have a line that says:

xmodmap /home/al3k/.Xmodmap &

You can run that command in the console and, since it will be run at every boot, it will be a persistent setting. Having escape at a much more reachable position makes productivity soar…at least for me. It allows me to Vim and Vimperate at a much faster speed. Hope this helps! Post comments if you have any questions, I am happy to help.