8 ms·
Efficient Editing With vim
- 10ren 17y agoCool 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).
- mustpax 17y agoTag 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.
- jballanc 17y agoSee, 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).
- pyre 17y agoJust 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)
- graywh 17y ago":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".
- nfnaaron 17y ago: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.
- soundphed 17y agodont forget :helpgrep !
- graywh 17y agoActually, 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.
- daok 17y agoI 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.
- cturner 17y ago(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
- scott_s 17y agoI 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.
- DannoHung 17y agogt and gT in command mode also do next tab and previous tab
- solutionyogi 17y agoAwesome, I didn't know this shortcut before! Much better than using :tabn :tabp !
- deleted 17y ago[deleted]
- bockris 17y agoalso #gt will go that tab number. I've changed my tab text to show the number in parenthesis before the name.
- solutionyogi 17y agoThat's a neat idea. How do you change it to show tab number in the tab header?
- graysnorf 17y agoFor 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
- lanstein 17y agoAmazingly, this is the first I've heard of fx/tx/Fx. How ridiculously awesome.
- latortuga 17y agoThis 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.
- nostrademons 17y agoYeah, I use 'dt,' or 'dt)' all the time to move single arguments of functions around, and 'd%' to move the function calls themselves.
- puffythefish 17y agoyi' will also work in this example.
- joshuaxls 17y agoI have this printed out and taped up right next to my monitor: http://wint1.kaist.ac.kr/files/attach/images/59/450/vi-vim-cheat-sheet.gif 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.
- chewbranca 17y agoI'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!!
- mrshoe 17y agoNooooo 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
- pyre 17y agoActually, 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}
- apotheon 17y agoI 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 http://sob.apotheon.org/?p=981 It's sort of a "ha ha only serious" moment.
- justinhj 17y agoYou 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.
- grosales 17y agoPretty cool, I was learning Vim through "A Byte of Vim" at http://www.swaroopch.com/notes/Vim http://www.swaroopch.com/notes/Vim
- Tihy 17y agoWarning: learning Vim makes you look for Vim like commands in any program you interact with using the keyboard. Everything else will be frustrating.
- scott_s 17y agoBy 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.
- nostrademons 17y agoI 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".
- GrandMasterBirt 17y agoHA! 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.
- barrkel 17y agoI 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.
- tetha 17y agoplus I still have to see a use for it. Usually I just go linewise or, inside a line, with movements like f) or $.
- barrkel 17y agoIt's mostly useful for ASCII diagrams, in conjunction with overwrite mode.
- apotheon 17y ago. . . 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.
- bbest86 17y agoYou can use it to block indent/unindent code, I find that quite useful.
- puffythefish 17y agoI'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.
- mhansen 17y ago
- durdn 17y agoThis 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-roundup-of-10-vim-articles-recent-and-older-gems/ http://durdn.com/blog/2008/11/26/vim-mind-share-soaring-roun...
- mkramlich 17y agovi is my favorite Rogue-like, with NetHack in close second, and my own http://DeadByZombie.com 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.
- apotheon 17y ago. . . 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.)
- deleted 17y ago[deleted]
- Prolitheus 17y agoA 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 http://www.vim.org/scripts/script.php?script_id=159 surround.vim: http://www.vim.org/scripts/script.php?script_id=1697 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 http://eclim.sourceforge.net
- EAMiller 17y agoAnother nice script I discovered recently is Nerd Tree : http://www.vim.org/scripts/script.php?script_id=1658 http://www.vim.org/scripts/script.php?script_id=1658
- Prolitheus 17y agoNerd 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).
- apotheon 17y agoMy preference is to use things like cd and ls to browse the filesystem. When I'm feeling really fancy, I might use tree.
- Prolitheus 17y agoDo you sometimes let loose and use tree -C?
- makecheck 17y agoIt is also very useful to learn the code-folding commands ("z*"). Rather than repeat them here, just say ":help fold".
- yurifury 17y ago: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'!
- pssdbt 17y agoLove 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.
- malditogeek 17y agoTIP 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!
- soundphed 17y agoif 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 :!)
- gnubardt 17y agoremapping 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
- graywh 17y agoRemapping 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.