Multiple selections in VIM - vim

Is it possible to select multiple non-consecutive lines (or sections) in VIM's visual mode? If so, how?

No, this is not possible without plugins.
But you can copy multiple lines into the same buffer, if that solves your problem.
To start the 'Accumulation Buffer':
mark a section to copy in visual mode,
press "a to operate on the buffer a with the next command and
yank it as usual (y).
To add to that buffer:
mark the next section and
press "A (capitalizing the buffer name means "do not overwrite the buffer, append to it instead")
and yank again using y.
You can then paste the accumulated buffer a at any time using "ap.

You have to install the multiselect plugin to get this capability. Find it here: http://www.vim.org/scripts/script.php?script_id=953

A more up-to-date answer is this plugin.
(disclaimer: I personally don't actually use it, it interferes too much with the rest of my vim setup. If your vim is relatively clean and you are moving over from sublime, this may certainly be your cup of tea.)
I would also like to point out the record/replay functionality of vim (the q key). Quite often recording is also unnecessary, I can do the tasks normally done with sublime's multi-select by doing it iteratively (e.g. search for something, perform the fix on the first instance of it, and then subsequent repeats are achieved by hitting n and N to move around and . to repeat the edit operation).
I do have my , comma key nnoremap'd to #q, this repeats the sequence that is recorded by pressing qq (record into q register).

Related

What's the vim way to select multiple instances of current word and change them?

Anyone familiar with Sublime Text's multiple cursor feature will recognize the pattern of doing the following: press a hotkey multiple times to select multiple instances of the word under the cursor and automatically create a new cursor for each of those instances; then edit each instance simultaneously, e.g. by replacing the current word with another word or whatever you want.
The multiple cursors feature is available for vim via plugin. Before using that plugin, I want (as a new vim user), to check whether there is a more natively vim-like way to achieve the same task.
For instance, I know I could use the :s command to do a search and replace (per instructions here), but that requires me to (1) type in the word I want to replace (or use the <C-r><C-a> shortcut to do so), as opposed to simply using the current word and (2) define a range. Perhaps this is the native vim way to do it, perhaps (likely!) there's another way I don't know.
So what is the native vim way?
I use the *, gn, and the . to make changes.
Select current word with * (go back with N)
Change word with gn motion. e.g. cgnfoo<esc>
Repeat via . command
Note: If you have many changes then using a substitution command would probably be better.
There is a nice Vimcasts episode about the gn motion: Operating on search matches using gn.
For more help see:
:h *
:h gn
:h .
You can record macros in Vim by pressing q<letter>. Macros can include the n command to search for the next instance of a word. You can also go into insert mode while recording (e.g. using the c command with a motion such as iw to replace the current word). Press q to stop recording, and then press #<letter> to replay the macro once. After that, you can use ## to repeat the macro as many times as you like.
While waiting for other answers, I'm going to post what I'm experimenting with while waiting for vim experts to answer:
:.,$s/<C-r><C-a>/foobar/gc
to substitute (the s) from the current line (the .) to the last line ($) (with the comma denoting the line range), using the <C-r><C-a> combo to copy the current word into the command, then using gc to change with confirmation, so I can hit yes/no for each instance then quit when I've done enough.

Pasting text in vim. A tedious operation?

I've been using vim for somewhat longer than a year now and during this time I have never feel really comfortable with the way vim works with yanking and pasting text (or maybe it is just me not using it in the most efficient way)
For example, I have the word "World" yanked onto a register, and I want to paste it after "Hello". (Note that there are no spaces on either of the words). So, what I would do is
Hello
|
Place cursor here, and press "p". Then, what I will end up with is
HelloWorld
So, in order to avoid this, I have always to swith into insert mode, insert a espace, and go back into normal mode (or either make sure that the yanked word has a space before it). Be as it may, this is quite annoying behaviour I can't think of a solution for... Am I missing something here?
Suggestions will be appreciated.
Thanks
option zero
just live with what you have now.
option one
create a mapping for your workflow. for example
nnoremap <leader>p i<space><esc>p
option two
:set ve=all
then you could move your cursor to anywhere and paste
option three
you could in insert mode use <c-o> do normal mode stuff or <c-r> to get register values
I recommend option zero
You can use the Smartput : Adjust spaces and commas when putting text plugin for that. It modifies the p / P commands (this can be toggled on / off).

How could I go about changing which buffer Visual mode will empty to when I override text?

I've recently stumbled upon this useful feature:
nnoremap D "_d
Allowing me to do "Dd" or "D$" in order to delete things without overriding the default buffer. However,
nnoremap V "_v
doesn't seem to do the trick when I try and replace text by selecting and pasting over it.
Any suggestions on how I accomplish that?
Thanks in advance.
I don't quite understand what you are trying to achieve.
d is an operator, so you can decide to put the result of the delete operation into a given register; the _ register in your case.
However v is not an operator, it is only a command to switch from normal to visual mode. So I don't think that "_v makes much sense.
You might be thinking of another operator like c (change), y(yank) or p (paste)
To get the full list of operators, type :help operators
You should have a look at :help registers, you might find what you want, it describes all the registers and how they behave.
short extract :
"" is the default register
"0 always contains the content of the latest "yank"
"1 always contains the content of the latest "delete" or "change"
The other numbered registers are used to keep the history of your previous actions.
So rather than avoiding to delete the default register when you are deleting you might use :
"0p when pasting to be sure to paste the last yanked text and not what you have just deleted.
Edit : Add some Vim speak
A buffer is the in-memory text of the file you are editing. It contains the whole file. :ls will display a list of open buffers.
A register is a buffer (for lack of better word) where your deleted or copied text goes. It could be a word, a line, etc. :reg will display the content of all your registers.

Community Wiki: "Vim: Advanced usage of the yanking mechanism"

Community Wiki
As the documentation of the yank system shows (thanks Michal), The Vim yank system seems to be more intricate then a standard clipboard. I therefore think it beneficial if vim veterans could perhaps show us some different styles of making use of this mechanism. particularly with the usage of vim for complicated projects without the use of a heavyweight IDE (say C++ ?).
Original Question
Now that I am using vim for everything I type, rather then just for configuring servers, I wan't to sort out the following trivialities. I tried to formulate Google search queries but the results didn't address my questions :D.
Question one: How do I yank and replace multiple times ?
Once I have something in the yank history (if that is what its called) and then highlight and use the 'p' char in command mode the replaced text is put at the front of the yank history; therefore subsequent replace operations do not use the the text I intended. I imagine this to be a usefull feature under certain circumstances but I do not have a need for it in my workflow.
Question two: How do I type text without causing the line to ripple forward ?
I use hard-tab stops to allign my code in a certain way -- e.g.,
FunctionNameX ( lala * land );
FunctionNameProto ( );
When I figure out what needs to go into the second function, how do I insert it without move the text up ?
Question three Is there a way of having a uniform yank history across gvim instances on the same machine ? I have > 1 monitors. Just wondering, atm I am using highlight + mouse middle click.
Answer one: A relevant, if not particularly encouraging, qoute from the Vim docs (see :help put-Visual-mode):
When using a put command like |p| or |P| in Visual mode, Vim will try to
replace the selected text with the contents of the register. Whether this
works well depends on the type of selection and the type of the text in the
register. With blockwise selection it also depends on the size of the block
and whether the corners are on an existing character. (Implementation detail:
it actually works by first putting the register after the selection and then
deleting the selection.)
The previously selected text is put in the unnamed register. If you want to
put the same text into a Visual selection several times you need to use
another register. E.g., yank the text to copy, Visually select the text to
replace and use "0p . You can repeat this as many times as you like, the
unnamed register will be changed each time.
Answer two: R (the capital 'R') puts you in replace mode.
I'm missing answer three, I'm afraid.
Answer three: Not quite matching the "uniform yank history" spec, but "+y yanks to clipboard and "+p pastes from clipboard if a clipboard is available.
Yank into a buffer
:y b
yanks into buffer b
And
:p b
places it.
I think there are more named buffers available.

Vim: transitioning from mouse to movement

I use MacVim (and gvim) a lot. I'm familiar with and use a lot of the basic movement commands (b, w, $, 0, G). However, for a lot of things—such as selecting particular lines on the screen or jumping to a particular column in a different line—I use the mouse (sometimes in concert with my left hand on the keyboard). It also helps that my mouse has a scroll wheel and buttons for changing tabs.
I also need to admit... I use the arrow keys on my keyboard rather than hjkl.
I think that my speed (and posture at the computer) will be improved by not having to escape from insert mode, and from keeping both hands on the main part of the keyboard.
What convinced you to abandon the mouse? What are the most helpful shortcuts for moving quickly between lines and columns, scrolling, etc.?
This question is inspired by this recent post
I think that my speed (and posture at
the computer) will be improved by not
having to escape from insert mode
No, you must escape from insert mode right after you typed what you want. It quickly becomes a reflex, so you don't really lose time (I sometimes even press escape after completing a web form...). Normal mode isn't just for moving around, it's used to perform most operations (save from typing): for instance deleting or moving sections of text. You also benefit from entering insert mode with the appropriate key: o to start a line, S to replace a line (while keeping indentation), A to move to the end of the line, c+motion to replace a few words or until a given character... All of these save keystrokes.
The mouse seems fast, but in reality it isn't precise, so you lose time (in addition to the constant back and forth with the keyboard). ViM has a long list of moving commands (see :help usr_03) which, when mastered, are faster than the mouse in most situations.
Use search the most you can (/, ?, *, #, f, t...). I personally use Ctrl+(d,u,f,b) a lot. Also, Ctrl+(o, i) and `` are really useful to go back where you were before a search or something else.
h, j, k, l are there to place your right hand near to useful commands (i, u, o...): I always have my fingers on them. The arrows force you to move your hand a lot.
Try to look at a few commands in :help, then use them a lot, and you'll get habits about what you should use to move according to the situation. Nobody uses ViM the same way.
The more I used the keyboard movement in vim, the less I wanted to use the mouse. It doesn't help that extended periods of time where I'm constantly moving from keyboard to mouse can take a toll on my wrist.
If you want to force doing things the vim way, unplug your mouse for a while! The more you use the keyboard, the more you will love it. This worked for me.
For moving between lines, I usually just use jk but I often skip to lines using :line_num. Getting to a specific column in a line, I typically use wbe^0$ and put modifiers in front of w, b, or e if I'm skipping through several words. And there is also the shifted versions, WBE which also come in handy often.
I have to admit that I often use the arrow keys for specific movement in vim.
I rarely use hjkl. However, I find that most of my navigation is done with other commands such as w (skip a word forward), b (skip a word backwards). Combine this with modifiers such as 3w (3 words forward). : skip to a specific line.
I've never really had to abandon the mouse since I never really started with it. All I can say is that attempting to use editors without all the keyboard shortcuts that vim has can feel quite painful.
I'd run vimtutor from the command line (Terminal.app in OS X). It runs vim with a tutorial document. That document was what really made me realize the power of some of the commands. You'll pick up some that are most useful to you and gain more over time. Eventually you'll find yourself using the mouse less and less.
One command that will really improve your movement speeds is f. f plus a character will jump to the first occurrence of that character on the current line. Pressing ; will jump to the next occurrence. Of course this can be used in combination with other commands. So, for example, removing all characters up to and including the first closing parenthesis is achieved by pressing d+f+).
You can seriously revolve your life around jk in hjkl.
nnoremap <c-k> ddkP "move current line up one
nnoremap <c-j> ddp "move current line down one
vnoremap <c-j> dp'[V'] "move visual block down one
vnoremap <c-k> dkP'[V'] "move visual block up one
"These may be a bit more esoteric to me"
inoremap jj <esc>o "Insert mode can move to next line (works mid line)
inoremap kk <esc>O "Insert mode new line on previous line
also for the desktop gui (ion3 and gnome)
winj - next window
wink - prev window
(This beats alttab if your editing in vim all day)
also read :he motion.txt in its entirety using j and k to scroll up and down as you do.
I learned vi so for me the mouse has never been something to use with a text editor.
I don't use hjkl except when the machine/network/keyboard/whatever-in-between is not comfortably configured.
Use the mouse where it makes sense. It often does (copy/paste to and from other gui applications).
To answer your question, though: Once you start using vim as a tool for transforming text bits with macros, the movements will all start to fall into place ;)
I think you're wondering if there's a quick way to move around while in insert mode (without using the mouse or arrow keys) but unfortunately there isn't; you have to escape out of insert mode. However I know that jumping all the way to your Esc key can be really annoying, which is why I've gotten into the habit of escaping with Ctrl+c, it's much faster.
I almost never use hjkl because it's too tedious. I usually try to jump right to where I want to go. There's a great cheat sheet out there (a Dvorak version is a available also (although it's in PDF)) that you can stare at and imagine the possibilities.
Mostly I use f, F, t, and T, and sometimes they're enhanced by typing a number up front like d2t) will delete up to the second close paren. Sometimes I use search. A lot of the time I use w, W, b, B, e, and E. They're all fantastic.
When I see vim users arrowing all over the place in visual mode just to delete text it makes me shudder, because there are so much easier ways (and hjkl only make you move your arm less, they don't change the number of button presses).
Something I did that I don't know if it's common or not is remapped my arrow keys to be <Esc><Up> and so on. For whatever reason I started out always arrowing around and assuming I'd be back in normal mode, so I just made it do that...
The biggest bummer about Vim is that now when I edit anything anywhere else I hit escape all of the time and type :w after every change...

Resources