7 ms·
There's too many little things to mention, they do all add up to be quite significant in the course of programming. Here's a tiny sample of some stuff I use co
by pengaru 6y ago
There's too many little things to mention, they do all add up to be quite significant in the course of programming.
Here's a tiny sample of some stuff I use constantly:
Move cursor forward word at a time: w ignoring punctuation: W reverse: b B
Move cursor to next occurrence of character Z: fZ reverse: FZ
Delete forward to next occurrence of character Z: dfZ reverse: dFZ
Change forward to next occurrence of character Z: cfZ reverse: cFZ (like delete but leaves in insert mode afterwards)
Repeat latest edit from current cursor position: .
A lot of the vi commands are composable, as illustrated to some extent above.
To search for say the next line starting with void: /^void
To delete from the current line up to the next line starting with void: d/^void
Note the similarity to dfZ/fZ from above, it's not 100% consistent but most times you can take a chance composing things as you'd expect and things work correctly.
There are far more sophisticated things too of course, but I'm not writing a vim tutorial in an HN comment.
You can do things like rename a function while rearranging its arguments either file-wide, or on a number of lines, or on an interactively selected visual region, or a section selected similar to d/^void... I presume other editors have such capabilities, but I'm most familiar with vim. In vim the actual renaming and rearranging is done using a regular expression containing backreferences:
s/old_func(\([^,]*\), \([^)]*\))/new_func(\2, \1)/
You may constrain that to a subset of lines a variety of ways, and on top of that can suffix a 'c' on the end of the substitution regexp to make it conditional and you'll get a chance to interactively yay/nay the substitution at every match in the considered lines. The conditional suffix is a godsend as it lets you be fast and sloppy with your regexps and just skip the false positives.
- brabel 6y agoHonestly, I can do almost all of these things with any standard editor as well, with standard shortcuts. The ones I can't do directly, I can do via Search/Replace functionality that is also standard in most modern editors. Your example about refactoring a function by using text editing capabilities is actually pretty bad unless you mean Vim can actually parse your programming language and perform the refactoring over all your code-base (not just a single file), like any IDE will do. This list you gave me just convinced me I don't need Vim.
- pengaru 6y ago> Honestly, I can do almost all of these things with any standard editor as well, with standard shortcuts. WTF is a standard editor and standard shortcuts? Would that be ISO or ANSI?
- Lio 6y agoAlso what is “almost all” in this context? Isn’t that just another way of saying I can’t do these things but don’t care? The Blub argument effectively.
- harisund 6y agoHere's the real question - So you are convinced you don't need Vim, right? Why are you on this thread? No one's saying vim is superior. However, you need to have an open mind to understand the alternatives. It's not like Vim is a paid product and by you not using it, Vim is going to die out soon or anything. If you dont' like it, great? You are happy with the alternative, great. You seem like you just want to deliberately start an argument and then prove you are right, but why?
- imtringued 6y agoI use vim as a boring config editor in terminals. I don't think it's the best editor by a long shot and even something as basic as Atom is more productive for code editing for me. The idea of customizing your vim setup for maximum productivity doesn't appeal to me. It always ended up in a mess and over ssh it's pointless.
- alwillis 6y agoYou may want to check out “How Vim killed Atom and VSCode on my Machine”—-https://medium.com/@aswinmohanme/how-vim-killed-atom-and-vscode-723a68ad59dc https://medium.com/@aswinmohanme/how-vim-killed-atom-and-vsc...
- brabel 6y agoI am in this thread because this is not a thread about how Vim is great, it's a thread about how Vim became popular, which I honestly don't understand in a day and age where modes are completely unnecessary. I see lots of happy Vim users telling the rest of us how they are so happy with Vim and they can't understand how anyone can do anything without it... so I thought I should point out I feel extremely productive on any non-mode text editor (all my shortcuts work even in this HN editor, on a browser, in Word, basically anywhere except old editors like Vim). Been programming for nearly 2 decades now and even when I started, I thought Vim was a tool from a time when we didn't have Ctrl and Arrow keys and we already had better alternatives. At this point, I feel like "I use Vim" is like "BTW I use Arch", said usually with an air of superiority as if choosing "the hard way" was somehow smart.
- ricksharp 6y agoThanks for the feedback. vscode has regex find replace, but I don’t use it very often. It does show every match highlighted in the current file while entering the find value, which is nice. It does the same for multi files if needed and gives a nice preview of every change. Word navigation I use a great deal (especially with multi-cursor editing). Ctrl+left/right jumps words, home, end to begin end of line. Ctrl+f Z Enter would go to next Z value. (Which I don’t use, because I don’t think in partial words much, but rather jump to mouse if I need to navigate far) That’s probably where I could gain in using that instead of going to the mouse every time.
- throwaway8941 6y agoCtrl+arrow is almost as bad as going to the mouse. The thing with vim is you rarely have to leave the home row. Some other random things I use constantly (I am not the person you're replying to): C — replace to the end of the line xp — swap two characters (which I have to use more frequently than I'm willing to admit.) ci( — replace everything in brackets (like the arguments of a function, this also works with " or ' for strings, { for blocks of code, and so on). The cursor doesn't have to be inside the brackets, it'll jump to the nearest match. :earlier 10m — time traveling, which comes in handy between commits or with config files, especially if you configure vim to write persistent change history. What did this file look like a month ago? Oh yeah, :earlier 30d . — repeat the previous command (this is much bigger than it sounds, as it allows you to repeat the same complex manipulation on different data in situations where you can't use multi-cursor, but which don't warrant building a whole macro.) A ton of custom filetype- and mode-dependent <leader>-based bindings (which is the spacebar in my case). space-space to jump to definition, space-f to reformat the file, that sort of stuff. This can probably be done in vscode. If you're really interested, there are tons of vim videos on youtube made by real vim pros. I am very far from being one. The best for me are live coding sessions where vim usage is demonstrated by doing the real work, not on some toy examples.
- RMPR 6y ago> :earlier 10m — time traveling, which comes in handy between commits or with config files, especially if you configure vim to write persistent change history. What did this file look like a month ago? Oh yeah, :earlier 30d Another trick to my bag, that's why I absolutely love these threads about Vim.
- IshKebab 6y ago> Delete forward to next occurrence of character Z How often do you really do things like this though? It seems like, sure you can do weird obscure things quicker in Vim, but only if you happen to remember how do to the weird obscure things, and it's only useful in weird obscure situations. For 99% of tasks the built-in VSCode stuff does the job for me. Ctrl-D especially.
- ketzu 6y agoI don't use vim, but this feature seems mostly useful for deleting things within marking characters, such as " and ', which I do surprisingly often. (But not often enough to learn a shortcut to do it)
- nemetroid 6y ago> How often do you really do things like this though? All the time. Some examples: * ct, : "change up to comma", useful for replacing a function argument. * ya" : "yank around '"'", copy the string literal the cursor is currently inside. * ci( : "change inside '('", useful for changing the condition in an "if" statement. * da{ : "delete (i.e. cut) '{'-delimited block", useful when changing the structure of if/else blocks. I agree that these commands may sound obscure, and many Vim users use the program for a long time without using them (I certainly did). But when you start using them, you find that these composable editing commands correspond really well to many of the logical editing actions you perform.
- tincholio 6y agoVery often, actually. It's something that pops up quite a lot when editing code, and you want to remove/change a chunk of text.
- pengaru 6y agoIn the very comment you're replying to I already said "stuff I use constantly".
- mcjiggerlog 6y agoNot that specific example, but you'd really be surprised by how useful some of these things are. For example you can do a `di[` to delete everything inside an array. Or `ci"` to replace everything inside a string.