I have a visual selection in vim spanning several lines and i want to "convert" that into a visual line selection. Preferably in a succint way if possible. Without just selecting the same lines again ofcourse, that doesn't seem like the vim way to do it.
I feel I could probably figure it out but I'm betting someone here knows a neat way to do it.
To switch from visual selection to visual line selection just press V (uppercase).
To switch back from visual line selection to visual selection, you can press v (lowercase) again.
Related
In all Visual Studio IDEs whenever you press Ctrl-F or Ctrl-H to open the search/replace edit box the IDE always places there the text under cursor. Is there way to make it to leave the present text intact instead? In all Delphis there is an option which I always uncheck: "Find text at cursor", but I couldn't find similar in MSVC.
In normal text edit after I select and copy, the selection stays still, so I can copy again quickly. But in vim when I press y in visual mode, I enter normal mode, so I need to select again. I want a quicker way.
Reasons:
When testing a console app, I copy test data from text editor and paste into console. Then I find bugs and edit code, the clipboard may change and I need to copy and paste test data to test again
You can reselect last visual region typing gv
*gv* *v_gv* *reselect-Visual*
gv Start Visual mode with the same area as the previous
area and the same mode.
In Visual mode the current and the previous Visual
area are exchanged.
After using "p" or "P" in Visual mode the text that
was put will be selected.
You do not need to be in visual mode in order to yank text. While in normal mode, the yank command can be combined with a motion: y{motion}. You can read more about that here: http://vim.wikia.com/wiki/VimTip312#Copying_and_cutting_in_normal_mode
It also sounds like you would like to yank text into the system clipboard. On most Linux distributions, you can do this by yanking text into the + register. Depending on the distro, you may need to install gvim.
Let's say that I've got the following text:
This allows you to select some text using Vim's visual mode
With the cursor on the 'a' in 'allows'. If I type vaw I switch to visual mode and then select "a word" ("allows "). If I repeat the aw then Vim also selects the "you ". Let's say I do this once more (highlighting "to ") and then realize that I've gone too far.
Is there a way to shrink/reduce the size of the visual area (other than using the h,j,k,l keys)?
I'd love to be able to either 'undo' my last command (i.e., have Vim move the selection back to before I typed that last 'aw') or else use the motion/text object commands to reduce the size of the visual area.
yes, you can keep pressing ge till it selecting allows again.
Based on your post (and on this one), I decided to make the vim-visual-history plugin that keeps
a traversable record of previous visual selections with [v, ]v, [V and ]V. So
in your case, you would just do [v to go back one selection. You can keep on pressing [v to go back further in time, and use ]v to go forwards again. The plugin works for your case, and also selections using text objects etc.
[count][v : Reselect previous visual selection
[count]]v : Reselect next visual selection
[V : Reselect first visual selection
]V : Reselect last visual selection
You can also give a count to jump through the history faster:
I have the following CSS that I want to comment out with "//" at the beginning of each line (using Sass).
a:focus {
outline: thin dotted;
}
With my cursor on the first line I enter visual linewise mode and select 3 lines Vjj. To comment I type I//ESC. What I expect to happen is all lines have the text "//" prepended but instead only the first line has been modified.
Alternatively if I use visual blockwise mode to select (i.e. Ctrl-vjj) the lines and press I//ESC I receive the expected result of all lines prepended with "//".
My assumption has been that linewise and blockwise modes were merely different ways to select text. If I wanted to select all text of multiples lines the selection commands were interchangeable as long as I was able to select the text to modify. But the behavior above leads me to believe there's a difference I don't yet understand.
Is it possible to use visual linewise mode to accomplish this task or is it just the wrong tool for the job? Also documentation on the differences between the two modes would be greatly appreciated.
If you are in linewise visual mode you can use normal to accomplish what you want.
:'<,'>norm I//
normal runs the command I// on every line in normal mode.
Character-wise, line-wise and block-wise visual modes all allow you to select text across multiple lines.
Character-wise visual mode is mainly useful when you don't care about or don't want to deal with "lines".
Line-wise visual mode is to be used when dealing with whole lines is important.
Block-wise visual mode is a convenient way to repeat a change across multiple similar lines. I like to see it like "a window inside the window" that allows me to act on a subset of my current buffer.
The one you choose is dictated by what you plan to do with that selection but their behavior differs only when doing visual mode commands: because Ex commands are always line-wise, they don't care about the specifics of your visual mode beyond the first line and the last line of the selection.
I would prefer to use visual mode and invoke :'<,'>s#^#//#
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.