When using tailwind, there are many utility classes like px-1, py-1 etc. When using Ctrl+a to increment px-1, it becomes px0. How can I make Ctrl+a on px-1 become px-2 instead?
Don't. The -1 in px-1 is recognized as a negative number so incrementing it can only turn it into a 0.
Instead, use <C-x> to decrement it to -2 or follow this suggestion, found a few screens below :help ctrl-a:
For decimals a leading negative sign is considered for incrementing/
decrementing, for binary, octal and hex values, it won't be considered. To
ignore the sign Visually select the number before using CTRL-A or CTRL-X.
vim sees px-1 as px and -1. To make it -2 you don't want to increment it (-1 + 1 = 0), you want to decrement (-1 - 1 = -2).
The command to decrement is Ctrl+X: docs.
Related
There is one line with 10000 characters, how can I select / replace / delete the characters between 1234th to 5678th fast?
Better in Vim, but if emacs can do, it's also good, thanks.
With :substitute:
You can skip the first 1233 characters, and then capture 5678 - 1234 + 1 = 4445 characters, using the :help /\{ multi, and :help /\zs to set the match start:
:substitute/^.\{1233}\zs.\{4445}/REPLACEMENT/
Normal mode
Go to the first character (0, 1233 right, then work on the next 4445 via the 4445l motion. For example, deletion: 01223ld4445l.
Direct addressing
The bad thing about both approaches is that you need to calculate the difference (4445 in your example). You can do that in the command-line via the expression register (<C-r>=5678-1234+1<CR>).
Alternatively, if there are no double-width or tab characters, the screen column can be directly addressed via the :help /\%v regular expression atom, or the | normal mode command:
:substitute/\%1234v.*\%5678v./REPLACEMENT/
or
1234|d5678|
In Emacs, navigate to the line and use move-to-column (bound to M-g TAB by default). Use set-mark-command (C-SPC) to activate the region. M-g TAB 1234 RET C-SPC M-g TAB 5678 RET will select the region of interest. Then you can use narrow-to-region (C-x n n) to narrow the buffer to just the selected region. Narrowing the buffer allows you to edit the narrowed region without affecting the rest. After editing, you can widen with widen (C-x n w).
I'm dealing with a fixed-width file format and I need to increase all of the numbers in some columns. I have a simple macro that adds a value to a number, moves to the next line and repeats (like 2aj) However, these numbers start from 1 and usually end above 10000, so the column widths get messed up, e.g. (underscores as spaces, this example only covering the jump from 9 to 10)
FOO_7_BAR
FOO_8_BAR
FOO_9_BAR
becomes
FOO_9BAR
FOO_10_BAR
FOO_11_BAR
(note the new column of text that will break my program)
when I need
FOO_9_BAR
FOO10_BAR
FOO11_BAR
I have been manual going through and deleting a space from the first 9 columns, then 90, then 900, but I am looking for a more robust way to handle this without dealing with the first 10, 100, 1000, etc. with different macros or any manual input.
Thanks.
I come up with this way, I think the animation explains itself:
The final result is:
FOO 3 BAR
FOO 4 BAR
FOO 5 BAR
FOO 6 BAR
FOO 7 BAR
FOO 8 BAR
FOO 9 BAR
FOO10 BAR
FOO11 BAR
This requires a bit of manual hackery, but it's still better than manually deleting spaces.
You could also probably write a function that does this automagically, via Vimscript, though!
First, find the length of the shortest line. You can do this via ex. :echo col("$") on the shortest line.
Then, run the following command:
:g/.\{NUM\}/exec "norm! /[0-9]\<cr>X"
Replace NUM in the above command with the original number you got in the previous step.
Here's an explanation of how it works:
:g/.\{NUM\}/ Find lines that are too long
exec "norm! A common idiom: build a string to execute in normal mode
/[0-9]<cr> Find the first number on the line
X Delete the space before it (equivalent to "hx")
Then simply repeatedly run the same command (you can do this by pressing :UpReturn) until all the lines are the same length—it will result in an error once this is the case (since it won't find any matching lines).
Here's a short animation of the entire process:
This can be done with :s and a sub-replace expression.
:%s/\v([^0-9]*)(\d+)/\=strpart(submatch(1), 0, 5 - len(submatch(2))).submatch(2)
The idea is we capture the portion before the digits and the digits themselves. The replacement execute an vim expression via \= which put the two capture groups back together. However slice the first capture group via strpart() to a fixed width (5 in this example) minus the length of our second capture group, len(submatch(2)).
For more help see:
:h sub-replace-expression
:h strpart()
:h len()
I'm learning vim with vimtutor. I was wondering if there is a difference between command motion number and number command motion.
For example:
2dw seems to me to work exactly like d2w, similarly 2dd does the same as d2d.
The two numbers are both called [count], in your example, indeed, they do same job. But the two counts come from different concept.
[count]command
this will do the command [count] times, 2dd does dd twice; 2dw does dw twice.
The second is from the {motion}, 2w, 2j etc.
If you want to see some differences, here are two I can think of:
Some commands don't support {motion}. For example, the X, you press 2X, will remove 2 characters before the cursor. However, you cannot do X{motion}. other commands that don't support {motion} p (paste), s etc. You can do 2p, 2s, but you cannot do p2w s3w
You get same result from 2dw and d2w, but the two 2 have different meaning, understanding what the number does is ok. you can combine the count and motion, like 2d3w.
The number command motion can use on all command, but number motion only in a few.
The most important is that repeat-action(.) redo the previous action.
Example:
2dd->. = 2dd->dd
d2d->. = d2d->d2d
Usually, I suggest use the first command. Because it is easier to be repeated.
Say I have the following line:
|add_test() (| == cursor position)
And want to replace the 'add' with a 'del'.
del|_test()
I can either press X three times and then press i to insert and type del.
What I want is something like 3c or 3r to overwrite just 3 characters.
Both of these don't do what I want, 3c overwrites 3 characters with the same
character, 3r does several other things.
Is there an easy way to do this without manually Xing and inserting the text?
3s, "substitute 3 characters" is the same as c3l. 3cl and c3l should be the same, I don't know why you'd see the same character repeated. I'm also a fan of using t, e.g. ct_ as another poster mentioned, then I don't have to count characters and can just type "del".
I struggled with the "replace a couple of characters" for a few days too; 'r' was great for single characters, R was great for a string of matching length, but I wanted something like the OP is asking for. So, I typed :help x and read for a while, it turns out that the description of s and S are just a couple of pages down from x.
In other words, :help is your friend. Read and learn.
Use c{motion} command:
cf_ - change up to the first '_' (including);
ct_ - change up to the first '_' (excluding);
cw - change the first word;
The word is determined by iskeyword variable. Check it with :set iskeyword? and remove any '_', like that :set iskeyword=#,48-57,192-255.
By the way see :help c and :help motion if you want more.
I think 3cl is what you want. It changes 3 characters to the right. I'd type ct_del<esc>, but that's not what you asked
c3 ('c', '3', space), then type the characters you want to insert. (Or you can use right-arrow or l rather than space.)
Or, as #Mike just said in a comment, R works nicely if the number of characters happens to match the number of characters you're deleting.
Or ct_ to change from the cursor to the next _ character.
Or, as #bloody suggests in a comment, 3s.
If the works have the same length you can use the R command which replaces what you had previously with what you type.
The other answers given use numbers. When the text is longer it's easier to not have to count. For example I often make headlines in markdown files like:
Some super duper long title that I don't want to have to count
double the line with yy pp
Some super duper long title that I don't want to have to count
Some super duper long title that I don't want to have to count
Highlighth the whole line with V
then use r{char} or in this case r= to get:
Some super duper long title that I don't want to have to count
==============================================================
(I added a space above to trip stack overflow's markdown formatting)
How to replace the strings (4000 to 4200 ) to (5000 to 5200) in vim ..
Another possibility:
:%s/\v<4([01]\d{2}|200)>/5\1/g
This one does 200 as well, and it does not suffer from the "leaning toothpick syndrome" too much since it uses the \v switch.
EDIT #1: Added word boundary anchors ('<' and '>') to prevent replacing "14100" etc.
EDIT #2: There are cases where a "word boundary" is not enough to correctly capture the wanted pattern. If you want white space to be the delimiting factor, the expression gets somewhat more complex.
:%s/\v(^|\s)#<=4([01]\d{2}|200)(\s|$)#=/5\1/g
where "(^|\s)#<=" is the look-behind assertion for "start-of-line" or "\s" and "(\s|$)#=" is the look-ahead for "end-of-line" or "\s".
:%s/\<4\([01][0-9][0-9]\)\>/5\1/g
:%s/\<4\([0-1][0-9][0-9]\)\>/5\1/g
will do 4000 to 4199. You would have to then do 4200/5200 separately.
A quick explanation. The above finds 4, followed by 0 or 1, followed by 0-9 twice. The 0-1,0-9,0-9 are wrapped in a group, and the replacement (following the slash) says replace with 5 followed by the last matched group (\1, i.e. the bit following the 4).
\< and > are word boundaries, to prevent matching against 14002 (thx Adrian)
% means across all lines. /g means every match on the line (not just the first one).
If you didn't want to do a full search and replace for some reason, remember that ctrl-a will increment the next number below or after the cursor. So in command mode, you could hit 1000 ctrl-a to increase the next number by 1000.
If you're on Windows, see an answer in this question about how to make ctrl-a increment instead of select all.
More typing, but less thinking:
:%s/\d\+/\=submatch(0) >= 4000 && submatch(0) <= 4200 ? submatch(0) + 1000 : submatch(0)/g