I tend to use Vim as my editor of choice. Even when using Visual Studio, I do so with the ViEmu plugin. I have always wondered why the directional keys were hjkl instead of jkl;. The latter are the home keys for the right hand. The former are not and overload the right index finger. Thanks to Hacker News, I now know the answer. The terminal which Bill Joy wrote the original Vi on was an ADM-3A and that had arrows drawn on the hjkl keys (see the link for a picture).
Friday, March 9, 2012
Saturday, June 21, 2008
Vim Tip: My .vimrc File
.vimrc is the config file for Vim. It can be stored in the Vim directory or in your user directory. With greater protection of the Program Files directory on Vista, I've gotten into the habit of keeping it in my user directory (c:\users\steverowe on most systems). here are most of the non-standard options I have in my .vimrc file. Maybe you can pick up some pointers.
"set tabs to 4 spaces.
set tabstop=4
set expandtab
set shiftwidth=4"automatically indent
set smartindent
set cindent"case insensitive search
set ignorecase
set smartcase"search the whole build tree for ctags
set tags=tags;/"open the window larger than normal (100 wide by 40 tall)
win 100 40set nocompatible
"allow for c,w to change part of a camel-cased word
source $HOME/camelcasemotion.vim"make it so mouse and keyboard don't exit select mode."
"this makes it so we can select with the mouse and then act on that block."
set selectmode="""make long-line navigation work like other windows apps
map <DOWN> gj
map <UP> gk
imap <C-UP> gki
imap <C-DOWN> gji"from here down is the default _vimrc
set nocompatible
source $VIMRUNTIME/vimrc_example.vim
source $VIMRUNTIME/mswin.vim
behave mswinset diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let eq = ''
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
let cmd = '""' . $VIMRUNTIME . '\diff"'
let eq = '"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3 . eq
endfunction
Saturday, June 7, 2008
Vim Tip: Better Searching
Two things I discovered about searching:
* will search forward for the word under the cursor. No longer do I have to type /WordUnderTheCursor.
# does the same thing, but goes backwards. It's like ?WordUnderTheCursor.
g* will search for any word containing the word under the cursor. So g* on the word count will find count, GetCount, Counter, etc.
g# is the same thing in reverse.
Saturday, May 31, 2008
Taking Advantage of Vim
Once you have mastered using Vim to replace Notepad.exe, it is time to starting taking advantage of what Vim has to offer. Doing so can increase your productivity. Below are most of the commands I use most frequently. It is important to note that Vim has a veritable cornucopia of commands and thus supports many different usage styles. What is presented here are just a fraction of the available commands.
Many commands in Vim have a similar format. This format is [repetitions][command][movement]. For instance, 2dw will delete the next 2 words.
Useful commands that take this format include:
Command | Action |
c | Change. Delete the text moved over and enter insert mode. |
d | Delete. Delete the text moved over. |
y | Yank. Copy the text moved over. |
No number implies 1. The combination cw will delete the rest of the word under the cursor and enter insert mode. This is probably the most commonly used combination for me.
There are two more variants on these commands. A capital letter will execute the command to the end of the line. D will thus delete the rest of the line. Two repeated letters will execute the command on the entire line. Thus cc changes the whole line and 2yy yanks the next 2 lines.
Common movement commands include:
Movement | Action |
w | word. A single word. |
b | word back. A single word, backward. |
) | sentence. A single sentence. |
h | left. One character to the left. |
j | down. One line below. |
k | up. One line up. |
l | right. One character to the right. |
Movements can be issued without a command in front of them. 2w will move the cursor 2 words to the right. You can maneuver all over the document using hjkl instead of the cursor keys if you like. Personally, I use the cursor keys and only use l when I want to change some number of characters. 5cl will delete 5 characters and enter insert mode.
Some commands operate without a movement. These include:
Command | Action |
x | Delete one character |
~ | Change the case of one character. |
r | Change one letter and return to normal mode. |
These commands can accept a number. Thus 5x will delete the next 5 characters.
Other useful commands:
:<line number> | Jump to that line number |
% | If on top of a ( or {, jump to the matching one. Very useful in coding. |
m<letter> | Sets a bookmark labeled <letter> at the cursor. |
'<letter> | Returns to <letter> bookmark. Useful for jumping around a file. |
u | Undo the last change. Useful if you forgot you weren't in insert mode. |
gg | Go to the top of the file. |
G | Go to the bottom of the file. |
>> | Increase indent. |
<< | Decrease indent. |
o | Insert a new line below and enter insert mode. |
These commands, in addition to the search and replace command introduced in the notepad post are the ones I find myself using most frequently. Once you become accustomed to them, editing in something without them will feel strange. I want to hit 2cw to change the next 2 words in Word all the time. I find the /search very useful also and miss it whenever editing in something that does not have Vim keybindings. My repertoire is fairly limited presently. As I discover new ways of doing things in Vim, I'll post them as Vim tips. If there are commands you use frequently that I don't discuss above, please mention them in the comments.
Saturday, May 24, 2008
GVim As Notepad
When I first encountered Unix in the early 1990s, I needed a text editor. I tried Emacs but the meta key and double control keys struck me as wrong. I tried Vi but couldn't figure out how to type anything. I came across Pico and for years used that as my editor. Why? I wasn't doing much other than modifying some shell scripts or dot files and it was easy. All the commands I needed were listed at the bottom of the screen. Pico is the Notepad.exe of the Unix world. Years later I was forced to do some programming on a Linux system via an ssh connection. Needless to say, Pico isn't really a great programming editor. I had a friend who was really excited about Vim (Vi Improved) at the time so I gave that a try. Emacs still didn't sound inviting.
I began by reading tutorials on the web. They all intended to get me up to speed on all the nifty functions in Vim very quickly. For instance, most tutorials start by teaching that hjkl are the cursor keys. They are, but you don't really need to know that for basic functionality. The ones with arrows on them will work just fine for most purposes. Options like this serve just to confuse at the beginning. The trouble is, I can only learn so much at once. Before I could learn how to run, I had to learn how to walk. I needed to be comfortable doing the basics before I could grok the extra stuff. I needed to be able to use Vim like notepad functionality before I could move on to various visual modes, regular expressions, and about 80 billions ways to get around the screen.
This then is the tutorial I wish I had found. It is how to use GVim as Notepad. GVim is the graphical version of Vim. It's the same as the shell-based version, but you get to use your mouse and there are menus. What I am about to present doesn't show any of the power of Vim, but it will help you become comfortable enough that the myriad other tutorials on the web can be of assistance.
Modes
The first thing to understand about Vim is that it is modal. Most editors are modeless. Letters are entered by typing them. Control keys do special things. This is true all the time. Control-S will invoke Save in notepad no matter what you are doing. Not so in Vim. Vim has different modes and different commands will have a different effect depending on which mode the editor is in. It is important to keep in mind which mode Vim is in. For now there are 2 modes you need to know about.
First there is "normal" mode. Vim always opens in normal mode. This is the mode where you enter commands. Whether you want to save a document or change the case of a letter, normal mode is where you want to be. The confusing part is that in normal mode, you can't actually add text to your document. If you start typing in a blank document, not much will happen. If you start typing in a full document, all sorts of strangeness will. In Vim's normal mode, nearly every key on the keyboard is a command. See this graphical cheat sheet if you want to see them all.
Insert mode is probably what you are familiar with. It's just like being in Notepad or Pico or even Word. Insert mode works just like you expect an editor to. The letters you type show up in the document as text. Cursor keys or the mouse can be used to move the cursor around. To enter insert mode, just type i. The cursor will change from an XOR cursor to a bar cursor. The word -- INSERT -- appears at the bottom of the screen when in insert mode. If you are new to Vim, this is the mode you'll spend the most time in.
Esc will exit insert mode and return to normal mode.
Basic Editing
If you are using GVim, editing is simple. Just enter insert mode and it works the ways Notepad works. You can select text with the mouse, cut with <ctrl-c>, paste with <ctrl-v>, cut with <ctrl-x>, and delete with the del key.
To save your work, hit esc to go to normal mode. The combination :w (write) will save the document. Many commands in Vim are prepended with a colon ':'. There are good reasons for this, but we won't go into them now. Just accept that they are needed to increase the number of available commands.
Exiting Vim can be done via the menus or by issuing the :q command from normal mode. If you want to quit without saving, :q! will accomplish that.
To load a new file, I recommend using the menus at this point in your Vim career. If you must, :e filename will load filename.
Search & Replace
Often times you'll need to find something in your document. To do so, enter normal mode (hit esc). Type / following by the word you are looking for. For example, /Steve will find the next time Steve appears in the document. As you type, you'll notice two things. First, the cursor immediately jumps to the first S following the cursor, then to the first St, etc. It's doing an incremental search as you type. In this way, you can stop when you have reached the desired location. No need to type the whole word. To stop, just press <enter>. The other thing you'll notice is that all instances of the word Steve have been highlighted. This makes it easy to see all the places in your document where the word appears. To go to the next instance, press n while still in normal mode.
To replace some text, say the word "from" with other text, the word "to", do this in normal mode: :%s/from/to/g. :%s means substitute on every line of the document. The /'s separate the parts of the command ala regular expressions. The trailing g says to replace all instances on each line.
If you already search for the word you want to replace, the syntax can be simplified by leaving out from. Thus :%s//to/g will replace whatever was last searched for with "to".
Conclusion
This should be enough to get you up and running in Vim. You won't notice any advantage from Vim yet, but you will be able to edit. Once you are comfortable with GVim as a Notepad alternative, you'll be ready to learn the parts that make Vim such a great editor. I have a post to help with that here.
For the Non-Visual User
If you happen to be using Vim from a command-line instead of GVim, a few more commands will be necessary for basic operation.
To select text, hit <ctrl-v> to enter visual mode and use the cursor keys* to select the text you want to operate on. If <ctrl-v> does a paste, you can use <ctrl-q> instead.
To cut or delete the selection, press x.
To copy the selection, press y (yank).
To paste a previously cut or copied selection, go to where you want to insert it and then (from normal mode) press p.
----
*This won't work in GVim. If you know how to make it work, let me know.
Saturday, May 17, 2008
Vim Tip: Finding a Variable Declaration
For most programming, I use Vim as my text editor of choice. It has a steep learning curve, but is very efficient once you get accustomed to it. I'm far from an expert on Vim and I'm always learning new things. From time to time I will endeavor to post about these tips so others can benefit from my learning.
Often times when editing code in a function, one runs across a variable that's type isn't immediately obvious. Here's a few navigation techniques I just discovered to help find out what the variable is:
gd - go to declaration. This starts at the top of the function/method and works down until it finds the declaration of the variable the cursor is on. Note that this will also highlight all instances of the variable, making it easy to pick it out in a crowd.
g, - go to last edit. When using something like gd, this helps you get back to where you were. g; will go forward to the "next" edit if you need to bounce between locations.
Monday, January 29, 2007
Contemplating ViM
A conversation with a colleague got me interested in ViM once again. I've used ViM on and off for a few years now but never gotten really adept at using it. For those that don't know, ViM stands for Vi Improved and Vi is one of the two dominant text editors in the *nix world (the other being emacs). Vi was written by Bill Joy. It is hard to learn but very powerful once you put in the time. ViM is the most popular of the modern incarnations of that editor. I may have to dust off my installation and try to master it once again. In my newfound interest, I located a few things which I thought I'd pass on.
First off, I figured out a good way to get the 64-bit version of ViM installed on a x64 Vista. You can install the 32-bit version but tell it to go to the x64 "program files" directory on install. This will set up all of the shortcuts and shell extensions will later need. Then you download the x64 build of ViM from here. Finally, you just unzip the contents of that package over the top of the previously installed ViM. Done. So far it seems to work fine.
Next, I found a few useful documents to help me get back into the habit. The first is this graphical cheat sheet. It's a bit hard to print as I had to download Inkscape and play with the size a bit. Once printed though, it is a great way to quickly find the needed keystrokes to accomplish most tasks. The most frustrating part about using ViM is knowing what you want to do but being unable to locate the right keystrokes.
I also ran across this tutorial called Efficient Editing with ViM that laid out the key ViM instructions better than most. I've read a lot of ViM tutorials and they tend to be hard to follow. They probalby make sense if you already understand ViM but they are hard to approach as an outsider. This one does the job better than most. Either that or I'm an insider now...
Finally, I've been hearing good things about ViEmu which is a ViM-like editor which works inside Visual Studio. No longer is one required to give up Intellisense to edit code efficiently. I haven't tried it yet but I'm about to.
Wednesday, February 15, 2006
Effective Text Editing
I ran across this article I thought I'd share: Seven habits of effective text editing. It's written by the author of VIM (Vi Improved) but the techniques apply to all good text editors. The techiques are:
Move Around Quickly - Use the editor to jump quickly to your destination. This involves not only search but also backet matching, jumping to symbols, etc.
Don't Type It Twice - Search and Replace, Completion, Macros, etc.
Fix It When It's Wrong - Syntax highlighting
A File Seldom Comes Alone - An editor should support quickly moving between files and viewing more than one file at a time.
Let's Work Together - Can you integrate your editor with other programs?
Text Is Structured - Can your editor integrate with your build system? Does it recognize errors?
Make It A Habit - Spend some time learning your editor. If you never read the docs, you'll never find where the editor can help you save time.
If you are using an editor which doesn't allow for these techniques, you might consider switching. A friend turned me onto VIM some time back and I've started to become proficient with it. The learning curve is a little steep but the payoff is big. It's also really lightweight which is nice in today's era of heavy-footprint editors. Right now I have VIM (well, GVIM), MS Word, and Visual Studio.Net 2003 running. Their memory usage in megs is 5, 23, and 18. By comparison, Notepad appears to take about 3 megs.
Wednesday, November 17, 2004
Code Editor Learning Curves
A friend of mine put this together. It's a graphical representation of the learning curve for some common code editors. It seems quite accurate.