8/19/2021
81

The default editor that comes with the UNIX operating system is called vi (vi sual editor). Alternate editors for UNIX environments include pico and emacs, a product of GNU. The UNIX vi editor is a full screen editor and has two modes of operation: Command mode commands which cause action to be taken on the file,. You can read this article, but I’m pretty sure since the default terminal in Mac OS X has a built-in scrollbar, the mousewheel commands automatically go to it. You could definitely use gVim as suggested in the previous answer. I find that I don’t generally want to use the mouse in Vim though as it takes my hands off the keyboard.


Using Vim Macro feature you can record and play a sequence of actions inside the editor.

This article explains how to perform record and play inside Vi and Vim editor using two detailed examples.

High Level Steps to Record and Play inside Vim

  1. Start recording by pressing q, followed by a lower case character to name the macro
  2. Perform any typical editing, actions inside Vim editor, which will be recorded
  3. Stop recording by pressing q
  4. Play the recorded macro by pressing @ followed by the macro name
  5. To repeat macros multiple times, press : NN @ macro name. NN is a number

Example 1: Sequence number generation inside a file using Vim Macro

1. Start a sequence-test.txt file to generate the sequence.

2. Go to insert mode and type “1 .” as shown below

Type: Esc i followed by 1.

3. Start the Recording and store it in register a.

Type: Esc q followed by a

  • q indicates to start the recording
  • a indicates to store the recordings in register a
  • When you do q a, it will display “recording” at the bottom of the vi.

4. Copy the 1st line to 2nd line to have two lines with 1 . as shown below

Type: Esc yy followed by p

  • yy will copy the current line
  • p will paste the line that was just copied

Note: Vim will still show recording at the bottom as shown below.

5. Increment the number.

Vim Command Macro

Type: Control a

By placing the cursor at the 2nd line, press Ctrl+a which increment the number to 2. as shown below.

Note: vim will still show recording at the bottom.

6. Stop the recording

Type: q

Press q to stop the recording. You’ll notice that recording message at the bottom of the vim is now gone.

7. Repeat the recording 98 times.

Type: [email protected]

  • Now repeat this job, by typing 98 @ a
  • @a repeats the macro “a” one time.
  • [email protected] repeats the macros “a” 98 times generating the sequence number 1 – 100 as shown below using macros.

Example 2: Repeat Vim Macro with different arguments

This example explains how you can executing the same command, with different input for it. i.e Framing the same command, with different arguments.

Before Executing the Macro: change-password.sql

After Recording and executing the Macro: change-password.sql

1. Open the change-password.sql that has only the names.

2. Start the Recording and store it in register a

Type: q a

  • q indicates to start the recording
  • a indicates to store the recordings in register a
  • When you do q a, it will display the message recording at the bottom of the vi.

3. Go to Insert Mode and Type ALTER USER

Type: I (Upper case i) followed by “ALTER USER ”

Place the cursor anywhere in the first line, and then press I. Which will take you to the first character of the line. Type ALTER USER

4. Copy the Next Word (i.e the name)

Vim

Type: Esc w yw

How to use vim
  • Press Esc, and then press w to go to the next word ( name ).
  • yw, copies the current word ( name ).

5.Go to the end and type IDENTIFIED BY ‘

Type: Esc A followed by ” IDENTIFIED BY ‘”

  • Press Esc, and A to move the cursor to the end of the line, and then type space.
  • Type IDENTIFIED BY ‘

6. Paste the copied Name

Type: Esc p

Press Esc, and then type p to paste the name that was copied in the step #4.

7. Complete the quote at the end.

Type: Esc A followed by ‘;

Press Esc, and A to go to the end of the line, and ‘;

8. Jump to the next line and stop the record.

Cheat

Type: Esc j followed by q

  • j to move to the next line.
  • q to stop the recording

Note: The recording message shown in the bottom of the vi will now disappear. At this stage, the change-password.sql will look like the following.


Fig: Vim Macro completed the recording

9. Repete the Macro with the arguments in the corresponding line

Type: 8 @ a

  • Now repeat this job 8 times by typing [email protected]
  • @a repeats the macro “a” one time.
  • [email protected] repeats the macros “a” 8 times completing the rest of the line automatically as shown below

Recommended Reading

Vim 101 Hacks, by Ramesh Natarajan. I’m a command-line junkie. So, naturally I’m a huge fan of Vi and Vim editors. Several years back, when I wrote lot of C code on Linux, I used to read all available Vim editor tips and tricks. Based on my Vim editor experience, I’ve written Vim 101 Hacks eBook that contains 101 practical examples on various advanced Vim features that will make you fast and productive in the Vim editor. Even if you’ve been using Vi and Vim Editors for several years and have not read this book, please do yourself a favor and read this book. You’ll be amazed with the capabilities of Vim editor.

Awesome Vim Editor Articles

Following are few awesome Vi / Vim editor tutorials that you might find helpful.

Note: Please subscribe to The Geek Stuff and don’t miss any future Vi and Vim editor tips and tricks.

Question or issue on macOS:

I’ve been googling around trying to figure out if it’s possible to use my mouse wheel to scroll while inside Vim in Mac’s Terminal, with no luck. It seems as if only X11 or iTerm support this.

Before I give up, I thought I’d try the geniuses here to see if anyone knows a way to do this. So, does anyone know if I can set that up?

Or should I seriously consider using a different terminal application?

How to solve this problem?

Solution no. 1:

Use MouseTerm (and do make sure to install SIMBL first!) and scrolling will work like a charm, even remote, using Mac Terminal.

You need to fully quit the Terminal application (Command+Q) and then launch it again after installing MouseTerm.

Solution no. 2:

And if you’re using iTerm, add this to your vimrc

Solution no. 3:

This is an old question, but a top hit on google, so I feel compelled to provide an updated answer. Powersaves mac download online.

Running OSX El Capitan 10.11, vim mouse and trackpad scrolling just worked(TM) for me in Terminal.app by default. However occasionally the mouse/trackpad input stopped manipulating the vim buffer, and started scrolling the terminal buffer. The answer was Command+R or Menu View –> Allow Mouse Reporting. Turning that on allowed the mouse/trackpad scroll operations to move the cursor in vim.

Solution no. 4:

  • Termanal Menu > View > Allow Mouse Reporting
  • Terminal Menu > Preferences >
    Keyboard > Scroll alternate screen

Vim Mode Mac

Solution no. 5:

You can read this article, but I’m pretty sure since the default terminal in Mac OS X has a built-in scrollbar, the mousewheel commands automatically go to it. You could definitely use gVim as suggested in the previous answer. I find that I don’t generally want to use the mouse in Vim though as it takes my hands off the keyboard.

I just use 50j to go down and 50k to go up. Not exactly scrolling, but it works pretty well.

Solution no. 6:

If the mouse functionalities still do not work properly take a look at my answer in this post How to let vim behave on Mac OS X as on Ubuntu?, just add to your .vimrc

Solution no. 7:

Make sure the terminal is xterm & not ansi in Terminal Menu > Preferences > Profiles > Advanced. I accidentally broke scrolling by changing the term type in a naive effort to get coloring to work over ssh.

Solution no. 8:

Use gVim, which gives you a text editing environment in a window you can scroll. Terminal is not involved when using gVim.

Vim Commands Mac Cheat Sheet

Solution no. 9:

I’m using xterm in X11 (XQuartz 2.3.4) and vim works very fine with mouse and also suport 256 colors.

Here is the ~/.Xresources I use to make my xterm nicer in X11:

Little tips, to remove the bell sound in X11’s xterm type this command:

How To Use Vim

Solution no. 10:

I would recommend using iTerm – it has so many advantages over Terminal eg Mouse support, 256 colors, sensible copy and paste (auto-copy, word/url selection with double click, middle click paste)…

Vim cheat sheet

Hope this helps!

6817.info – 2018