0

enter image description here

  • 3rd block of code needs to go from ipt1 to ipt4

  • 4th block of code needs to go from ipt1 to ipt5

Because it is only 3 selections, creating a macro is probably slower than manually, but doing it manually is still tedious.

What is the fastest way to be changing these? I come across situations like this a lot, and usually I try to use visual block mode with the change function, but these are not aligned so that is not an option.

  • A Vim screenshot would probably be more fitting. – romainl Oct 31 '16 at 7:19
  • 2
    I think we have a problem, here. First, you are not using either vi or Vim so an authoritative answer can only be given by someone who uses (and knows) Fakevim. Second, by not disclosing that information upfront (by way of a tag, for example) you made Karl waste his time posting an answer that, according to fakevim's README, is of no use fo you (no gn in fakevim). I don't really mind having fakevim questions on this site as soon as you tag them appropriately. – romainl Oct 31 '16 at 9:42
  • 1
    Change your screenshot, then, and revert my edits. – romainl Oct 31 '16 at 10:17
  • 2
    I personally think that questions not directly pertaining to either vi or Vim have nothing to do in a place called "vi and Vim". This includes fakevim, ideavim, neovim, nvi, spacemacs, evil-mode, busybox vi and even support for vim plugins. But tags can be filtered in or filtered out so, as long as questions are correctly tagged I'm OK. – romainl Oct 31 '16 at 10:27
  • I'm confused. Is this question intended only to ask about Fakevim (in which case why was the fakevim tag removed?) or are more general Vi/Vim answers desired (in which case, why does the title still mention Fakevim?) – Rich Nov 11 '16 at 15:13
3

Using visual mode and substitution:

v/}<CR>s/ipt1/ipt4<CR>

Using substitution only:

:,/}/s/ipt1/ipt5<CR>
  • Would that take longer for you to do that, then just manually replacing 4 instances? I'm not being cheeky; it is an honest question. – Akiva Oct 31 '16 at 7:29
  • 1
    This is the most generic method. It may or may not be faster than manually replacing the four instances but it has the enormous advantage of being deterministic. Anyway, if you need to do that often you should consider custom mappings… that's what we all do. – romainl Oct 31 '16 at 9:29
  • Yeah, fair enough. I was trying to think of making a good generic function, maybe taking two parameters: an integer for the position, and a string for replacement. maybe like :'<,'>foobar 4 "5" -- – Akiva Oct 31 '16 at 10:34
  • This usually where line numbers help. Not sure if FakeVim has that option. Either use absolute lines :50,55s/ipt1/ipt4 or use relative to your cursor position -3,+2s/ipt1/ipt4 – jecxjo Nov 2 '16 at 20:14
2

I would either do as @romainl suggests, or I would do a search, then use the gn text object (see :h gn). Specifically, I would do:

  1. /script\zs1<cr> (search for script1, but only match the 1)
  2. Make sure the cursor is on the first match that you want to change to 4, e.g. by repeating n or N to search forward/backward.
  3. cgn4...cgn5... (cgn = change the search match, then repeat with .)
  • Wow; really interesting, didn't know that you could search but match for only the last character. I'm really curious whether you can correspond search mode with visual mode. I should really be looking into that question, because then it is just a matter of using the change function. – Akiva Oct 31 '16 at 7:36
  • I don't understand what you are asking here. – Karl Yngve Lervåg Oct 31 '16 at 7:37
  • @KarlSomethingRather Geez... how to describe this... Okay; Can I take all my matches, and have them turn into visual mode? The functionality would be similar to visual block mode, where using the change function will delete everything, enter insert mode, and apply all the changes you make on the one line, to all others. – Akiva Oct 31 '16 at 7:43
  • 1
    No, not directly. But there is a plugin that might give you something like what you want: vim-multiple-cursors. I don't use that myself, and so I can't help you any further with this. – Karl Yngve Lervåg Oct 31 '16 at 9:35
  • 1
    @KarlYngveLervåg, you are wasting your time. The OP uses fakevim. – romainl Oct 31 '16 at 9:44
2

As I have yet to internalise the regular expression Karl Yngve Lervåg uses, I'd probably just brute force this example (with not many substitutions to make) with the dot and n commands:

  1. Search for text to change, leaving cursor at end of match: /ipt1/e
  2. Adjust cursor position if necessary with n or N
  3. Make the first edit and repeat for the other search matches, then do the same for the second edit: r4n.n.n.nr5n.n.n.
  • Missed seeing this before I posted my answer. Just jumped the gun when I saw the visual mode answer and wanted to get my $0.02 in. – ParanoidGeek Nov 12 '16 at 18:29
1

I run into this often enough myself. My preferred way to handle it is manually with some slightly advance search and replace techniques.

In this case, since you are going to be changing the last character of the block, and you can visually see what you want to work on, I might do the following:

/ipt1/e

This will search for the target text and put your cursor at the end (the 1) which is what you want to change. Then I would do the following keystrokes in normal mode

r4     # replace the 1 with a 4
n      # goes to the next match, places you at the end
.      # does the same substitution of 1 to 4
       # repeat n and . until you've finished the block for 4

When you get to the next block where you want a 5, just do a r5 and then n and .

You then are just bouncing between the 'n' and '.' and if you go too far, you can just use 'u' to undo. Very fast and limited typing is required.

  • I'm having trouble getting this to work. When I do :s/ipt1/e, it replaces my next entry with e. – Akiva Nov 13 '16 at 0:01
  • 1
    @Akiva Good catch. I reflexively just typed the :s before the search. I've updated the answer. – ParanoidGeek Nov 23 '16 at 13:49

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.