vim: highlight words in visual block mode - vim

Below is a screenshot of me entering visual block mode and pressing "w" to select by word:
How can I select every word in the rows I have selected? Meaning I want the full word in the rows highlighted instead of it getting cut off as shown in the screenshot.
edit: What I want to be able to do is delete a column of words of varying length. In the example screenshot I want to delete the words between the tags, But it could be any column of words.

There are a bunch of plugins for multiple selection, look them up on vim.org.
But I must remind you that visually selecting text is more often than not an unnecessary step. Why don't you explain what you actually want to achieve instead of your failed attempts? Maybe there's a better way...
[edit]
:'<,'>norm dit
seems to be the simplest way to achieve your goal without selecting every word:
and :,+7norm dit would be even better because you don't select anything.

The highlight modes can only select blocks (by cursor, by line, or by rectangular block). You can use a plugin such as vim-multiple-cursors to do what you are trying to do.

The only place where Vim allows a non-rectangular, "jagged edge" visual selection is at the end of the lines, i.e. by extending the blockwise selection with $. Therefore, you'd need to (temporarily) get rid of the trailing </th> (or include it in the selection, but operate in such a way that they are kept intact).
You shouldn't need a selection to work with the text. For example, to delete the text inside the tags, you can use a substitution:
:%s#<th>\zs.*\ze</th>##

You can't. You can only select rectangular blocks in block select mode. Maybe a plugin solves this?

Related

Any ideas on how to format these lines in vim?

I have the following lines that I've already formatted quite a bit, but I can't seem to figure out how to format these jagged lines so they are all in the correct position. I've been trying using macros and also using /s but it just seems the tabs or spaces after the word is a bit different for each. What might be a good way to format this here?
You can select the entire part you want to format and then use the normal command
'<,'>s/\([^: ]\+\)[ ]*:[ ]*\([^: ]*\)/\=printf('%-8s%8s%s%8s%8s',submatch(1),' ',':',' ',submatch(2))/g
which will pad the result evenly around the :.
You can select in vim using v then enter normal mode with esc followed by entering command mode with :. Paste the above line in command mode once the selection is made and hit enter.
Use vim-easy-align. And Select the text you need to align. Input ga*:. Done.

Is it possible to simultaneously get the contents of a visual selection into a register and replace all the characters?

Consider the following text file.
Replace and yank this portion Ignore this portion
Suppose I have visually selected the part that says Replace and yank this portion.
I can take one of the following actions at this point.
I can use y to yank the contents into a register, but this destroys the visual selection.
I can use rx to replace each of the characters with an x, but this also destroys the visual selection.
Is it possible to simultaneously put the visual selection into a register and replace each of the characters in the visual selection with an x?
That is, I'm looking for a sequence of commands that result in the selected text being in a register, and each character in the selected text replaced by x. I'm not picky about which register.
Immediately after posting this question, I realized that all I needed was to be able to re-select the text that was just selected.
A quick Google search led to using gv for re-selection.
Thus, the final command sequence to achieve the desired effect is ygvrx. This will first yank the sequence into the register, re-select the previous selection, and then replace the characters.
Visually select the text and press c for change. Type the text you want and press <esc>. The text that was there before (in this case Replace and yank this portion) is now in your "" register, so you can just hit p as soon as you want to paste it.
type :h reg to see a list of all registers and what text you have inside them.

How do I search within a selected block of code with vim

say, I want to search for a particular text withing a function block.
The present way, that i am implementing is selecting the block of code from within the function brackets with vi{ and then copying it and pasting it to a new file. After that I am searching for the text within the new file with /<search-text>
I want to know, if there is a short cut to this?
vi{
:'<,'>g/foo/#
The '<,'> range is inserted automatically.
See :help range and :help :g.
I think this might be what you are looking for:
Limiting search scope for code in Vim
Using /\%Vsearch pattern should get you what you want after you have selected the block of code you wish to search in. You enter visual mode by hitting v and moving the cursor around to highlight the block you are searching in.
The almost exact same question has been asked last week on vi.SE.
While \%V can restrict the search to the current visually selected text (which is the precise answer to your question, but not to your indirectly expressed need), selecting the current function is much more tricky than a simple vi{. A perfect and simple way to select the current function requires scripting. That's where my answer on vi.SE kicks in.

Vim real tab characters start at column 8- I can't move all the way left

I'm using real tab characters in my files, and when whitespace characters are set to invisible (which it is by default), then the farthest left I can go is column 8. But if I make whitespace visible (:set list!) then it still says that I'm at column 8, but it at least shows my cursor all the way to the left. What could be causing this?
This is not a matter of correct or incorrect. Vim just chooses to put the (single cell) cursor on the last cell of the Tab, and the jumping forward movement probably makes Tabs easier to detect, so most people like that behavior. Only with :set list, where a Tab is represented by a start and follow-up characters does this change.
If you really can't get used to it, the only workaround (short of modifying Vim's source code directly) is:
set list listchars=tab:\ \ " Note: trailing space after the last backslash!
Note that this has other side effects, e.g. when soft wrapping words.
It's not a bug or a problem, other than a visual annoyance. In the upper screenshot your cursor is at first character of the line. Try a character modifying command like rx on it, you'll see.

Paste multiple times

What is the best way replace multiple lines with the contents of the clipboard?
The problem I'm having is when I yank a line and paste it over another line the "yank" is replaced with the line I just replace. Now, if I want to replace another line with the same line I have to go back up and yank it again.
There's got to be a better way to do this.
I have this in my .vimrc:
xnoremap p pgvy
(note: this will work only with the default register, but this mapping is easy to remember). Writing a more elaborate version would be possible. Also, you still can use P to get the old behaviour.
"0 should have the contents of your yank. It's a bit more tedious to type, but "0p should do what you want.
Alternatively, don't select-and-replace the old lines up front. If you find those lines with a search, just hit n. over and over (after an initial p), then when they're all pasted, do ndd followed by as many n.s as necessary.
The biggest mental switch I've needed to make when moving to Vim is to figure out how to apply group edits sequentially. I.e. rather than doing a bunch of edits on a line and then doing a bunch of the same edits on another line, I'll do the first edit on a bunch of lines (using . to great effect), then the second edit on a bunch of lines, etc. Alternatively, the use of macros may help as they are fantastic, but sometimes a little more tedious to get working correctly with "complex" changes.
I often use another registry, copy the line you need to some named registry "ay and then paste from there "ap
When you paste over a selection in Vim it will replace the default register with the contents of the selection. If pasting over a selection is wiping out the contents of the clipboard register then very likely you have the following line in your .vimrc
set clipboard=unnamed
One option is to remove that and use the explicit clipboard register "+
Another option is to use any of the other explicitly named registers (a-z). After the first paste yank the line back into "c for example and then use "cp to paste from there on out.
Instead of using copy/paste, it is often better to use a text object command such as ciw to change the inner word. This method has the advantage of being easily repeatable using the . repeat command.
yiw Yank inner word (copy word under cursor, say "first").
... Move the cursor to another word (say "second").
ciw<C-r>0 Change "second", replacing it with "first" ( is Ctrl-R).
... Move the cursor to another word (say "third").
. Change "third", replacing it with "first".
use np where n is the number of how much time you want to paste the lines eg 3p will paste 3 lines.

Resources