Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Here's a good tip I picked up recently when working with numbers:

Increment a number on or after the cursor with Ctrl-A and decrement with Ctrl-X in normal mode. Saves a few keystrokes over having to replace or enter insert mode.



I've been using those more and more recently and have noticed a couple interesting things to be aware of. First, Vim interprets a dash character immediately preceding the number as a negative sign. (In playing around a bit I suspect this behavior is slightly influenced by the syntax rules for the particular filetype you're working with.) The effect of this is to flip the behavior of CTRL-A and CTRL-X. Secondly, if the first digit of the number is '0', Vim will behave as if the number is octal, so incrementing "07" once will result in "010". Similarly, a prefix of "0x" or "0X" will cause a number to be treated as hex.


To avoid the "07" -> "010" problem you can add

    set nrformats-=octal
to your .vimrc.


You can prepend a count to that too, to increment/decrement by a fixed amount.

This gets scary useful when you use tpope's speeddating plugin.


Just be aware if you are in a screen session, ctrl-a ctrl-x chord will lock the screen. It bit me only an hour ago.




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

Search: