Like you would in virtually any application editing text. For example:

this is a big sentence so I can explain
^                         ^
start of selection        my cursor is here

I'd like to copy only up to the cursor but not including I.

Any way to do this?

  • I suggest go through $ vimtutor. Otherwise you're gonna keep getting in many of same problems. These are very trivial actions, so, you really need to read the manuals. Start with vimtutor, go through :h intro.txt. – klaus Mar 26 '19 at 18:11
  • 2
    Welcome to our site @ LukeSkywalker! I'm undeleting the question because @Klaus put some effort in the answer and the question isn't bad or off topic so there is no reason to make them loose their answer – statox Mar 28 '19 at 12:57

Answer: y^


vim is a modal editor. That means it has specific modes for doing specific things. Most common three modes are:

  1. Normal Mode : Enter all normal editor commands (see :h Normal-mode)
  2. Insert Mode : Insert characters (see :h Insert-mode
  3. Visual Mode : Highlight a text area to perform a normal editor comman on (see :h Visual-mode

But there are many other modes also. For more info, see :h intro.txt

In normal mode, you can perform editing tasks, for example, yanking (a.k.a copying) text. You can do that by two methods:

  • Normal mode : You need to perform :h Operator-pending-mode to yank the portion of the text you want to. Vim has many text objects (see :h motion.txt). You can go in operator-pending mode by issuing a command, in this case, yank (y) and then specify the text object to perform the command (yank) on with suitable text objects.
    If you want to yank from the start of the line til the character under the cursor, I'd do y^

  • Visual mode : You go into visual mode with v and highlight the portion of the text you want to perform the yank command on, and then issue the command. I'd do hv^y

  • I am sorry, for some reason the wrong version of my question was posted... My question was more precise. – Luke Skywalker Mar 26 '19 at 18:09
  • What do you mean? I see that you edited your question, but my answer does apply to your edited (and more specific and explained) question. If the answer doesn't fulfill your requirements, i.e. doesn't solve your problem, please report back. – klaus Mar 28 '19 at 13:26
  • I believe if I follow your instruction I will copy the selection including the character under the cursor. Which is what I am trying to avoid. – Luke Skywalker Mar 28 '19 at 13:32
  • 1
    @LukeSkywalker But it is working as you want in my vim. Can you confirm that you issue the command y^ when your cursor is on the character you want to exclude from the yanked text? – klaus Mar 28 '19 at 13:52
  • 1
    @LukeSkywalker Plug 'tpope/vim-fugitive' let's say my cursor is on the hyphen of vim-fugitive and then I issue y^. It should yank the text of Plug 'tpope/vim and you can check what is yanked in the default unnamed register by :reg" – klaus Mar 28 '19 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.