Use line range within a fold - vim

Is it possible to restrict line range to within current fold? Say I want to do a substitution from second line through end of fold i.e.
2,$s/str/replace/
but I want it to apply to current fold only.

There are motions for jumping to the beginning of the current fold (:help [z) or to its end (:help ]z) but they don't have equivalent marks. If that were the case, one could do this with the imaginary '* mark:
:2,'*s/str/replace/
but that mark doesn't exist so it won't work.
One way to achieve what you want would be to visually select the area:
]zv[zj
and then do your substitution on the visual selection:
:'<,'>s/str/replace/
with the range '<,'> automatically inserted for you.
Or you could go the "grok vi" way and place manual marks:
]zme[zmb
before using them in your substitution:
:'b+,'es/str/replace/
but it seems less optimal than the "visual way".
Another solution would be to close the fold:
zc
and do the substitution on the closed fold, with confirmation so that you can skip the substitutions that occur on the first line:
:s/str/replace/c
So there are two approaches, here: one where care is put into defining the exact range and one where care is put into not substituting the wrong thing.

Related

Vim replacing in inverse direction

How can I replace in Vim from current line in inverse direction (upward) (for searching I'm using ?textToFind, for replacing from current position :,$s/a/b)?
You can use backwards range with :s command.
If you want to do replacement from line 1 to your current line, you can do :,1s/foo/bar/g vim will ask you if you are sure to apply command on a backwards range, press y
You can also do something like :,-3s/foo/bar/ to do replacement from current line (n) till line n-3
The range used for s// (and other Ex commands) can be made of:
line numbers, 1,23
relative lines, -5,+17
line shortcuts, .,$
marks, 'a,'g
searches, ?foo?,/bar/
or any combination of the above items, ?foo?,'g, 23,$, +5,/bar/, .,/baz/+6…
A range extending from the first instance of foo before the cursor to the last line could look like that:
?foo?,$
A range extending from the first instance of foo before the cursor to the current line could look like that:
?foo?,.
and even be shortened to:
?foo?,
There's no built-in way to visit the lines from the end to the beginning. Vim will issue a Backwards range given, OK to swap? query, and turn around the range if confirmed. The only way is by manually specifying the individual lines in reverse order:
:.s/a/b | .-1s/a/b | .-2s/a/b | ...
Of course, you can write a custom command for that.
If you also require reverse replacement inside a line (all of that only makes sense with the confirm flag, doesn't it?), you're out of luck with :substitute.

Setting editing range in Vim

Is there a way to set the editing range for a file in Vim so that it will apply to any subsequent command? I'm about to do some major refactoring of a function and I would like to limit all of my changes (substitutions in this case) to just that function. In the past I've just copied the function to a new buffer, made the changes and copied it back to the main file, but I'm hoping there is a more clever way.
I know I can set a mark at the start and end of the function and use the marks for the range of the substitute command and I know I can use visual mode to select the range of lines, do something, use gv to reselect and then do the next command, but both of these methods rely on me remembering to specify the range before each command.
Any other suggestions?
Here is a cool trick with folds:
Fold your whole function.
Perform a substitution, :s/foo/bar/c<Enter>, note the c flag for "confirmation", it's vital when doing refactoring in Vim. The substitution will be applied to every line in the fold. AFAIK, you can run any Ex command that way but I've never used this trick with anything else than :s and :g/:v.
Hit y/n to accept/reject each substitution.
After you accepted/rejected the last substitution the fold goes back instantly to its "closed" state: you don't have to type a range for the next substitution.
GOTO 2
Assuming vim was compiled with +textobjects, you can select the function as a block. So perhaps you are looking for:
va{:s/foo/bar
To replace foo with bar only in the current {} bracketed function. If your functions are not delimted by braces, you could write a function to select the range according to some syntax rules, but perhaps this is good enough for your needs.
Use Narow Region Plugin of vim http://www.vim.org/scripts/script.php?script_id=3075 put them on your vim folder and use like this:
:[range]NR
See this page: https://github.com/chrisbra/NrrwRgn

Vim - Search and replace the results

I'm getting more and more comfortable with Vim after a few months.
BUT, there is only one simple feature I can't get any answer from the web. That is "Search and replace the results". The problem is that I know:
:/keyword to search, and hit enter "keyword" will be highlighted (of course with set hlsearch)
n, or N to navigate
:% s/keyword/new_keyword/g to replace all occurences of keyword with new_keyword.
BUT, I would think that there must be a way to search, and replace the matched keyword (highlighted) with any new_keyword WITHOUT doing ":% s/keyword/new_keyword/g", which is a lot of typing considering search & replace is such a day-to-day feature.
Any answers/comments will be greatly appreciated!
If you've already done a search you can do a substitution for the same pattern by simply leaving out the pattern in the substitute command. eg:
/keyword
searchs for "keyword", and then:
:%s//new_keyword/g
will replace all occurrences of "keyword" with "new_keyword".
Searching and using the dot command (you didn't meantion you are using the dot command, that's why I highlight it) to repeat the last input action is my best bet here.
I use s///g for search and replace.
Well, since #keyword# and #new_keyword# account for most of the characters, and you need some way to differentiate between them (i.e., a character in vim, or tab between entry fields in dialog in a different editor), you're left with maybe four or five keystrokes beyond that.
So I think you're probably overestimating number of keystrokes and also forgetting that (1) it becomes very natural, and (2) working this way allows you also to naturally modify the action performed by specifying a different range or option flag.
But you can cut down on keystrokes. If you want you can map a key to automatically bring up the command line with '%s/' already in place. e.g.:
nmap s :%s/
The command above would remap 's' (I'm not recommending remapping to that key, but it gives the idea) and set you up to insert the keyword.
Also, you can set the 'gdefault' option to default to substituting multiple times per line. This lets you skip the ending '/g' in your keystrokes:
set gdefault
See ':h gdefault' for help section on that option.
In the end I would say just get used to the default way it works, because using it that way allows you to keep same basic operation when you want to specify different ranges or option flags, and creating a new special map is just another thing to remember. gdefault may be worth setting if you think you're going to want it majority of time, adding /g flag at end when gdefault is set has effect of turning /g off. . .
Move to the first highlighted word then record a macro for replacing the word and moving to the next one, e.g:
gg
n
qq
caw new_word^[
n
q
#q
##
##
...

Is there a good Vi(m) command for transposing arguments in a function call? Bonus points for Emacs

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

How to restrict operations to certain lines?

I have to work on some relatively huge code files in vim.
How do I restrict some operations like find-next normal-n and others to a certain function / block?
How would I visually know if I'm within that block or outside it?
Looking and line numbers seems awkward, specially that the line numbers I need to work with are generally 5 digits long!
You can set marks at the beginning and end of your block using m (e.g. ma, mb) and then refer to them in the range of a ex command as :'a,'b.
Like bignose said you can use a visual block to create an implicit region for a command, which can be passed to an ex command using :'<,'>
You can also use regexes to delimit a block (e.g. for all the lines between start and end use :/start/,/end/
For example, to make a substitution in a range of lines:
:'<,'>s/foo/bar/g
:'a,'bs/baz/quux/g
:/harpo/,/chico/s/zeppo/groucho/g
The last visually selected range is remembered so you can reuse it without reselecting it.
For more on ranges, see :help range
You can further restrict yourself within a range by using g//. For example, if you wanted to replace foo with bar only on lines containing baz in the selected range:
:'<,'>g/baz/s/foo/bar/g
When you define a new ex command, you can pass the range given to the ex-command using as <line1>,<line2>. See :help user-commands for more on defining ex-commands.
Within a vimscript function, you can access an implicitly passed range using a:firstline and a:lastline. You can detect your current linenumber using line('.'), and detect whether you're inside the block using normal boolean logic (a:firstline <= line('.') && line('.') <= a:lastline). See :help functions for more on vimscript functions.
Another approach, is to use vim's inner i and single a selectors. For example, to delete the entirety of a double quoted string, use da" in normal mode. To leave the quotes, use di". See :help object-select for more.
Vimtips has exactly what you were looking for:
Search in current function
See also :help pattern-atoms
Vim uses Shift-v to select by lines. Having selected a series of lines, many commands will then be restricted to the selection.
I don't think the search commands (/, n, etc.) are restricted that way though.
For commands like search and replace, you can easily limit yourself to a couple of lines:
:.,+3s/foo/bar/cg
replaces every occurrence of "foo" in the current line and the following 3 lines with "bar". I don't think you can do that for search, though.

Resources