01-05-2021



You can open up Terminal on your Mac (perhaps by opening Spotlight on your Mac with Command⌘-Space key combination and type “Terminal”. Traverse to a folder you want to work in (documents will do. Find out your internal IP address by using Terminal. This method is easier and faster for Mac users who are familiar with a command line program called Terminal. Even if you have not used Terminal before, just follow the instructions and you will find the internal IP address. First, use spotlight by pressing Command and Spacebar and type Terminal.

  1. Mac Terminal Vi Commands Cheat
  2. How To Exit Vi In Mac Terminal

The new Dark Mode in macOS Mojave is a nice addition and is – especially inthe night hours — more pleasing to your eyes than the light mode.

Terminal

However, enabling Dark Mode will not change the Terminal profile, which isa little bit annoying – especially if your color theme has a light and a darkvariant (like the infamous Solarized, Snow, One, or my own Rasta theme).

If you change your Terminal profile to something dark, Vim still doesn’t lookright because it uses its own mechanism for light/dark backgrounds (see :help'background' for details) and doesn’t know about the changes you made to theTerminal profile.

If you execute :set background=dark in Vim (and if you color schemesupports it), Vim looks nice and dark now, too.

However, on the next day, the fun begins again when you want to switcheverything back to light mode …

Wouldn’t it be nice if this could all be accomplished with a single command?

There are tools, that help you with switching to/from macOS Dark Mode (e.g.,NightOwl or Shifty), but they can’t change your Terminal profile or notify Vim.

Commands

As it turns out, it’s not too hard to implement a little program that doesexactly this:

  • You can uses the defaults command to get the current macOS Dark Theme mode:

  • You can use AppleScript (oh, how I love this language …) to set Dark Mode andupdate the Terminal profile:

  • You can wrap both things with a Python script:

  • You can use the timer_start() function introduced in Vim 8 and neovim toregularly check for the current Dark Mode settings. Put this into your Vim config:

  • You can create an Automator action that runs the Python script and thatcan be activated with a global shortcut. I use ⌥⌘D (you need todeactivate this shortcut for showing/hiding the Dock first). This is theAppleScript I used:

The drawback of this method is that the current application (at the time youpress ⌥⌘D) is used as “source” of the action you get two dialogs asking youto give that app permissions to remote control the System Settings and Terminal.

A better solution would be if the authors of NightOwl and Shifty wouldintegrated this into their tools. I’m gonna contact them and see what happens. :-)

Update:

MacVimgot anOSAppearanceChanged event that is emitted every time MacVim changes its appearance.

Thanks to Frank for the heads up!

What is the VI editor?

Mac Terminal Vi Commands Cheat

The VI editor is the most popular and classic text editor in the Linux family. Below, are some reasons which make it a widely used editor –

1) It is available in almost all Linux Distributions

2) It works the same across different platforms and Distributions

3) It is user-friendly. Hence, millions of Linux users love it and use it for their editing needs

Nowadays, there are advanced versions of the vi editor available, and the most popular one is VIM which is Vi Improved. Some of the other ones are Elvis, Nvi, Nano, and Vile. It is wise to learn vi because it is feature-rich and offers endless possibilities to edit a file.

To work on VI editor, you need to understand its operation modes. They can be divided into two main parts.

In this tutorial, you will learn more about-

Click here if the video is not accessible

Terminal

vi Command mode:

  • The vi editor opens in this mode, and it only understands commands
  • In this mode, you can, move the cursor and cut, copy, paste the text
  • This mode also saves the changes you have made to the file
  • Commands are case sensitive. You should use the right letter case.

vi Editor Insert mode:

  • This mode is for inserting text in the file.

  • You can switch to the Insert mode from the command mode by pressing 'i' on the keyboard

  • Once you are in Insert mode, any key would be taken as an input for the file on which you are currently working.

  • To return to the command mode and save the changes you have made you need to press the Esc key

How to use vi editor

To launch the VI Editor -Open the Terminal (CLI) and type

And if you specify an existing file, then the editor would open it for you to edit. Else, you can create a new file.

VI Editing commands

  • i - Insert at cursor (goes into insert mode)
  • a - Write after cursor (goes into insert mode)
  • A - Write at the end of line (goes into insert mode)
  • ESC - Terminate insert mode
  • u - Undo last change
  • U - Undo all changes to the entire line
  • o - Open a new line (goes into insert mode)
  • dd - Delete line
  • 3dd - Delete 3 lines.
  • D - Delete contents of line after the cursor
  • C - Delete contents of a line after the cursor and insert new text. Press ESC key to end insertion.
  • dw - Delete word
  • 4dw - Delete 4 words
  • cw - Change word
  • x - Delete character at the cursor
  • r - Replace character
  • R - Overwrite characters from cursor onward
  • s - Substitute one character under cursor continue to insert
  • S - Substitute entire line and begin to insert at the beginning of the line
  • ~ - Change case of individual character

Note: You should be in the 'command mode' to execute these commands. VI editor is case-sensitive so make sure you type the commands in the right letter-case.

Make sure you press the right command otherwise you will end up making undesirable changes to the file. You can also enter the insert mode by pressing a, A, o, as required.

Moving within a file

Mac Terminal Vi Commands
  • k - Move cursor up
  • j - Move cursor down
  • h - Move cursor left
  • l - Move cursor right

You need to be in the command mode to move within a file. The default keys for navigation are mentioned below else; You can also use the arrow keys on the keyboard.

Saving and Closing the file

  • Shift+zz - Save the file and quit
  • :w - Save the file but keep it open
  • :q - Quit without saving
  • :wq - Save the file and quit

You should be in the command mode to exit the editor and save changes to the file.

Summary:

How To Exit Vi In Mac Terminal

  • The vi editor is the most popular and commonly used Unix text editor
  • It is usually available in all Linux Distributions.
  • It works in two modes, Command and Insert
  • Command mode takes the user commands, and the Insert mode is for editing text
  • You should know the commands to work on your file easily
  • Learning to use this editor can benefit you in creating scripts and editing files.