I'd like to search and replace to the end of the code block I'm in.
Essentially I want to search over the motion ]}.
I tried
:]}s/=/+=/gc
and it didn't work. "Parse error"
I'm working with VsVIM in Visual Studio if that makes a difference.
The : ranges do not take motion arguments. You could do this via visual mode:
v]}:s/=/+=/gc
Note that when you hit : in visual mode, it will embed the ranges for you.
You can select the block with
{
Shift-v
}
then make:
:s/src/dst/g
I'm not sure if that will work in VsVim but you could do something like this in Vim:
[{v]}:s/=/+=/gc
Note that you can use a numeric range too:
:56,75s/=/+=/gc
As hepta said, you could do visual mode then your replace.
You could also do :.,.5s/old/new/gc
Or :.,/}/-1s/old/new/gc
See vim.wikia.com/wiki/Ranges
Related
How would you surround the following text with 3 backticks ``` using tpope's Vim Surround.
All I can is 1 backtick using S` in visual mode:
This is not what you asked but this can be done without surround:
(from visual mode)
c
```
<C-r>"
```
<Esc>
See :help ctrl-r.
Define a custom surround:
(Insert following in your .vimrc or file specific config ~/.vim/after/ftplugin/markdown.vim )
" Custom surrounds
let b:surround_{char2nr('c')} = "```\r```"
now visual select and Sc will give you desired surround.
Or use a snippet solution; for example using Ultisnips define a snippet like so:
snippet code
\`\`\`${1}
${0:${VISUAL}}
\`\`\`
endsnippet
now visual select your desired lines then hit snippet expansion key ( mine is Tab ) type code and hit Tab again. that's it.
Here another ultisnips solution.
snippet code "add backtics codes" w
`!v repeat(nr2char(96),3)` ${1:markdown}
${0:${VISUAL:type here}}
`!v repeat(nr2char(96),3)`
endsnippet
If you do not want "markdown" after the first line just get rid of it. I am showing this solution only to show how to avoid backslashing so much.
I've got a text file:
hello world ssh!
this is your destiny
Oh my goodness
In visual mode, I select from "world" to "my": I wish to change letter 's' into 'k' in my selection.
I definition cannot use line-mode because in that way the 3rd line's "goodness" will be changed.
So how to do this replacement in visual mode?
To achieve the same in normal mode (after you've made a selection and escaped from it), use the \%V atom along with the % range modifier together in a substitute query
:%s/\%Vs/k/g
:s/foo/bar - a substitute query
% - makes sure it applies to all lines in the file
\%V - limits the range to the last visual mode selection (i.e. the one you get with gv)
g - a global flag which makes sure all occurrences will be affected (instea of the first one in each line, which is the default behavior)
More at http://vim.wikia.com/wiki/Search_and_replace_in_a_visual_selection
You can use \%V to restrict the search to the visual selection:
:'<,'>s/\%Vs/k/g
See :help \%V
In Vim, after I installed vim-snipmates, I want to use this snippet to auto complete brackets.
snippet (
(${1})${2}
But, I encountered a problem, if I input things like this:
The last bracket is completed by the snippet when I input the first ( and tab, and this time when I press tab, I want to add another ) to the end and make things like this:
But, it turns out the Vim thinks I want to input a ) to finish everything, so it replaces the last ), and there is still one ) at the end of the line.
I want to know which plugin did this replacement and how can I disable it.
.vimrc
Bundle "MarcWeber/vim-addon-mw-utils"
Bundle "tomtom/tlib_vim"
Bundle "garbas/vim-snipmate"
Bundle "honza/vim-snippets"
I feel it's very hard to describe it.
Rather than using a snippet plugin, say Snipmate, Neosnippet or Ultisnips, the job of auto close parens, brackets or quotes )}]"' is better suited for an autoclose plugin, such as Delimitmate or autopairs.
The second < tab > didn't work like define but jump to the ${2}
When I'm coding in C/C++, when I type '{' I generally want the next couple characters to be a newline, tab, newline, ended by '}'. This is especially convenient with top-down programming so that you can make the format of your function and continue on, only to come back later and it has already been prototyped for you.
So, to clarify, I want '{' to be replaced by
{\n\t\n}
Is that possible in vim, and if so, how?
is this ok for you?
inoremap { {<cr>}<esc>O<tab>
with this mapping, if you type foo{ in INSERT mode, it will change to:
foo {
I
}
I is cursor position.
You may be interested to try out some of the many "auto closing" plugins available. I use DelimitMate but you could try AutoClose or AutoPair.
You have a bunch of yank registers. Perhaps the simplest solution would be to yank that to a designated register and then put from that register each time you need it.
For that kind of "top down" coding with "auto prototyping", snippet plugins are excellent.
With a snippet plugin you can do much more extensive "top down" coding than just expanding brackets.
Ultisnips is a great snippet plugin. If you don't have python support in your vim, then SnipMate, the predecessor of Ultisnips, is quite capable also, but not as advanced.
With Ultisnips, you could use any of the three snippets below:
1) Create the brackets and place cursor following them
snippet {
{
}
$1
endsnippet
2) Create the brackets and place the cursor within them
snippet {
{
$1
}
endsnippet
3) Create the brackets and place the cursor within them, then Ctrl-j to place the cursor following the brackets
snippet {
{
$1
}
$2
endsnippet
But in addition to simple snippets like these three, you could get much more sophisticated, defining whole function/class/etc templates in snippets that can be quickly expanded and jumped through.
I have been using vim for a couple of years now, and though I have learnt a lot of time saving shortcuts, but I have never used the Visual mode, which is supposed to be all powerful :
... Visual block mode (to edit columns) is something many editors lack, but that I can't live without. I have shocked and awed people at work using just this, making some edit in a few keypresses that someone would've otherwise spent ten minutes doing manually.
I want to understand why and when should I be using Visual mode.
Can someone give me an example of "making some edit in a few keypresses that someone would've otherwise spent ten minutes doing manually"?
If you see CTRL-CCTRL-V and recognise what it does, you should use visual mode.
If, like me, you see A:esc0df:$p$x as an edit command, don't bother :-)
When I use visual mode, it's to select whole lines or blocks. For example you can do [esc][shift+v][y] to copy the currently line I'm on. Here's more information.
Visual mode allows you to perform an operation on a block of text. It is the only way to perform an operation on a block in Vim.
A simple example of this would be copying or moving text.
A more advanced example would be sorting the lines in a certain part of a file. You can do this by entering visual mode, selecting a block of text, pressing Esc to enter command mode, and typing !sort. You can see more details about his example and how it works here: http://www.oualline.com/vim-cook.html#sorting_visual
I actually just did a screencast showing off great uses for visual mode. You can check it out at http://lococast.net/archives/241
As other's have said, it's great for doing any sorts of editing (edit, remove, search/replace) withing a specific range of code.
Insert a column of commas.
Delete a column of commas.
Act on rectangular selections.
Act on a section of text that is not a line or word.
Several good examples have already been given. Here are some others.
I also use visual mode:
To swap two regions of text.
To refactor out variables, types, and functions.
To surround the selection with pair of things (usually brackets)(NB: surround.vim also does this I guess), but also with control statements.
But simply most of the time to have a more interactive way to yank or change text.
To paste over without overwriting the current unnamed register.
To highlight the current selection (with Mark.vim)
And to do many other things I don't remember right know.