Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select first word from each line in multiple lines with Vim

Tags:

vim

I would like to copy the first words of multiple lines.

Example of code :

apiKey := fmt.Sprintf("&apiKey=%s", args.ApiKey)
maxCount := fmt.Sprintf("&maxCount=%d", args.MaxCount)
id := fmt.Sprintf("&id=%s", args.Id)
userid := fmt.Sprintf("&userid=%s", args.Userid)
requestFields := fmt.Sprintf("&requestFields=%s", args.RequestFields)

I would like to have this in my clipboard :

apiKey
maxCount
id
userid
requestFields

I tried with ctrl-v and after e, but it copies like on the image : enter image description here

like image 565
GermainGum Avatar asked Mar 24 '17 14:03

GermainGum


People also ask

How do you go to the beginning of a line in vim?

Press 0 to go to the beginning of a line, or ^ to go to the first non-blank character in a line.

How do you select multiple lines in vi?

Manipulate multiple lines of textPress Shift+V to enter line mode. The words VISUAL LINE will appear at the bottom of the screen. Use navigation commands, such as the Arrow keys, to highlight multiple lines of text. Once the desired text is highlighted, use commands to manipulate it.


1 Answers

You could append every first word to an empty register (let's say q) using

:'<,'>norm! "Qyiw

That is, in every line of the visual selection, execute the "Qyiw sequence of normal commands to append (the first) "inner word" to the q register.

You need to have > in cpoptions for the newline to be added in between yanks (:set cpoptions+=>), otherwise the words will be concatenated on a single line.

If you want to quickly empty a register you can use qqq in normal mode (or qaq to empty register a).

Note: the unnamed register ("") will also contain what you want at the end of the operation, so you don't need to "qp to paste it, p will do.

like image 193
Marth Avatar answered Sep 27 '22 19:09

Marth