1

I have a lot of older LaTeX files about physics, which contain values like 9.81 m/s^2. Since I'm now using siunitx and it's very useful \SI command, I have to change this to \SI{9.81}{m/s^2}.

There must be a better way than inserting \SI{, replacing the space with }{, moving past the unit and insert }. I would like a command like <localleader>si which can be called with the cursor within the value.

2

Sure, try this:

noremap <LocalLeader>si lBi\SI{<Esc>Ea}{<Del><Esc>Ea}

Alternatively you could do basically the same thing by creating a macro. Start recording a macro with qq, type what you need to do the change. Type q again. Next time you need to do the replacement, just use @q.

See :h recording for more info.

| improve this answer | |
  • Thanks a lot, that did the job. I have removed the lF9 and created an additional normal mode command. inoremap <LocalLeader>si \SI{<Esc>Ea}{<Del><Esc>Ea} noremap <LocalLeader>si bi\SI{<Esc>Ea}{<Del><Esc>Ea} <Esc> – Floyd May 25 '16 at 18:44
  • woops, that was supposed to be noremap. Not sure how I missed that, I did test it. :P Glad it worked for you. The IF9 should make it work with your cursor on any point of your original value. – Tumbler41 May 25 '16 at 19:02
  • lF9 does not work for me, it would move the cursor left one char and then Find the 9 the occurence of the character to the left. That does not seem right. Would it not make more sense to use rb for the normal mode command? – Floyd May 27 '16 at 6:12
  • That's odd. Use whatever you like I suppose, but F9 in normal mode should look backwards for the first '9' character. The reason it moved the cursor one to the right was so that if your cursor was on 9 it would still find it. (I was assuming that the sequence would always start with a 9 as per your example, but maybe that isn't the case.) Not sure what you mean about rb. That should just replace the character under your cursor with the letter b.... – Tumbler41 May 27 '16 at 14:15
  • Ah, now I get it. No, the 9.81 was just an example. The numbers are always different, as are the units. The rb was a typo, I meant lb (move right and then back to the beginning of the word). That way, the command works no matter if the cursor is on the first char or not. – Floyd May 27 '16 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.