Hacker News new | past | comments | ask | show | jobs | submit login
Efficient Editing With vim (jmcpherson.org)
218 points by ninjac0der on Sept 25, 2009 | hide | past | favorite | 121 comments



Warning: learning Vim makes you look for Vim like commands in any program you interact with using the keyboard. Everything else will be frustrating.


It's gotten to the point now where the first thing I say about any editor is, "That's cool and all... but does it have vim key bindings?" If it doesn't.... :q

I don't care how many built-in debuggers your IDE has; if I can't navigate it like I can vim, it's infinitely less productive for me.


I created this bash alias cause I was typing it so much at the the command line.

    alias :e=vim


hahahaha same here!


By force of habit, when I wanted to close an IM window, I have typed

  :q
Instead of switching to the mouse and hitting the "x". Then I had to explain to the person that I wasn't sticking my tongue out at them.


I once wrote ":tabnew something.js" in my IM window and sent it before I realized that the wrong window had focus. The person replied with, "bash: :tabnew: command not found." I told him, "I was hoping you were vim, actually".


HA!

Also using vim will cause your escape key to be heavily hit constantly after typing anything especially if you are not sure what mode you are in, then only realizing you are not in vim period.


Oh god I do this so much it's getting to be painful.


  inoremap jj <Esc>


ctrl+c is much better than esc :) but I use emacs more than vi[m]


Yeah -- more (keys) is always better!

Oh, wait . . .


I don't have esc bound to caps-lock so its a reach!


We see a lot of ":q" messages in #vim.


I've hit Ctrl-W to try to delete a word, and ended up closing an IRC window. It's not exclusive to vim. :)


OS X:

Put the following in ~/Library/KeyBindings/DefaultKeyBindings.dict

{ "^w" = "deleteWordBackward:"; }

GTK:

echo 'gtk-key-theme-name = "Emacs"' >>~/.gtkrc-2.0

gconftool -t string --set /desktop/gnome/interface/gtk_key_theme Emacs

Windows:

... uh ...


I've found it easy to avoid misusing Ctrl-w because of the fact I use it so much to close tabs in Firefox. You just need to have two different uses for something, both of which you use very regularly, to get used to not accidentally misusing them, I've found.


Vimperator will break that habit, for sure.


Alas, I think you may be right. I haven't been using it very long.

Once I actually buckled down and started learning how to use it, I was surprised by how quickly and easily a natural feeling of "this is how it should be" developed while using Vimperator. Of course, there's a lot of positive transference of knowledge from my heavy use of Vim itself, but still -- I didn't expect to find it this easy.

I think part of the reason may be that every single keyboard-driven browser I have ever used before (Lynx, W3m, Links, et cetera, ad infinitum) was so awful as a Web interface that I became convinced Web browsers were a uniquely GUI-suited type of application. Vimperator is definitely changing my mind.

Thank goodness.


I've hit ctrl-w and closed a window while taking a test in class.


When I first switched to my Mac, when I'd switch between split windows (ctrl w w), I'd often accidentally close my Terminal session (command w). The Apple Command sat where I expected ctrl to be.


Have you tried vim like browsing?

http://vimperator.org/trac/wiki/Vimperator

For keyboard lovers.


Can you explain why Vimperator is a good idea. As a vim user myself, I understand vim was designed first and foremost to input and edit text using the keyboard, is bringing those key bindings to browsing the web is a good idea?

For the best navigating experience, I feel Opera does really well. You keep one hand on the keyboard (nearing 1, 2 to switch tab; and z, x to move back & forth in history) and one hand on the mouse to click around & do gestures.


When I used Vimperator (I finally changed back just because I don't like how FF performs on OS X) I found I was strongly bimodal — surprise — where I'd spend the majority of my time just using the keyboard, both hands, and then if something demanded the mouse I shift to my trackpad.

The thing I want to point out is how loathe I became to switch to that trackpad. Vimperator makes you despise flash even more.


Yes, vimperator is great. Try it, you'll see. Vimperator makes it so you barely use the mouse, and that's definitely an improvement imnsho.


You may not be aware of it, but the view command on (most?) Unix systems is basically a read-only way to execute vi. It has the same keybindings in general, of course, because it's actually the same program with the -R option, more useful than more or less. It's especially more useful because one can "break out" of the read-only limitations if necessary (and if one has the requisite permissions for the file in question), but one has to actively override those limitations.

Since the Web is, a lot of the time, basically a read-only medium with occasional need for limited editing -- and, when done well, it's a primarily textual medium -- the vi interface philosophy turns out to be quite appropriate. If you dislike having to switch interface contexts between keyboard and mouse because you're a touch-typist who has learned the efficiency and power of a keyboard driven interface for text editing, a browser that does the same thing is a very attractive idea.

Unfortunately, most people's experience of text user interface browsers is limited to the shite that has been available for more than a decade, such as the ubiquitous Lynx. While it's entirely keyboard driven, it's not a good TUI. It is, in fact, a crap TUI. Vimperator, however, has managed to improve on the failures of all other TUI Web browsers I've ever seen by importing vi idioms to the context of Web browsing -- improve on it so much that it blows away both TUI and GUI browsers, at least for me.

I also use a window manager with a keyboard driven interface, and I probably do more typing online than the vast majority of Web browser users. With all that in mind, keeping one hand on the keyboard and the other on the mouse would be broken up a lot by having to get my hands back to home row on the keyboard so I could do things like type in the browser, switch workspaces and do work there, and so on. At least 98% of the time, Vimperator obviates the need to keep one hand on the mouse the way you do it, which means there isn't any interface context switching going on in my brain at least 98% of the time for me, whereas without Vimperator I'd spend more like 60% of the time I'm using the Web browser also using a mouse and would have to switch back to home row every time I need to copy text to or from Vim in a terminal emulator (for instance).

Now, the only times I need a mouse in the browser is when I want to do something like click the upvote arrow here at HN or do something in a Flash object (such as start the Flash player, since I use Flashblock).

I used to believe that for the best browsing experience, a mouse was necessary for the majority of uses, because the Web is an inherently graphical medium. Even essentially pure-text pages seemed that way to me, because things like hyperlinks are location-specific. Vimperator has allowed me to learn the error of my reasoning in that regard.


Why don't you use gestures for switching tabs and moving back and forward?


I've been using it for a long time and totally love it. I have the same mappings I have for Vim, for open and move between tabs and so on. It's an awesome tool.


I'm using it now, as a matter of fact.


20-year user of vi here... yes, once you get used to vi, going back to an editor for humans is hard.


Yeah -- giving up my transhuman lifestyle would be difficult.


I am still waiting for Vim FPS style (WSAD navigation keys, sniper scopes to skip words and all... sure it might need 3d graphics but hey, its the price you pay).


I, however, am waiting for an FPS, vim-style!


I use hjkl in all my games. That, or asdf. Keeps me from getting wrist cramps.

True story.


Damned straight. I want World of Vimcraft, yesterday!

. . . or maybe Vim of Warcraft. I guess VoW is a better acronym than WoV.


You can always get vim keys in any readline-enabled terminal program (like bash) by creating ~/.inputrc and putting this in it:

set editing-mode vi set keymap vi

http://vim.wikia.com/wiki/Use_vi_shortcuts_in_terminal


But, if something does have vim bindings, even in a limited manner, it suddenly gains extraordinary appeal (e.g. keyboard navigation in gmail)


I would kill for GMail to add vim-like text editing.


Have you heard of the "It's All Text" Firefox extension? It copies the content of any textarea to a tempfile and opens it up in an external editor of your choice. Writes to the tempfile are copied back to the textarea. It's not quite in-browser vim, but it's pretty close. https://addons.mozilla.org/en-US/firefox/addon/4125


Man this is awesome! I've just written this comment in vim. Thanks a lot.

Ugh... I decided to edit some more.. Hm.... ughh... more....


of course, if you use vimperator you have ctrl-i for that


There is also ViewSourceWith which allows the use of different editors.


. . . but Vimperator also Vimperates the entire browser, instead of just the text areas. Unless you're an Emacs zealot, I don't see the downside.


I think we got it by now. Unfortunately, vimperator has quite a few quirks and makes too many changes that cannot easily be undone, which is the reason why I uninstalled it and moved to VSW.


I moved in the opposite direction, from VSW (which I used heavily for a while) to Vimperator.


Try Vimperator. <C-i> opens gvim for the currently-focused text box.


You have made my day.

To make this work with MacVim on a Mac:

    :set editor="/Applications/MacVim.app/Contents/MacOS/Vim -g -f"


I had no idea this feature existed. I was still using the View Source With extension to open Vim in a terminal emulator window when editing text in a text area, even though I have Vimperator. Now I can do away with the extra extension.

Thank you.


Meh. I just use Mutt.


Try the "it's all text!" Firefox extension.


Nooooo all of the secrets of our vim cult revealed in one easy-to-read document! Vim is hard, people! Trust me. You're not smart enough to use it.

:-P


Actually, I find that Emacs has a steeper learning curve than Vi/Vim. I had an easier time learning to switch between command/insert modes than to try and get comfortable with all of the Meta-/Control- sequences in Emacs.

{edit} not to start a flame war.. to each their own {/edit}


I find that Vim's learning "curve" is almost vertical at first, but Emacs' gets worse as it goes while Vim's gets better. There's an image here that illustrates the phenomenon pretty well:

http://sob.apotheon.org/?p=981

It's sort of a "ha ha only serious" moment.


You can use a text editor for your whole working life, so what doesn't matter is the learning curve. What matters is what you can do once you're an expert.


I feel silly for only recently discovering tabs in vim.

  :tabnew file
Opens up a new tab,

  :tabn
Goes to the next tab, and

  :tabp
Goes to the previous tab. Works great in both gui and non-gui mode.


Similar to this, you can open up multiple files at the same time without saving.

    set bufhidden
    nmap <C-h> :bp
    nmap <C-l> :bn
If you put this in your .vimrc file, you'll need to put a '^M' at the end of the nmap lines. You do this by doing <C-v><C-m> and it puts in a special character that means newline. Then you can switch between open buffers with ctrl+l and ctrl+h even if you have unsaved changes. it will end up looking like this:

    set bufhidden
    nmap <C-h> :bp^M
    nmap <C-l> :bn^M
Unfortunately if you have many files open and don't touch them all it prevents you from quitting with a message 'E173: 17 more files to edit'. I haven't worked out how to obliterate this without damaging features I want (e.g. not quitting when there's unsaved data in those other files).

The default vim that ships with linux distributions is often a pile of junk that has been hacked by the distribution, is missing features like these. You want to get vim-extended under redhat-like distributions (they've significantly improved the quality if their junk vim in recent releases), and vim-nox or vim-full in debian-like distributions.


Instead of a literal ^M in the file, just use <CR>.

And I think you mean "set hidden".

And regarding E173: You can :qa, which won't lose any unsaved changes.

And the proper vim package for Debian or Ubuntu is "vim". "vim-full" is the old name and it just installs "vim".


I rely on buffers rather than tabs, too, and find minibufexplorer.vim to be a great addition: http://www.vim.org/scripts/script.php?script_id=159


I never got the hang of tabs. I got comfortable with buffers before tabs were out, and I didn't see anything that tabs could offer that buffers don't. I probably have my screen split between two buffers 60% of the time. Once in a while I have my screen split with screen AND with vim :D


tabs are pretty cool, but I like split and a set of macros I use for easily switching. The main reason I prefer split screen to tabs is that I often want to see code in the other buffer while I work.

Open up a file, :split a new one, and then try these:

map <C-j> <C-w>j80<C-w>+ " up one window, maximized map <C-k> <C-w>k80<C-w>+ " down one window, maximized map <C-h> 80<C-w>+ " maximize current window map <C-i> <C-w>= " all windows equal height


tabs are pretty cool, but I like split and a set of macros I use for easily switching. The main reason I prefer split screen to tabs is that I often want to see code in the other buffer while I work.

Open up a file, :split a new one, and then try these:

map <C-j> <C-w>j80<C-w>+ " up one window, maximized map <C-k> <C-w>k80<C-w>+ " down one window, maximized map <C-h> 80<C-w>+ " maximize current window map <C-i> <C-w>= " all windows equal height


gt and gT in command mode also do next tab and previous tab


I never bothered to learn that stuff before I started using Vimperator. I tend to prefer buffers over tabs for dealing with multiple files in a single Vim instance, so I never had much use for tab-related keybindings until I started using a vi-like interface for a tabbed browser.

Maybe that's just me, though.


also #gt will go that tab number. I've changed my tab text to show the number in parenthesis before the name.


For those who prefer alt navigation, you can add to your gvimrc

  " tab navigation
  map <A-1>  1gt
  map <A-2>  2gt
  map <A-3>  3gt
  map <A-4>  4gt
  map <A-5>  5gt
  map <A-6>  6gt
  map <A-7>  7gt
  map <A-8>  8gt
  map <A-9>  9gt
  map <A-0>  10gt


What is that heretical "g" doing in front of "vimrc"?


You know, if I had a terminal emulator that I could run vim with 24-bit colour depth in, that would go a long way towards turning me from the path of heresy.


Maybe it's some config he only uses in gvim.


Whoosh . . . ?


This also works with Vimperator. I'm now thinking about installing that Firefox extension that gives all the tabs numbers, for ease of reference. Alas, I don't remember what it was called -- but I'm sure I'll find it again.


That's a neat idea. How do you change it to show tab number in the tab header?


This is for GVIM.

  set guitablabel=(%N%M)\ %f


    :help setting-tabline


Awesome, I didn't know this shortcut before! Much better than using :tabn :tabp !


You can always open files as tabs at startup too with -p, like this: vim -p foo.c foo.h


Thanks, I've made frequent use of doing that with -o (horizontal split) and -O (vertical split).


A decent intro; I'd add to the movement section these:

g$ - move cursor to last displayed character of line

Tx - move cursor backward and to the right of the first occurrence of x (reverse of tx)

And get started with split views:

^ws - horizontal split view

^wv - vertical split view

^w{h,j,k,l} - move to window in respective direction

Some decent scripts to get started with:

minibufexpl.vim: http://www.vim.org/scripts/script.php?script_id=159

surround.vim: http://www.vim.org/scripts/script.php?script_id=1697

More of a full-fledge suite, a frontend to a head{ed,less} Eclipse server: http://eclim.sourceforge.net


Another nice script I discovered recently is Nerd Tree : http://www.vim.org/scripts/script.php?script_id=1658


Nerd Tree is indeed a good one, and I'll add to this vimfs, which isn't a Vim plugin but rather a CLI file system browser with Vim bindings. I hardly ever actually browse my file system (zfs, grep, and locate do all of the work for me), so I'm not sure how much mileage one can get out of this (especially compared to something like Midnight Commander).


My preference is to use things like cd and ls to browse the filesystem. When I'm feeling really fancy, I might use tree.


Do you sometimes let loose and use tree -C?


I have done so, but not in a long time. Then again, I haven't even used tree in a long time. Maybe I don't really use it any longer.

Hmm. . . .


^[ - Same as Esc (I prefer not reaching so far to Esc)

W - move forward a Word

B - move backward a Word


This is one of the best articles to speed up vim proficiency. When I read it a long while ago it really made a difference in my speed and typing joy.

If you guys love these kind of vim related posts, a while ago I had collected some good articles with brilliant tips:

http://durdn.com/blog/2008/11/26/vim-mind-share-soaring-roun...


Cool feature I discovered today, for multi-file project navigation:

  :tag yourMethodNameHere
Tab-completion works, so typing ":tag yourM", and pressing tab repeatedly cycles through all the tags with that prefix (you need to have created the tags file with ctags first).


Tag jumping is also cool: Ctrl-] will jump to the tag under the cursor and pushes the current location on the tag. Ctrl-T pops the tag stack and moves you back to your previous location. Good for checking the definition of a function and jumping back.


See, this is one of the things I love about Vim: there's always something new to learn! Somehow I had overlooked "K" to go to man pages. The other thing I love about Vim is the excellent documentation! I wanted to know more about "K" so I typed ":help K" and learned that, for example, if you're editing Ruby and would rather use 'ri' in place of 'man', all you have to do is add ":set keywordprg=ri" to your .vimrc (and if you have the ruby.vim files, it's already done for you).


Just a note, but ':help' doesn't always go to what you want. For example the option 'smartindent' (set with ':set smartindent') can be abbreviated as ':set si', but ':help si' takes you to the help for the ':sim[alt]' command.

It's useful to note that when searching through help you type something like:

  :help 'si'
To get help on a variable and:

  :help :si
To get help on a command. The first form would take you to the help entry for 'smartindent' and the second would take you to the entry for :simalt. If you just use:

  :help si
It will just find the first match for 'si', which in this case is the tag :si and not the tag 'si'.

Other possible confusions with :help are:

  :help tab
Will take to the help for <Tab> (the tab key/tab character), whereas the help page for all the things related to Vim tabs is:

  :help tabpage
(though :help tabs will take you to the :tabs command which is part of the tabpage help doc)


:help si<CTL-D> (type :help space s i and control-d after the i) will give you all available variations in help on si, including 'si' and si among many others.

CTL-D is a sort of intelli-suggest in context (except it's move 1/2 page down in command mode)

:help<CTL-D> gives variations of help in your installation.

:help <CTL-D> (help then space then control-D) gives more variations.

:help help<CTL-D> (ask for help on help, with a control-D immediately after the last p) gives more variations on help.

:help c_CTRL-D (literally typed just like that) is the help entry for using CTL-D on the command line.

:help CTRL-D<CTL-D> (everything up to the first D literally, then control-D) lists all the different help subjects for CTRL-D in various contexts.

:<CTL-D> (control-D immediately after typing colon) lists all the things you can do on the command line, including help.

Help.


":help help-context" explains this. And :h 'si' is for an option, not a variable.

And what's up with highlighting the most abused and misunderstood option ever? 'cindent' replaced 'smartindent', and usually you want "filetype indent on" in your .vimrc, not "set si" or "set cin".


dont forget :helpgrep !


Actually, you would use an ftplugin or autocmd to :set keywordprg=ri only when ft=ruby, which is what the ruby.vim files do. In fact, they are already part of the standard runtime files distribution.


I think it's the first time that I want to install vim to try it. As a Windows user, I always found NotePad++ and other text editor an easier choice but I can now see some powerfull utilities to vim now. Very clear article.


(I'm just throwing tips in comments where I see opportunities to help people who find any of the same niggles I've had over the years.)

Users who want to run Windows vim with no titlebar, create a file _vimrc in your base vim directory (on my windows desktop it's C:\Program Files\Vim). In this, put this line:

    set guioptions-=T


You can control a lot of things with guioptions. I would recommend doing a:

  :help guioptions


You mean toolbar?


I find it amusing that he asserts "blockwise selection mode. Extremely powerful and available in very few other editors".

Blockwise selection mode is available in almost every Windows editor I use - from the Delphi IDE, Visual Studio, even to Notepad2, Shift+Alt does block selection, or alternatively Alt + mouse selection.


I'm not familiar with most Windows editors, but — at least on OS X — although blockwise selection is available in most editors, blockwise insertion like vim has is usually left out.

E.g., in vim say I have a list like this that I wanted to append a period to on each line (a simple example, but you can see its uses):

    foo
    bar
    baz
I would just type block-wise select it (<c-v>$jj) and then type A.<esc>

I use this feature all the time.


Wow, I had no idea you could bulk-insert that easily! Thanks for sharing


plus I still have to see a use for it. Usually I just go linewise or, inside a line, with movements like f) or $.


You can use it to block indent/unindent code, I find that quite useful.


It's mostly useful for ASCII diagrams, in conjunction with overwrite mode.


. . . or source code formatting, or adding comment characters to multiple lines, or in any other way making an identical change to several adjacent lines at the same time.


Pretty cool, I was learning Vim through "A Byte of Vim" at http://www.swaroopch.com/notes/Vim


:Ex is a very useful command. And at first I thought that doing 'vim foo' where foo was a folder was just a quirk of UNIX, but eventually I figured that it's a folder browse view.

No more feeling around folder structures with ':e'!


if you are viming on an osx macbook of some sort and are sick of the damn small-ass control key they left us with, i've found it helpful to remap the caps lock key to control. you can do this easily in System Prefs > Keyboard > Modifier Keys. so now you can <esc> insert mode with the ^[, hardly moving your hands at all.

This works for me personally because I very rarely find myself even thinking about or noticing the true CAPS key, but if you are stuck on your CAPS bein CAPS, then this wouldn't be for you :!)


TIP for the Vim lovers out there:

Firefox: you can do a quick search hitting '/' Skype [Mac]: you can replace text in your last sent message with the 's/<old>/<new>/' syntax

I would love to hear about other apps with Vim-ish behavior :)

VIM IS FULL OF WIN!


Amazingly, this is the first I've heard of fx/tx/Fx. How ridiculously awesome.


This is especially powerful when trying to move small chunks of code around. Say you have something like

document.getElementById('asweetid')

and you want to grab just the id for use elsewhere. In command mode pressing f' will move your cursor to the quote, yf' will put 'asweetid' into your yank register and a p anywhere else will paste it. I also make heavy use of ctx where x is a character on a line that I want to replace up to. The best part about vim, I think, is how I think about the commands in my head and they just kind of flow out of my fingers.

"Hmm I want to change (press c) to (press t) that closing paren (press shift 0)."

Even just typing that my left hand instinctively moved toward the caps lock to go to command mode.


Yeah, I use 'dt,' or 'dt)' all the time to move single arguments of functions around, and 'd%' to move the function calls themselves.


yi' will also work in this example.


I have this printed out and taped up right next to my monitor: http://wint1.kaist.ac.kr/files/attach/images/59/450/vi-vim-c...

It's helped me polish off those last few vim commands that I never got used to. I recommend looking at it daily.


I've got that cheat sheet saved on my computer and I've looked at it several times before, but today was the first time I realized that you can hit '&' to repeat the last :s command on the current line. Very slick!!


Yep, and 'g&' is really helpful. It runs the last substitute across the entire file, preserving flags.


I thought I was a Vim master and I just looked at that and discovered once again how stupid and inefficient I've been this whole time. This sort of thing keeps happening to me with Vim.


Source: http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial...

That's the tutorial that fed me that chart in easy-to-manage pieces, easing me into vim nicely.

Also highly recommended, the rant by the same guy: http://www.viemu.com/a-why-vi-vim.html


It is also very useful to learn the code-folding commands ("z*"). Rather than repeat them here, just say ":help fold".


remapping capslock -> esc will make editing so much easier and faster. being able to easily enter command mode has increased my speed more than anything else, it's also really useful in other interfaces, to be able to quickly hit escape instead of reaching over to the top of the keyboard.

add xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape' to your ~/.xinitrc on linux


Remapping capslock to another control will make editing even easier and faster. Especially when you realize escape can be entered with ctrl-[ and sometimes ctrl-3.


Love me some vim, but isn't this kind of old? It is awesome though - think I used it back when I was getting started with vim.


vi is my favorite Rogue-like, with NetHack in close second, and my own http://DeadByZombie.com in third.

vi is the only Rogue-like where you can get real work done with it. And every time you learn a new trick it's like your XP level or attribs go up, or you've descended down to a new more evil level of the dungeon, with more powerful monsters to fight with your enhanced vi-fu.


. . . and I thought vi(m) was awesome before!

I will never look at the king of all editors quite the same way. You have my thanks, mkramlich. May your life be long and prosperous.

(I totally didn't mean that to sound like a Vulcan blessing, I swear.)




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: