In a large buffer, I'd like to scroll down to the last occurrence of pattern pattern.
If I am at the first occurrence, it is easy enough to search for the pattern /, reverse the move to next occurrence n with N and get to the last..
If I am in the middle of a sequence of occurrences.. is there a better way to jump?
An easy way to find the last occurrence is to jump to the end and search backwards:
G?foo<CR>
In fact, there's an even easier way if you were already searching for something. Just jump to the end and search backwards for the thing you were just searching for:
GN
Simple as that.
Edit: if your search occurred on the very last line, then GN would skip over it to the second last occurrence. Using ggN would solve this problem. (And gg?foo<CR> for similar reasons.)
A potentially longer solution:
:vim foo % | clast
You can try searching backward via ?
:1?SEARCH_PHRASE
Another solution is:
G/\(.\{-}\zs\(foo\)\)\{1,}<CR>
Related
I am using vim to edit a huge file-I want to replace the 100th occurence of 'Luke' with 'The chosen one'. I searched the Internet for a possible solution and I guess I should do somethink like /Luke and then hitting the 'n' key 99 times, but I find this a bit tedious. Is there any easier way to get this done?
next supports a count
99n
should do
Otherwise, you could play with :global (it's a little bit overkill)
:let c = 0
:g/Luke/let c+=1|if c==100|s//This one!/|endif
You can script something, using feedkeys() function and using :s commadn with the "c" flag. On the mailinglist, this was once suggested.
This question already has answers here:
Vim - Delete til last occurrence of character in line
(5 answers)
Closed 8 years ago.
This is a sentence
If the cursor is on s of This, I want to move the cursor on n of sentence
I can do $Fn
But this can't be used combining with other command like delete
e.g If the cursor is on s of This, and delete all between cursor and n of sentence
d$Fn doesn't work
Anyone knows how to do this?
Assume that you have trouble with deleting words between s and the second n in "sentence". You can do it by
d2fn
And as #kev mentioned, easymotion is a good choice, with easymotion you can do it in a more intuitive way like
d<Leader><Leader>fn
the above command will highlight n in the line and let you choose.
My JumpToLastOccurrence plugin extends the built-in f/F/t/T motions with counterparts that move to the last occurrence of {char} in the line. Your example would be d,fn.
Vim plugin easymotion can help you.
As others have said, you can use the search command / in combination with the d command to delete up to a pattern match.
But, you can also make use of search offsets to place the cursor anywhere you want in relation to that search. See :help search-offset for details, but in your case:
d/senten/e will delete up to and including the second 'n' in "sentence".
You could also use d/sentence/e-2 to do the same thing but limit the match even more.
This is very powerful in combination with incremental search and search highlighting, because then you can see exactly what you're acting on before you hit <Enter> to finish the command, or <Esc> or <C-C> to cancel the whole thing.
You could use visual mode: v$Fnd
My Vim plugin ft_improved can also help. You simply keep on typing until the match is unique.
I don’t know which “n” in “sentence” is referred to here, but you can indeed use d to do this. Simply combine it with search (/):
d/n
Or, if you want to delete up to the second “n” in “sentence”, you could make the search pattern one character more specific:
d/nc
I would like to know if/how I can make vim look for the next occurrence of a variable. Let's say the variable's name is simply 'n', then /n would give me all occurrences of that letter, which isn't always terribly helpful. I guess I could create a regex to solve the problem, but I wondered whether there was some command/keystroke I simply don't yet know about; and as all my googling has been to no avail I decided to put a question on here.
Thanks a lot for your help!
If you have the cursor over the variable in question, you can press * and it will search for the next occurrence or # will search for the previous one.
This is equivalent to typing:
/\<n\>
(\< matches on the start of a word and \> matches on the end of word). The only difference (for reasons I'm not sure of) is that * and # don't pay attention to the 'smartcase' option.
See:
:help *
:help /\<
If you press n in command mode it will give you the next match of your search.
More detail:
/ will start forward search
? will start backward search
n will give you the next result in the direction you are searching
N will give you the previous result wrt the direction you are searching in
For example if I have some code like:
foo = bar("abc", "def", true, callback);
Is there a nice command to move true to the 1st or 2nd position leaving the commas intact?
P.S as a bonus my friend want to know if this works in Emacs too.
In Vim if you place the cursor at the start of the first word and do dWWP then it will have the desired effect. Here is a breakdown:
dW delete the current word, including the comma and the following whitespace
W move to the start of the next word
P insert the deleted text before the cursor
This will work if there are further parameters after the pair to be swapped - it will need to be modified if there are only two parameters or you want to swap the last two parameters, since it will paste the text after the closing bracket.
Alternatively you could use a regex substitution:
:%s/(\([^,]\+\),\s*\([^,)]\+\)/(\2, \1/
This will find the first two arguments after the open bracket and swap them.
update:
A search of vim.org found the swap parameters plugin, which should do exactly what you want and can handle situations that either of the above methods cannot.
I don't know the answer for vi, but in Emacs, transpose-sexps (C-M-t) will swap two arguments either side of the cursor. Actually transpose-words (M-t) was my first guess, but that leaves the quotes behind.
You need a transpose emacs command. But its limited to not guessing that its transposing in lists, it only considers text (it can't guess the 1st, 2nd word of list). Try this.
Keep your cursor at after comma of true. Use M-x transpose-words. By default it will transpose with next word from the point. Shortcut is M-t.
You can use C-u 2 M-t for transpose with next second word.
Now coming to your question. If you want to move true, to backward 1 word, use C-u -1 M-t, and for backward 2 words C-u -2 M-t.
Am not a VIM guy. So sorry bout that.
If you want to do this as a refactoring, not just as text manipulation, I'd suggest looking into Xrefactory, a refactoring tool for Emacsen (free for C/Java, commercial for C++).
Transposing previous (Ctrl-t p) and next (Ctrl-t n) argument ... add the
following into your .vimrc file:
map <C-t>p ?,\\|(<CR>wd/,\\|)<CR>?,\\|(<CR>"_dw?,\\|(<CR>a, <C-c>?,<CR>P/,<CR>w
map <C-t>n ?,\\|(<CR>wv/,<CR>d"_dw/\\,\\|)<CR>i, <C-r>"<C-c>?,<CR>?,\\|(<CR>w
My previous question seems to be a bit ambiguous, I will rephrase it:
I have a file like this:
copythis abc
replacethis1 xyz
qwerty replacethis2
hasfshd replacethis3 fslfs
And so on...
NOTE: replacethis1, replacethis2, replacethis3, ... could be any words
How do I replace "replacethis1","replacethis2","replacethis3",.. word by "copythis" word by using minimum vim commands.
One way I can do is by these steps:
delete "replacethis1","replacethis2","replacethis3",.. by using 'dw'
copy "copythis" using 'yw'
move cursor to where "replacethis1" was and do 'p'; move cursor to where "replacethis2" was and do 'p' and so on...
Is there a better way to do this in VIM (using less number of vim commands)?
Since you changed your question, I'd do it this way:
Move to the first "replacethis1" and type cw (change word), then type "copythis" manually.
Move to the next "replacethis", hit . (repeat last operation)
Move to the next "replacethis", hit .,
and so on, and so on.
If "copythis" is a small word, I think this is the best solution.
The digit needs to be included, and there could be more than one instance per line:
:%s/replacethis\d/copythis/g
Given that "replacethis[1-3]" can be arbitrary unrelated words, the quickest/simplest way to do this globally would be:
:%s/replacethis1\|replacethis2\|replacethis3/copythis/g
(Note that you need to use \| to get the pipes to function as "or". Otherwise, vim will look for the literal | character.)
I've been struggling with this for a long time too, I think I just worked out the cleanest way:
Use whichever command is cleanest to put copythis into register r:
/copythis
"rye
Then go to the replacement and replace it with the contents of r:
/replacethis
cw<CTRL-R>r<ESC>
Then you can just n.n.n.n.n.n.n. for the rest of them, or if they're wildly different just go to the beginning of each and hit .
The key is replacing and pasting in one step so you can use . later.
:%s/copythis/replacethis/g
To replace all occurrences of copythis with replacethis. Or you can specify a range of line numbers like:
:8,10 s/copythis/replacethis/g
Note, the /g on the end will tell it to replace all occurrences. If you leave that off it will just do the first one.
create this mapping:
:map z cwcopythis^[
( ^[ is the escape character, you can type it in vim using Ctrl+V Ctrl+[ )
go to each word you want to replace and press z
if u need to do essentially the same action multiple times - swap 1st word of one line with second word of the next line, I say you could record a macro and call it whenever you need to
Have you tried string replacement?
%s/replacethis/copythis
A host of other parameters are possible to fine-tune the replacement. Dive into the Vim help for more details. Some more examples here.
You can remap e.g. the m key in normal mode to delete the word under the cursor and paste the buffer: :nnoremap m "_diwP.
Then you can just copy the desired word, move the cursor anywhere onto the to-be-replaced word and type m.
EDIT: Mapping to m is a bad idea since it is used to mark locations. But you can use e.g. ; anyway.