I found substitue in vim has a range parameter:
:[range]s/pattern/sring/[c]/[g]/[e]/[i]/[I]/[p]
But I'm wondering if it would be handy to let the substitution occur only in the visual selection.
Anyone know how to do that?
Yes, you could do that.
After visual selection, press : will give you :'<,'>, '<,'> is the [range] part.
Then do the substitute like below but note that this will affect the entire line(s):
:'<,'>s/foo/bar/g
If you would like to change foo to bar only in the visually selected area (i.e., character-wise or block-wise selection) then you'll have to use the \%V atom:
:'<,'>s/\%Vfoo/bar/g
The article Search and replace in a visual selection (archive) has much more useful info on the topic.
To limit the scope to the visual lines, the '<,'> range does the job. For a characterwise or blockwise selection, this isn't sufficient, though. You need to modify the regexp or use the vis.vim plugin; otherwise, also characters outside the selection (but within the line(s)) will be affected, too.
Find the complete discussion on the Vim Tips Wiki: http://vim.wikia.com/wiki/Applying_substitutes_to_a_visual_block
NOTE: This answer is from 2012, and the \%V atom can now (4/2/2021) be used to restrict changes to inside the visual selection (character-wise or block-wise). See the article Search and replace in a visual selection (archive), but the article above has also been updated since.
Related
I recently started using vim for writing markdown and latex files. Visual mode works great when copying lines or blocks of code, but when I'm writing, I often need to copy non-rectangular chunks of text. For example, what I'd like to be able to do is this:
But, with visual mode I can only seem to do this:
or this:
l
Is there a way to select the entire first line and only a portion of the second line?
All of Vim's modes are listed under :help vim-modes. Here is what it says about visual mode:
Visual mode This is like Normal mode, but the movement commands
extend a highlighted area. When a non-movement
command is used, it is executed for the highlighted
area. See |Visual-mode|.
Where you get a handy link to :help visual-mode, which is a whole section delving into every detail of the three visual modes, started with v, V, and <C-v>.
Note that it says this near the top:
This is introduced in section |04.4| of the user manual.
This means that visual mode is taught very early in the user manual and, by consequence, that it is a very basic and foundational topic. It also implies that going through the linked section of the user manual is a prerequisite.
This is the second paragraph of that very basic section:
You start Visual mode by pressing "v". You move the cursor over the text you
want to work on. While you do this, the text is highlighted. Finally type
the operator command.
Note that it says v, not V.
Visual mode, by way of v, again, is also casually introduced in lesson 5.3 of $ vimtutor.
All that to say that, everywhere visual mode is mentioned in the built-in documentation, from the very basic vimtutor to the very extensive reference manual, v is systematically mentioned first.
It used to be part of my Vim vocabulary... change to visual mode, place cursor to a search word, type * and all text between first and next search word would be highlighted. The highlighting can continue as long as you repeat pressing * and there is the next matching search word. Perfect set of commands to copy text, or simply as visual aid.
Coming back to Vim now after some time, this is not working on my configuration. What happens now is that only the letter under the cursor and all other such letters get highlighted.
I wonder if I have some conflict in my mappings or else? I use Vim v7.3. Anyone can help?
Vim does not have a visual mode command for *; this only works in normal mode. However, many people have a customization that supplies the super star visual variant, from Search for visually selected text - or a plugin like my SearchHighlighting plugin; its plugin page has links to many alternative plugins.
I guess you had something like that, too, but forgot about it. I also doubt that is was triggered by Ctrl + *; combinations of Ctrl and non-alphabetical letters mostly aren't available as unique mappings. The usual mapping is just *, overloading the original normal mode command for visual mode.
Vim 7.3 is from 2010 and very outdated. To become reacquainted with Vim (which I commend!), please choose a modern variant. It should be possible to install the latest version 8.1; if you can't find a proper package for your distribution (for Windows, check the binaries from either vim-win32-installer or tuxproject; on Linux, it's also not very difficult to compile it yourself (e.g. from the GitHub sources).
It's the first time i noticed about the selection mode in vim when i accidentally triggered it from visual-line mode by <c-g>.
Vim already has visual mode for text selection what's the use case of selection mode, can anyone give me a hint on this?
(note: i've checked the manual page which describes it as
a resemblance of MS Windows text selection
but i still cannot quite understand why do we need any mouse actions in vim)
Select-mode is commonly used in snippet plugins such as vim-snipmate. Mappings can be created that are unique to select-mode so as not to interfere with regular visual mode behaviour.
Here is an excerpt from Practical Vim by Drew Neil:
If you are happy to embrace the modal nature of Vim, then you should
find little use for Select mode, which holds the hand of users who
want to make Vim behave more like other text editors. I can think of
only one place where I consistently use Select mode: when using a
plugin that emulates TextMate's snippet functionality, Select mode
highlights the active placeholder.
As the documentation suggests, select mode is a bit different from visual mode. Here's the commands you can do in it:
Commands in Select mode:
- Printable characters, <NL> and <CR> cause the selection to be deleted, and
Vim enters Insert mode. The typed character is inserted.
- Non-printable movement commands, with the Shift key pressed, extend the
selection. 'keymodel' must include "startsel".
- Non-printable movement commands, with the Shift key NOT pressed, stop Select
mode. 'keymodel' must include "stopsel".
- ESC stops Select mode.
- CTRL-O switches to Visual mode for the duration of one command. *v_CTRL-O*
- CTRL-G switches to Visual mode.
Otherwise, typed characters are handled as in Visual mode.
When using an operator in Select mode, and the selection is linewise, the
selected lines are operated upon, but like in characterwise selection. For
example, when a whole line is deleted, it can later be pasted halfway a line.
You can see the docs here.
What they mean by "resembles MS Windows selection" is probably that you can extend the selection with Shift+Arrows, and also that any printable characters entered will substitute the selected text and enter insert mode.
Also, see this question/answer at the vi.SE.
Is there something like % (that represents the whole buffer in Vim command line) for the current selection, so that I can do something like: :# sort (Imagine # represents the selection).
EDIT:
Sorry, I missed to mention that I am requesting for a way to operate on block selections not on ordinary selections that can be operated using ranges '<,'>.
Yes. Example:
:'<,'>!sort
The range :'<,'> represents the visually selected lines.
:* is shorthand for :'<,'>
If you hit : while in visual mode it will start the command with '<,'>
For more help see:
:h '<
:h v_:
:h range
You are probably looking for the marks '< and '>:
:'<,'>sort
If you just select a few lines and hit : to enter the command line these marks should appear automatically.
As others have already remarked, the visual selection is represented by the '<,'> range (there's also the :* short form). However, as all ranges, this always covers entire lines, even if the selection is only characters or a block.
Most Ex commands (like :sort) only operate on full lines, for :substitute, you can limit the effects to the visual selection by including the special \%V atom in the search pattern, cp. :help /\%V.
The idea to make Ex commands handle blockwise selections is old, but unlikely to be tackled any time soon.
Ok, this was previously a question about Vim until I learned it was particular to the Visual Studio plugin ViEmu.
In ViEmu, 'v' puts the highlighter cursor between the previous character and the current one, such that walking backward leaves the letter that was under cursor when 'v' was pushed unselected. This is particularly annoying when trying to use visual mode from the end of the line. Is there a key that is to 'v' as 'a' vs. 'i' and 'p' vs 'P'.
Here's an example:
I have the following text with my cursor sitting over the trailing s of the word dances.
The fat yellow dog dances
and I wish to to change it to:
The quick brown fox jumps
I'd like to enter visual mode with the highlighter positioned to the right of the s in dances. That way, as I use shift+f to search backward to say the f in fat I select everything. Using v->shift+f->f will highlight everything but the s in dances which is annoying.
If we think about how a change made with an operator, say d3e, is different from the same change made using Visual mode, v3ed, we find that the distinctive element in Visual mode is that of interactivity.
In Visual mode it is natural to start selecting and then steadily honing in on the target area: Instead of v3ed I might as well use veeed or veeeebbed or v4ebbed. Or maybe after vee I realise I need to include stuff that comes before my selection, thus o, bb, and finally d.
The point is, when using
operators, we need to be precise: operator plus target – a hole-in-one shot;
Visual mode, we are free to employ the full array of Vim's motions to describe our target area, whatever shape it may be, and there's no pressure to be precise or hit the mark immediately.
In case you often need to start Visual mode just after the current cursor position (or you have another similar use case), you can always create a custom mapping. Here are some ideas:
:nnoremap <Leader>v <Space>v
:nnoremap <Leader>v $hv
:nnoremap <Leader>v $F;v