Live data from Hacker News

Ten Years of Vim

matthias-endler.de

41–50 of 123 posts

Re: Ten Years of Vim

#41
post #37

I use (Vim IMproved - not vi) vim all the time. I find it interesting to see what features other people tend to use in vim, there seems to be some minor differences in their workflows, but it's not clear who "wins" the productivity battle. I only use a very small number of it's features: * `:tabe` Create a new tab * `:e .` Start browsing files in that tab * `:w` Write the file * `:q` Quit the file (`:q!`, seriously,…

I'm guessing you mean "dd" and "yy" to delete/yank lines. "x" deletes a single character, what you mean extract a line?

If you learn "w", which just means advance by a word, you can combine them with your existing knowledge for much better use. So "dw" becomes delete a word, or "d3w" means delete next 3 words. "v" is also useful in conjunction with "w" / "b".

Re: Ten Years of Vim

#42

One thing google can do that blows most peoples mind when I show them: Did you know vim supports time travel undo? Sometimes you do some things then undo and redo trying to get back to a certain point in time. In vim you can step forward and back through all this by doing "g-" or "g+" to travel backwards or forwards in time. Or you can say ":earlier 50s" to go back to where you were 50 seconds ago. Most editors treat…

All the JetBrains IDE's keep a history of your edits, I accidentally rm -rf'd the wrong dir and reverted my code back thanks to JetBrains. I can't imagine vim saving your old files that have yet to be checked in to SVN / git (breaking changes anyone?) that you've not yet opened on vim. Funny you mention that though, a coworker opens files in vim and does changes and tests but keeps it open if he's going to try someth…

Did that too, I thought history wouldn't work cause the .idea folder inside was gone, but it did. JetBrains saved me some good 5 hours of work that day.

Re: Ten Years of Vim

#43
post #33

One thing google can do that blows most peoples mind when I show them: Did you know vim supports time travel undo? Sometimes you do some things then undo and redo trying to get back to a certain point in time. In vim you can step forward and back through all this by doing "g-" or "g+" to travel backwards or forwards in time. Or you can say ":earlier 50s" to go back to where you were 50 seconds ago. Most editors treat…

One of my favorite vim plugins is https://github.com/sjl/gundo.vim , which can open a pane showing the actual tree of versions, including at what time the version changed. j and k can move around in the tree, and the pane with the file will show what it was at a given time. I've definitely used it to pull out a version of a file as it existed an hour ago...

Undo-tree for you emacs folks: https://www.emacswiki.org/emacs/UndoTree

Re: Ten Years of Vim

#44
post #37

I use (Vim IMproved - not vi) vim all the time. I find it interesting to see what features other people tend to use in vim, there seems to be some minor differences in their workflows, but it's not clear who "wins" the productivity battle. I only use a very small number of it's features: * `:tabe` Create a new tab * `:e .` Start browsing files in that tab * `:w` Write the file * `:q` Quit the file (`:q!`, seriously,…

I'm guessing you mean "dd" and "yy" to delete/yank lines. "x" deletes a single character, what you mean extract a line? If you learn "w", which just means advance by a word, you can combine them with your existing knowledge for much better use. So "dw" becomes delete a word, or "d3w" means delete next 3 words. "v" is also useful in conjunction with "w" / "b".

'[d|c]iw', to delete/change the word your inside of is another small improvement.

Re: Ten Years of Vim

#45
I read an article a few years ago, the name of which escapes me. It was something like “Idiomatic Vim” or “Linguistic Vim”. I would love to find that article again, if anyone happens to know based on the sparse info I’ve just provided, I would greatly appreciate it.

One of the things I learned was that you can do things like yank to a character. In other words, if your cursor is at the beginning of “The quick brown fox jumped over the lazy dog” you can type ‘ytx’ (Yank To to the “x” next “x” character) and it’ll yank “The quick brown fo”. That’s really handy with code, I find.

Re: Ten Years of Vim

#47

One thing google can do that blows most peoples mind when I show them: Did you know vim supports time travel undo? Sometimes you do some things then undo and redo trying to get back to a certain point in time. In vim you can step forward and back through all this by doing "g-" or "g+" to travel backwards or forwards in time. Or you can say ":earlier 50s" to go back to where you were 50 seconds ago. Most editors treat…

All the JetBrains IDE's keep a history of your edits, I accidentally rm -rf'd the wrong dir and reverted my code back thanks to JetBrains. I can't imagine vim saving your old files that have yet to be checked in to SVN / git (breaking changes anyone?) that you've not yet opened on vim. Funny you mention that though, a coworker opens files in vim and does changes and tests but keeps it open if he's going to try someth…

Vim also has persistent undo. Check out the options:

    undofile
    undodir
These will cause vim to store all file histories into files within that directory. Now you can close and reopen vim and continue undoing as much as you want!

Re: Ten Years of Vim

#48
post #14

Earlier quoted context omitted.

Very cool. Do you know if you can "mark" spaces in time to jump back to like you can spatially in vim? Sometimes I change a bunch of random stuff at once to try and get something to work, and perhaps this use case would be better suited to an actual git commit but I feel like a mark in time might be a good inbetween measure.

The closest thing I'm aware of is ":earlier {N}f", which "[Goes] to older text state {N} file writes before.". So once you're somewhat happy with what you've got, write the file, and then continue editing, repeat, and you can revert back to any previous saved file state.

Awesome, thanks

Re: Ten Years of Vim

#49

One thing google can do that blows most peoples mind when I show them: Did you know vim supports time travel undo? Sometimes you do some things then undo and redo trying to get back to a certain point in time. In vim you can step forward and back through all this by doing "g-" or "g+" to travel backwards or forwards in time. Or you can say ":earlier 50s" to go back to where you were 50 seconds ago. Most editors treat…

All the JetBrains IDE's keep a history of your edits, I accidentally rm -rf'd the wrong dir and reverted my code back thanks to JetBrains. I can't imagine vim saving your old files that have yet to be checked in to SVN / git (breaking changes anyone?) that you've not yet opened on vim. Funny you mention that though, a coworker opens files in vim and does changes and tests but keeps it open if he's going to try someth…

It has saved me too a couple of times. Jetbrains for the win :)

Re: Ten Years of Vim

#50
post #37

I use (Vim IMproved - not vi) vim all the time. I find it interesting to see what features other people tend to use in vim, there seems to be some minor differences in their workflows, but it's not clear who "wins" the productivity battle. I only use a very small number of it's features: * `:tabe` Create a new tab * `:e .` Start browsing files in that tab * `:w` Write the file * `:q` Quit the file (`:q!`, seriously,…

That's a solid set and will take you far. I did eventually start peppering in a few pattern matching command things that don't require much regex.

* `:g` Global command, executes an Ex command on every line that matches via the pattern `:[range]g/pattern/cmd`

One common use I have for using `:g` is to delete all lines that match or do not match a pattern, like when I've removed an attribute from some data structure and I want to quickly fix all my mock objects to remove the attribute: `:g/pattern/d`

To delete patterns that do not match, useful for debugging: `:g!/pattern/d`

* `:norm` this thing motivated me to use more of the movement commands like `A`, `o`, `D`, `f`, `cw`, and `ci`. Used `:[range]norm cmd`, it acts like each key in the command is run on every line in range. I usually use it with a selection, for example to add a comma to the end of every line in range with `A`, or insert at the end of line: `:norm A,`

Or to delete everything after the first period in a range using `f` to move to character, `l` to move after that character, and `D` to delete everything after cursor: `:norm f.lD`

The best part about learning these two is that they can be used together. If you want to insert `fmt.Println(err)` after every line (using `o`), but only on the lines matching `err != nil`: `:g/err != nil/norm ofmt.Println(err)`

Or two write `time.Now(),` only on lines which have `"created_at":` and only after the colon: `:g/created_at/norm f:lDA time.now(),`

And if you've got hashes with string keys and string values i.e. `"key": "value"`, you can upper case the string values matching `"pattern"` with: `g/pattern/norm 3f"lvi"~` On lines it matches it finds the third double quote with `3f"` then moves a character over with `l`, visual selects inside the double quote with `vi"` and uppercases the selection with `~`.

One regex trick that I end up using with `:g` pattern matching is `^\s*`. When you put it before any pattern then the pattern matches only if it has some amount of whitespace preceding it, and that whitespace extends from the beginning of the line to the start of the pattern. It is good for targeting code indented with whitespace.

Final thought, when you're typing your commands in norm and you've entered insert mode, you can issue the escape command with which when done right should look like `^[` but will not actually be those characters.

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

Post reply on HN