9 ms·
Staying the hell out of insert mode (vi)
- xutopia 16y agoI guess I have to give it a shot again. I keep on getting frustrated with VIM. Perhaps this is the proper way to learn it.
- gte910h 16y agoMake sure you get function completion, etc for your language integrated in. I know that function is one of the one's non-vimmers seem to miss most when coding, and I'd miss it too, if it wasn't one of the first things I setup for every language. http://www.vim.org/scripts/script.php?script_id=1879 http://www.vim.org/scripts/script.php?script_id=1879 http://vim.wikia.com/wiki/Omni_completion http://vim.wikia.com/wiki/Omni_completion C++ http://vim.wikia.com/wiki/C%2B%2B_code_completion http://vim.wikia.com/wiki/C%2B%2B_code_completion Python http://www.vim.org/scripts/script.php?script_id=1542 http://www.vim.org/scripts/script.php?script_id=1542 Objc http://www.vim.org/scripts/script.php?script_id=2674 http://www.vim.org/scripts/script.php?script_id=2674 Most people who use vim don't use it as an exercise in minimalism we're often caricatured as constantly undergoing. I mean, we don't chock it up to Visual Studio/Emacs level of plug-ins, but we definitely use some in our day to day on average.
- anatoly 16y agoMost people who use vim don't use it as an exercise in minimalism we're often caricatured as constantly undergoing. How do you know that?
- gte910h 16y agoI talk with other vim users? Even in unix shops, lots of the people end up trying to use stuff like kDevelop and stuff. Those of us who instead vim, we talk.
- anatoly 16y agoI use vim in a minimalistic fashion; I've never talked to you about it. My guess is that you haven't talked to other than a minuscule portion of vim-using developers around the globe, and are presenting your prejudice as fact.
- novas0x2a 16y agoAlso, a gem that most people don't seem to know about: a standard vim install comes with a program called `vimtutor`. Running it will put you inside vim with an excellent first tutorial, suitable for beginner vimmers. Good luck! The muscle memory you learn now will pay off in the future! :D
- sophacles 16y agoYes, vi related muscle memory is particularly useful when using non-vi text entry.:wq
- koenigdavidmj 16y agoGrr. Why can't ^W be something less harmful in most programs?
- jrockway 16y agoWhat programs is it not backward-kill-word in?
- sophacles 16y agoIn vim it is the first part of a sequence for manipulating windows. This mindset can conflict with e.g. firefox, in which ctrl-w closes a window. (particularly if there are multiple firefoxes/tabs open) Edit: That should be close tab, not close window, c-shift-w is window.
- jrockway 16y agoAh. In Emacs, bash, and conkeror, it does something useful. (I've never really understood why there are keyboard shortcuts for things like "quit program" and "print". How often, if ever, do you want to do those things? Do they really need to be one keystroke away!?)
- 16y ago
- nailer 16y ago[code folding section deleted, I stand corrected] Vim's indentation changes still require a mode change three obscure keys and an enter, and vim's discoverability still sucks.
- novas0x2a 16y agoWhat do you mean "yet"? vim has had code folding for years: :help fold.txt If you want to indent code, < and > are your friends. (Really? Indenting code with search/replace? What are we, barbarians?)
- gfodor 16y agoDepending on the area you want to indent, you can use visual mode with % to select the block and then >> it over, or, for more specific regions, use visual block mode with ctrl-V to highlight the first column of the text you want to indent, hit I, indent the line by hitting space, and hit escape. When you move the cursor the other lines will indent.
- Avshalom 16y agoAt least for curly bracket languages. >iB indents the block <iB deindents dunno which languages it works for otherwise http://vim.wikia.com/wiki/Category:Indenting http://vim.wikia.com/wiki/Category:Indenting
- philwelch 16y ago">" is the indent command. Repeat it ">>" to indent the current line. Use any other vim ranging construct (e.g. ">2j") to indent wider ranges--i.e. the current line plus two lines below. "=" is the autoindent command. Vim recognizes many languages and you can give it plugins to understand more. Use "==" to autoindent the current line. My personal favorite, "gg=G", jumps to the top of the document and autoindents the entire thing. One time at work I was gently reminded how many spaces I was supposed to indent per the company's coding standard. I quickly set the relevant editor parameters and with four keystrokes my code was compliant. I think there's a code-folding ability but I've never used it.
- billybob 16y agoIndenting? Heck yes. '=' will indent for you. You just have to give it a movement. =j means 'indent this line and the next' =% means 'indent to the matching brace' =/foo mean 'indent to the next occurrence of foo' 1G=G means 'go to the first line and indent until the last one' If you want specific rules for indenting file types, you can put stuff in your .vim/indent/ folder to define them
- hotmind 16y agoThe title makes this article full of win.
- willwagner 16y agoThe "kj" remapping seems like a nifty trick; I've spent a while getting accustomed to ctrl-[ instead of the <ESC> key (I can't reach the <ESC> key without moving my left hand), but it's still hard for me to do quickly and reliably.
- latortuga 16y agoThe author of the article suggests using Caps lock as control but I've found it's a stellar place to have an escape key. After a few days it felt completely natural for my left pinkie to hit escape. For those on windows, there are a number of batch files that will help edit your registry to remap the keys although I found it to be a problem on Windows 7 until I tried Sharp Keys which worked great. Another trick that is useful for people who compulsively hit the Ctrl+s keybind from other editors; the following is from my vimrc: " Map Control+s to save a file nmap <C-s> :w<CR> imap <C-s> <ESC>:w<CR> This leaves you in normal mode which is usually what I want when I hit ctrl+s.
- pyre 16y agoDon't hit C-s when you're using vim in a terminal though. (But if you do, hit C-q)
- latortuga 16y agoI actually almost put this in my original post but I didn't know if this was just our server's vim install or if it applied generally. Thanks for the heads up!
- zieski 16y agoFor insert mode I switched to using <C-O> instead of <Esc> after reading that it only enters normal mode for 1 command. That way I stay in whatever mode I was in when I saved.
- lanstein 16y agoI map caps to esc instead of ctrl, and that plus H -> ^ and L -> $ means I hardly move my hands when out of insert mode.
- oliverkofoed 16y agoThe author recommends remapping "kj" to ESC. I've remapped caps lock to ESC, which I think is a far better use of that useless button, and a hack i recommend to everybody i know who uses VIM. I could never understand that VIM choose to put such an important button so far away from the homerow.
- silentbicycle 16y agoEscape used to be located closer, IIRC it was next to 1 (on `) on some older keyboards. (Much like Ctrl used to be where CapsLocks is now.) Moving it is easy, of course. Also, Ctrl-[ is usually the same key as Escape, and if you have CapsLock remapped to Ctrl, they're practically on the home row. (The article hints at this, but doesn't explicitly note that C-[ is Esc.) You could also remap CapsLock to Esc, of course.
- oliverkofoed 16y agoI'm on a danish keyboard; the programmer-handy keys like "[" and "]" have been moved to shortcuts (alt-9 for "]") to make room for the danish letters "æøå", so some of the combo's that are easy to make on english keyboards are next to impossible on mine. Once in an effort to reach maximum typing efficiency, i attempted a switch to VIM+Dvorak. However, i quickly realized that i would have to use a Danish variant of Dvorak (there is no official standard..) and remap VIM to oblivion, which would make me too much of an oddball masochist.
- silentbicycle 16y agoA bunch of my coworkers are Swedish, and I've had similar issues with the brackets. It's aggravating. I use the English/American Dvorak, but it takes me a second to switch back to qwerty as necessary. (I'm probably in the rare niche of mixed vi/emacs users, FWIW.) Vi + Dvorak isn't so bad, though I feel like Dvorak is (somehow) more compatible with Emacs.
- slug 16y agoI have the same problem, although for a different language, and solved it by using a shortcut (alt-ctrl-k) to switch between layouts (en-us,etc). I do it both on KDE and fluxbox, which are fairly easy to configure and the "mental" switch is not problematic.
- city41 16y agoI use ViEmu in Visual Studio as my primary "vi editor", and the clashing of Visual Studio and vi really adds up after a while. As I'm sure you can guess CTRL+O and CTRL+C don't work in ViEmu. I think the real solution is to make vim my main editor for working in C#. That's a pretty tall task unfortunately.
- frou_dh 16y agoGiven how well it marries with VS intellisense, it seems almost unfathomable to write C# in a more general text editor. Of course for C++, VS intellisense is a lot weaker and probably not that much of a draw.
- city41 16y agoYeah, part of me is not convinced ViEmu is the way to go. So many things in VS still require either the mouse or the arrow keys. A simple example is intellisense, once it pops up, j/k are not going to scroll through it.
- klochner 16y agoThe only problem I've found with remapping keys (and getting used to it that way), is that you look like an idiot when using someone else's machine. This was particularly unfortunate in a coding interview: "I'm used to a different keyboard layout, seriously."
- sophacles 16y agoMy first action on a new machine is: git clone $myserveraddy/homedirpublic . which includes a relevant vimrc. Helps alot. (get permission first with explanation of why...)
- kowen 16y agoThis gets even more embarrassing if you're used to typing in dvorak. I can never remember the actual keys I use to perform certain actions - my fingers obviously know how, but my brain has no longer retained the actual mapping. And I can't just look down at the keyboard to figure out which key it is, since I don't have a dvorak keyboard, just remapped the qwerty one to use the dvorak layout.
- jerf 16y agoMy plan to combat this in any future interviews (and a couple of previous ones) is to bring my laptop with me. If there's any development questions, I'll do it on my laptop. If they become offended that I want to develop in my preferred environment, on my preferred keyboard layout (Dvorak + capslock->backspace), with my preferred editor... they have failed my interview. It ought to be a plus that I came that well prepared. Incidentally, having been on the interviewing side, I do not see this often enough. Bringing your own laptop ought to be something you always do. I can provide "a" coding environment but it won't be "yours". Unless you always develop in a completely uncustomized standard Visual Studio environment, maybe (in which case frankly I'm still wondering a bit...).
- Derferman 16y agoI've remapped caps lock to ctrl on my personal laptop, so whenever I use another computer, I end up typing half of my commands / text in uppercase.
- Avshalom 16y agoVi(m) out of the box is a fantastic editor. As a code composing environment it leaves a lot to be desired until you've tacked on enough plugins that it looses a lot of it's vimmy goodness (imo obviously), as a result I tend to do my first draft in various places (FlashDevelop, the Factor Listener, Dr. Scheme etc...) and then use vim for all the follow up work. I don't know that it's a failure of vim but I think it's part of the reason articles like this keep getting written. The people not already deeply invested in an editor/IDE (me for instance) are the ones who spend more time writing new code than maintaining or adding to a significant existing code base. Just my thoughts, not really a comment on the article.
- DannoHung 16y agoscreen + vim is all the IDE I need (and want, for the most part).
- Avshalom 16y agoIf you mean just vim and not vim + omnicompletion + whatever, then more power to you I guess. I try to keep my vim pretty vanilla outside of font and color preferences because I spent most of college jumping from one computer to the next and I didn't want to get used to relying on anything that wouldn't be there next week. That said vim + screen kinda translates to vim + 'my entire computer' which doesn't prove my point really but also doesn't say much about the completeness of vim as an environment.
- pyre 16y agoYou can send commands to other 'windows' inside of a screen session. So you can whip up vim plugins to spawn a new screen window (for example) with your test suite running in it. This allows for some of the things that you can do with 'buffers' in Emacs that you can't really do with them in Vim (i.e. you can't launch a test suite in Vim with the output sent to a Vim buffer and still be able to edit with Vim while the suite is running). That said, I much prefer hex-editing with Emacs to using a combination of xxd and vim.
- jrockway 16y agoI like how people think vim is vi, and talk about vim-specific features as though they are in all vis. Incidentally, I sometimes use vim as $EDITOR when dealing with perforce, because if the editor dies (or fails to start) for any reason, all changes are committed by default with no commit message... and sometimes I don't have an emacs for emacsclient to connect to. (The result is 30 minutes of work trying to get perforce to uncommit my changes. Usually I fail and just comment out the tests, commit that, and fix the code later. Ah, perforce...) Anyway, I recently discovered that C-p, C-n, C-b, and C-f work as they do in Emacs. They just need to add C-a, C-e, M-m, C-k, and C-y, and normal mode will become unnecessary. (And I won't need to install mg on so many machines.) And then vim will almost have as many editing features as my bash prompt ;)
- Locke1689 16y agoHint: set -o vi That said, if you really believe that vim can't do everything that emacs can do in editing, I have nothing else to say...
- jrockway 16y agoEditing is but a small part of programming.
- Locke1689 16y agoEditing is a large but incomplete part of programming, which is why vim is a large but incomplete part of my programming environment. Look, if you're arguing this just to take potshots at "that thing you don't like" then we should end this conversation now. If you're open to the possibility that we can both be productive with different toolsets then I'd like to learn more about your setup and maybe share some of the benefits of mine.
- jrockway 16y agoYou're the one getting defensive. I am merely amused that vim added the Emacs-style keybindings to insert mode.
- pge 16y agoFor vim users, particularly newer ones, I think the cheat sheet at http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial... is great. Happy Hacking Lite keyboards are also great - they have the old layout with Ctrl where Caps Lock usually is and Esc not too far away.
- RevRal 16y agoI have tab remapped to esc, and s-tab as tab.
- blahedo 16y agoAh, too bad---I was hoping for an article on why to stay the hell out of insert mode, not how to. I know how to; I sometimes have a hard time convincing my students that it's good for them to become power users of their editor, and thought this might be a link I could pass along.