Skip to main content
Fix typos
Source Link
Jair López
  • 1.9k
  • 1
  • 16
  • 24

As I said in the comments using multi cursors (even with a plugin) isn't really "following the vimVim way", I totally understand that it is attractive for someone coming from Sublime-Text but you can often find alternatives which are at least as efficient with vimVim built-in features.

Of course, finding these alternative solutions isn't always easy and sometimes it takes time but it will get easier with your Vim experience and you'll see that with time multiple cursors will seem totally useless to you.

The strength of this command is that the last change can be an action working on a character, a line or a whole file. For example, a change can be delimited beby the moment you enter insert mode and the moment you go back to normal mode.

All the challenge of the dot command is to learn how to make repeatable changes: it will come with groking vimgrokking Vim but the basic is to understand how to make your change in a repeatable way.

Because A; createcreates an atomic action so when you'll use . on another line, no matter where you are in the line you'll insert the semi colon at the end. Whereas when using $a; you split your change in two parts $a and the insertion of ; so if you use . it will insert the semi colon on the current position of the cursor.

Macros are another extremely important tool in vimVim since it allows you to record a sequence of keystrokes and repeat it as if you typed it again.

I'll use, as an example, your second use case:

  • Now you can use the macro to repeat your edit. As you are on the right line to edit you can simply execute the macro with @q. As we want to execute it two times twice you can use 2@q and you'll get the following result:

NOTE 1 As you may have noticed, using Oea0ea at the beginning of the macro was really important. Indeed, if you had put your cursor at the end of the first word before recording the macro and executing it again your result would have been:

The final @q@q would have called the macro by itself instead of using 2@q; you'd just have used @q and all the work would have been done.

Here comes another trick that doesn't directly apply to your use case but can'tcan be really useful to edit a large number of line at the same time. Let's get this extract of CSS code:

For more details on the [range] parameter please see :h :range. I wontwon't detail it here, I'll simply remind that % represents the whole file, '<,'>''> represents the last selection, and 1,5 represents the lines 1 to 5 of the file.

This parameter defines the lines which will be treated by the global command. If no range is precisedspecified, then the global command will use % by default.

NOTE 1 An important point that took me sometimesome time to realizedrealize is that the normal command is an ex command which means that you can use it with the global command. That can be really powerful: let's say that I want to duplicate all the lines which contains echo, I don't need a macro or even the magic formula n.. I can simply use

global has an opposite command vglobal abbreviated v which works exactly like global exceptedexcept that the command will be applied on lines which doesn'tdon't matchesmatch the [pattern] parameter. This way if we apply

As you can imagine these examples are pretty simple and are just made to demonstrate that when you follow the Vim way you really rarely need several cursors. My adviseadvice would be when you encounter a situation where you think it would be useful, write it down and take some time later to find a better solution. 99% of the time you'll eventually find a faster/more efficient way to do it.

Also I will repeat myself one more time but I really encourage you to read Practical Vim ofby Drew Neil because this book is not about "How to do that or this in Vim" it is about "How to learn to think in the Vim way" which will allow you to built your own solution to your future problems in a good way.

As I said in the comments using multi cursors (even with a plugin) isn't really "following the vim way", I totally understand that it is attractive for someone coming from Sublime-Text but you can often find alternatives which are at least as efficient with vim built-in features.

Of course finding these alternative solutions isn't always easy and sometimes it takes time but it will get easier with your Vim experience and you'll see that with time multiple cursors will seem totally useless to you.

The strength of this command is that the last change can be an action working on a character, a line or a whole file. For example a change can be delimited be the moment you enter insert mode and the moment you go back to normal mode.

All the challenge of the dot command is to learn how to make repeatable changes: it will come with groking vim but the basic is to understand how to make your change in a repeatable way.

Because A; create an atomic action so when you'll use . on another line, no matter where you are in the line you'll insert the semi colon at the end. Whereas when using $a; you split your change in two parts $a and the insertion of ; so if you use . it will insert the semi colon on the current position of the cursor.

Macros are another extremely important tool in vim since it allows you to record a sequence of keystrokes and repeat it as if you typed it again.

I'll use as example your second use case:

  • Now you can use the macro to repeat your edit. As you are on the right line to edit you can simply execute the macro with @q. As we want to execute it two times you can use 2@q and you'll get the following result:

NOTE 1 As you may have noticed, using Oea at the beginning of the macro was really important. Indeed if you had put your cursor at the end of the first word before recording the macro and executing it again your result would have been:

The final @q would have called the macro by itself instead of using 2@q you'd just have used @q and all the work would have been done.

Here comes another trick that doesn't directly apply to your use case but can't be really useful to edit a large number of line at the same time. Let's get this extract of CSS code:

For more details on the [range] parameter please see :h :range. I wont detail it here, I'll simply remind that % represents the whole file, '<,'>' represents the last selection, and 1,5 represents the lines 1 to 5 of the file.

This parameter defines the lines which will be treated by the global command. If no range is precised, then the global command will use % by default.

NOTE 1 An important point that took me sometime to realized is that the normal command is an ex command which means that you can use it with the global command. That can be really powerful: let's say that I want to duplicate all the lines which contains echo, I don't need a macro or even the magic formula n.. I can simply use

global has an opposite command vglobal abbreviated v which works exactly like global excepted that the command will be applied on lines which doesn't matches the [pattern] parameter. This way if we apply

As you can imagine these examples are pretty simple and are just made to demonstrate that when you follow the Vim way you really rarely need several cursors. My advise would be when you encounter a situation where you think it would be useful, write it down and take some time later to find a better solution. 99% of the time you'll eventually find a faster/more efficient way to do it.

Also I will repeat myself one more time but I really encourage you to read Practical Vim of Drew Neil because this book is not about "How to do that or this in Vim" it is about "How to learn to think in the Vim way" which will allow you to built your own solution to your future problems in a good way.

As I said in the comments using multi cursors (even with a plugin) isn't really "following the Vim way", I totally understand that it is attractive for someone coming from Sublime-Text but you can often find alternatives which are at least as efficient with Vim built-in features.

Of course, finding these alternative solutions isn't always easy and sometimes it takes time but it will get easier with your Vim experience and you'll see that with time multiple cursors will seem totally useless to you.

The strength of this command is that the last change can be an action working on a character, a line or a whole file. For example, a change can be delimited by the moment you enter insert mode and the moment you go back to normal mode.

All the challenge of the dot command is to learn how to make repeatable changes: it will come with grokking Vim but the basic is to understand how to make your change in a repeatable way.

Because A; creates an atomic action so when you'll use . on another line, no matter where you are in the line you'll insert the semi colon at the end. Whereas when using $a; you split your change in two parts $a and the insertion of ; so if you use . it will insert the semi colon on the current position of the cursor.

Macros are another extremely important tool in Vim since it allows you to record a sequence of keystrokes and repeat it as if you typed it again.

I'll use, as an example, your second use case:

  • Now you can use the macro to repeat your edit. As you are on the right line to edit you can simply execute the macro with @q. As we want to execute it twice you can use 2@q and you'll get the following result:

NOTE 1 As you may have noticed, using 0ea at the beginning of the macro was really important. Indeed, if you had put your cursor at the end of the first word before recording the macro and executing it again your result would have been:

The final @q would have called the macro by itself instead of using 2@q; you'd just have used @q and all the work would have been done.

Here comes another trick that doesn't directly apply to your use case but can be really useful to edit a large number of line at the same time. Let's get this extract of CSS code:

For more details on the [range] parameter please see :h :range. I won't detail it here, I'll simply remind that % represents the whole file, '<,'> represents the last selection, and 1,5 represents the lines 1 to 5 of the file.

This parameter defines the lines which will be treated by the global command. If no range is specified, then the global command will use % by default.

NOTE 1 An important point that took me some time to realize is that the normal command is an ex command which means that you can use it with the global command. That can be really powerful: let's say that I want to duplicate all the lines which contains echo, I don't need a macro or even the magic formula n.. I can simply use

global has an opposite command vglobal abbreviated v which works exactly like global except that the command will be applied on lines which don't match the [pattern] parameter. This way if we apply

As you can imagine these examples are pretty simple and are just made to demonstrate that when you follow the Vim way you really rarely need several cursors. My advice would be when you encounter a situation where you think it would be useful, write it down and take some time later to find a better solution. 99% of the time you'll eventually find a faster/more efficient way to do it.

Also I will repeat myself one more time but I really encourage you to read Practical Vim by Drew Neil because this book is not about "How to do that or this in Vim" it is about "How to learn to think in the Vim way" which will allow you to built your own solution to your future problems in a good way.

PS Special thanks to @Alex Stragies for his editing work and the corrections he made to this long post, you're da real MVP! ;-)

PPS Sorry for the long post!.

PS Special thanks to @Alex Stragies for his editing work and the corrections he made to this long post, you're da real MVP! ;-)

PPS Sorry for the long post!

PS Special thanks to @Alex Stragies for his editing work and the corrections he made to this long post.

Add paragraph on global command
Source Link
statox
  • 51.1k
  • 19
  • 158
  • 237
  • Put your cursor on the word variable1 and begin to record your macro with qq. This means "start recording all my future keystrokes in the register named q".

  • Start making your edit typing:

  • 0 to go at the beginning of the line

  • e to go at the end of the first word

  • a to append after your cursor

  • .someStuff to append the wanted text

  • <Esc> to stop the insertion

  • j to go on the next line

  • q to stop recording the macro

  • You'll get:

you'll get:

normal commandsThe global command

ItThe global command is late for me ata tool which allows to apply an ex mode command on lines matching a pattern, once again that's a good way to apply the momentsame change on different place without needing multiple cursors.

The syntax is the following:

:[range] g / pattern / command 

For more details on the [range] parameter please see :h :range. I wont detail it here, tomorrow I'll finish this answersimply remind that % represents the whole file, '<,'>' represents the last selection, and 1,5 represents the lines 1 to 5 of the file.

This parameter defines the lines which will be treated by the global command. If no range is precised, then the global command will use % by default.

The [pattern] argument is a search pattern as you are used to use with normalthe search commandsengine. As it integrates the search history you can leave this field blank and how they the global command will then use the last search pattern in the search history.

Finally the [command] parameter is an ex command as you are probably used to.

Now the behavior of the global command is pretty simple:

  • Iterate through all the lines defined in the [range] parameter
  • If the current line matches the defined pattern, apply the command

As the [command] parameter is an ex command, you can do a lot of things. Let's take the following pseudo code which isn't pretty interesting and have a lot of debugging messages:

var myList = null var i = 0 myList = new List() echo "List instantiated" for (i=0; i<10; i++) myList.Add(i) echo i . " added to the list" echo "end of for loop" 

Now let's say that you're sure this code works and you want to delete these useless echo statements:

  • You can apply your global command on the whole file so you'll have to prepend the command with % (or with nothing since % is the default range).

  • You know that the lines you want delete all matches the pattern echo

  • You want to delete these lines so you'll have to use the command :delete which can also be abbreviated as d

So you'll simply have to use the following function:

:%global/echo/delete 

Which can also be usedabbreviated as

:g/echo/d 

Note that % disappeared, global is abbreviated as g and delete as d. As you might imagine the result is:

var myList = null var i = 0 myList = new List() for (i=0; i<10; i++) myList.Add(i) 

NOTE 1 An important point that took me sometime to make changesrealized is that the normal command is an ex command which means that you can use it with the global command. That can be really powerful: let's say that I want to duplicate all the lines which contains echo, I don't need a macro or even the magic formula n.. I can simply use

:g/echo/normal YP 

And voila:

var myList = null var i = 0 myList = new List() echo "List instantiated" echo "List instantiated" for (i=0; i<10; i++) myList.Add(i) echo i . " added to the list" echo i . " added to the list" echo "end of for loop" echo "end of for loop" 

NOTE 2 "Hey what if I want to use my command on different placeslines which doesn't match a specific pattern?"

global has an opposite command vglobal abbreviated v which works exactly like global excepted that the command will be applied on lines which doesn't matches the [pattern] parameter. This way if we apply

:v/echo/d 

On our previous example we get:

echo "List instantiated" echo i . " added to the list" echo "end of for loop" 

The delete command has been applied on lines which didn't contained echo.

As you can imagine these examples are pretty simple and are just made to demonstrate that when you follow the Vim way you really rarely need several cursors. My advise would be when you encounter a situation where you think it would be useful, write it down and take some time later to find a better solution. 99% of the time you'll eventually find a faster/more efficient way to do it.

Also I will repeat myself one more time but I really encourage you to read Practical Vim of Drew Neil because this book is not about "How to do that or this in Vim" it is about "How to learn to think in the Vim way" which will allow you to built your own solution to your future problems in a good way.


PS Special thanks to @Alex Stragies for his editing work and the corrections he made to this long post, you're da real MVP! ;-)

PPS Sorry for the long post!

  • Put your cursor on the word variable1 and begin to record your macro with qq. This means "start recording all my future keystrokes in the register named q".

  • Start making your edit typing:

  • 0 to go at the beginning of the line

  • e to go at the end of the first word

  • a to append after your cursor

  • .someStuff to append the wanted text

  • <Esc> to stop the insertion

  • j to go on the next line

  • q to stop recording the macro

you'll get:

normal commands

It is late for me at the moment, tomorrow I'll finish this answer with normal commands and how they can be used to make changes on different places

  • Put your cursor on the word variable1 and begin to record your macro with qq. This means "start recording all my future keystrokes in the register named q".

  • Start making your edit typing:

  • 0 to go at the beginning of the line

  • e to go at the end of the first word

  • a to append after your cursor

  • .someStuff to append the wanted text

  • <Esc> to stop the insertion

  • j to go on the next line

  • q to stop recording the macro

  • You'll get:

The global command

The global command is a tool which allows to apply an ex mode command on lines matching a pattern, once again that's a good way to apply the same change on different place without needing multiple cursors.

The syntax is the following:

:[range] g / pattern / command 

For more details on the [range] parameter please see :h :range. I wont detail it here, I'll simply remind that % represents the whole file, '<,'>' represents the last selection, and 1,5 represents the lines 1 to 5 of the file.

This parameter defines the lines which will be treated by the global command. If no range is precised, then the global command will use % by default.

The [pattern] argument is a search pattern as you are used to use with the search engine. As it integrates the search history you can leave this field blank and the global command will then use the last search pattern in the search history.

Finally the [command] parameter is an ex command as you are probably used to.

Now the behavior of the global command is pretty simple:

  • Iterate through all the lines defined in the [range] parameter
  • If the current line matches the defined pattern, apply the command

As the [command] parameter is an ex command, you can do a lot of things. Let's take the following pseudo code which isn't pretty interesting and have a lot of debugging messages:

var myList = null var i = 0 myList = new List() echo "List instantiated" for (i=0; i<10; i++) myList.Add(i) echo i . " added to the list" echo "end of for loop" 

Now let's say that you're sure this code works and you want to delete these useless echo statements:

  • You can apply your global command on the whole file so you'll have to prepend the command with % (or with nothing since % is the default range).

  • You know that the lines you want delete all matches the pattern echo

  • You want to delete these lines so you'll have to use the command :delete which can also be abbreviated as d

So you'll simply have to use the following function:

:%global/echo/delete 

Which can also be abbreviated as

:g/echo/d 

Note that % disappeared, global is abbreviated as g and delete as d. As you might imagine the result is:

var myList = null var i = 0 myList = new List() for (i=0; i<10; i++) myList.Add(i) 

NOTE 1 An important point that took me sometime to realized is that the normal command is an ex command which means that you can use it with the global command. That can be really powerful: let's say that I want to duplicate all the lines which contains echo, I don't need a macro or even the magic formula n.. I can simply use

:g/echo/normal YP 

And voila:

var myList = null var i = 0 myList = new List() echo "List instantiated" echo "List instantiated" for (i=0; i<10; i++) myList.Add(i) echo i . " added to the list" echo i . " added to the list" echo "end of for loop" echo "end of for loop" 

NOTE 2 "Hey what if I want to use my command on lines which doesn't match a specific pattern?"

global has an opposite command vglobal abbreviated v which works exactly like global excepted that the command will be applied on lines which doesn't matches the [pattern] parameter. This way if we apply

:v/echo/d 

On our previous example we get:

echo "List instantiated" echo i . " added to the list" echo "end of for loop" 

The delete command has been applied on lines which didn't contained echo.

As you can imagine these examples are pretty simple and are just made to demonstrate that when you follow the Vim way you really rarely need several cursors. My advise would be when you encounter a situation where you think it would be useful, write it down and take some time later to find a better solution. 99% of the time you'll eventually find a faster/more efficient way to do it.

Also I will repeat myself one more time but I really encourage you to read Practical Vim of Drew Neil because this book is not about "How to do that or this in Vim" it is about "How to learn to think in the Vim way" which will allow you to built your own solution to your future problems in a good way.


PS Special thanks to @Alex Stragies for his editing work and the corrections he made to this long post, you're da real MVP! ;-)

PPS Sorry for the long post!

Made a great answer a tiny bit better by fixing some typos, and other small details
Source Link
Loading
Source Link
statox
  • 51.1k
  • 19
  • 158
  • 237
Loading