Mac Terminal Vi Commands

Posted on  by admin

Commands marked. are bash built-in commands. Bash is the default shell, it runs under Darwin the open source core of macOS. In macOS Catalina the default shell will change to zsh and in time this page will be updated to include that. Depending on the command (s) you run to get your console in the desired mode, just add that line in /.inputrc. This should be sufficient. If /.inputrc does not exist, you can create it without worries. A line with set keymap vi-command sets your Terminal to' command mode'; a line with set keymap vi-insert your Terminal to 'insert mode'. 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.

What is vi

The vi editor is elaborated as visual editor. It is installed in every Unix system. In other words, it is available in all Linux distros. It is user-friendly and works same on different distros and platforms. It is a very powerful application. An improved version of vi editor is vim.

The vi editor has two modes:

  • Command Mode: In command mode, actions are taken on the file. The vi editor starts in command mode. Here, the typed words will act as commands in vi editor. To pass a command, you need to be in command mode.
  • Insert Mode: In insert mode, entered text will be inserted into the file. The Esc key will take you to the command mode from insert mode.

By default, the vi editor starts in command mode. To enter text, you have to be in insert mode, just type 'i' and you'll be in insert mode. Although, after typing i nothing will appear on the screen but you'll be in insert mode. Now you can type anything.

To exit from insert mode press Esc key, you'll be directed to command mode.

If you are not sure which mode you are in, press Esc key twice and you'll be in command mode.

Using vi

The vi editor tool is an interactive tool as it displays changes made in the file on the screen while you edit the file.

In vi editor you can insert, edit or remove a word as cursor moves throughout the file.

Commands are specified for each function like to delete it's x or dd.

The vi editor is case-sensitive. For example, p allows you to paste after the current line while P allows you to paste before the current line.

vi syntax:

In the terminal when you'll type vi command with a file name, the terminal will get clear and content of the file will be displayed. If there is no such file, then a new file will be created and once completed file will be saved with the mentioned file name.

Linux vi example

Let's understand vi through an example:

To start vi open your terminal and type vi command followed by file name. If your file is in some other directory, you can specify the file path. And if in case, your file doesn't exist, it will create a new file with the specified name at the given location.

Example:

Look at the above snapshot, we are creating a new file file.txt (as this file doesn't exist) and have entered the full path for the directory Downloads.

Command mode

This is what you'll see when you'll press enter after the above command. If you'll start typing, nothing will appear as you are in command mode. By default vi opens in command mode.

Look at the above snapshot, it is blank as it is a new file. To start typing, you have to move to the insert mode. At the end of the terminal window, directory name and file name are displayed.

Insert mode

To move to the insert mode press i. Although, there are other commands also to move to insert mode which we'll study in next page.

Look at the above snapshot, after pressing i we have entered into insert mode. Now we can write anything. To move to the next line press enter.

Once you have done with your typing, press esc key to return to the command mode.

To save and quit

You can save and quit vi editor from command mode. Before writing save or quit command you have to press colon (:). Colon allows you to give instructions to vi.

exit vi table:

CommandsAction
:wqSave and quit
:wSave
:qQuit
:w fnameSave as fname
ZZ Save and quit
:q! Quit discarding changes made
:w! Save (and write to non-writable file)

To exit from vi, first ensure that you are in command mode. Now, type :wq and press enter. It will save and quit vi.

Type :wq to save and exit the file.

Look at the above snapshot, command :wq will save and quit the vi editor. When you'll type it in command mode, it will automatically come at bottom left corner.

If you want to quit without saving the file, use :q. This command will only work when you have not made any changes in the file.

Look at the above snapshot, this file is modified and hence on typing :q it displays this message at bottom left corner.

The above file can be saved with the command :!q. It discards the changes made in the file and save it.

Look at the above snapshot, we have typed :!q, it will save our file by discarding the changes made.

Vi Commands

Linux vi editor is different from other editors. You have to use different keys to use different functions. Although, it's quite easy and interesting to use vi editor.

The vi editor commands are case sensitive.

Have a look at the vi commands in the following table.

Mac

To switch from command to insert mode:

Mac Terminal Vi Commands List

Command Action
iStart typing before the current character
IStart typing at the start of current line
aStart typing after the current character
AStart typing at the end of current line
oStart typing on a new line after the current line
OStart typing on a new line before the current line

To move around a file:

Commands Action
jTo move down
kTo move up
hTo move left
lTo move right

To jump lines:

Commands Action
GWill direct you at the last line of the file
``Will direct you to your last position in the file

To delete:

CommandsAction
xDelete the current character
XDelete the character before the cursor
rReplace the current character
xpSwitch two characters
ddDelete the current line
DDelete the current line from current character to the end of the line
dGdelete from the current line to the end of the file

To repeat and undo:

Commands Action
u Undo the last command
.Repeat the last command

Command to cut, copy and paste:

Mac Terminal Vi Commands Examples

Commands Action
ddDelete a line
yy(yank yank) copy a line
pPaste after the current line
P Paste before the current line

Command to cut, copy and paste in blocks:

CommandsAction
<n>ddDelete the specified n number of lines
<n>yyCopy the specified n number of lines

Start and end of line:

CommandsAction
θBring at the start of the current line
^Bring at the start of the current line
$Bring at the end of the current line
Delete till start of a line
d$ Delete till end of a line
Mac terminal commands

Joining lines:

CommandsAction
JJoin two lines
yypRepeat the current line
ddpSwap two lines

Move forward or backward:

CommandsAction
wMove one word forward
bMove one word backward
<n>wMove specified number of words forward
dwDelete one word
ywCopy one word
<n>dwDelete specified number of words

How To Exit Vi In Mac Terminal

Mac Terminal Vi Commands

Search a string:

CommandsAction
/stringForward search for given string
?stringBackward search for given string
/^string Forward search string at beginning of a line
/string$Forward search string at end of a line
nGo to next occurrence of searched string
/<he>Search for the word he (and not for there, here, etc.)
/pl[abc]ceSearch for place, plbce, and plcce

Replace all

Syntax:

Example:

CommandsAction
:1,$ s/readable/changed/Replace forward with backward from first line to the last line
:3,6 s/letters/neww/g Replace forward with backward from third line to the ninth line

Mac Terminal Vi Command

Text buffers:

CommandsAction
'addDelete current line and put text in buffer a
'ap Paste the line from buffer a

Abbreviation

Best Terminal Commands Mac

Syntax:

Example:

Mac Terminal Vi Commands Pdf

CommandsAction
:ab au abbrevition and unabbreviationAbbreviate au to be 'abbrevition and unabbreviation'
:una au
Un - abbreviate au

Vi Editor Index

Vi Editor


When it comes to quickly taking care of daily tasks, the command line can be both powerful and dangerous. Take today’s commands as an example: the rm command allows you to remove (or delete) files. The rmdir command does the same to directories (also know as folders). But be careful: Unlike when you move files to the Trash from the Finder, there’s no way to get them back if you use these commands. Still, if you want to tap into Terminal’s powers, this is a command you can’t overlook. I’ll show you how to add a safeguard to ensure that you only delete files you really want to delete.

Why bother deleting files with the command line?

Deleting files with the Finder isn’t too difficult, plus you can always fish files out of the Trash if you change your mind. So why bother using the command line? Here are some reasons:

  • You can delete multiple files quickly and efficiently using wildcards.
  • You can remove files from the Trash when you encounter stubborn errors.
  • You can delete files that are hidden in the Finder; these files, which can contain settings for certain apps or parts of MacOS, contain a dot (.) before their names and the Finder doesn’t show them.
  • If you’ve lost access to the Finder because your Mac is on the blink, you might be able to use the command line to troubleshoot the problem.

Delete files

It’s dangerously easy to delete files with the rm command. Here’s an example. After you launch Terminal (in your /Applications/Utilities folder) type cd ~/Desktop to navigate to the Desktop directory. If you had a file here named MyFile.rtf that you never, ever wanted to see again, you could run this command:

rm MyFile.rtf

When you press Return, the file will go poof! It will be gone, toast, history. You can’t get it back.

You can even delete multiple files in a single command. So, if you have three files on your Desktop that you want to delete, and you want to delete them all at once, you can do so like this:

rm MyFile.rtf MyCV.rtf MyGreatAmericanNovel.rtf

Again, pressing the Return key does the dirty work.

If I sound ominous when discussing the powers of the rm command, there’s a good reason. As I said before, this command deletes files; it nukes them. You can’t get them back. You can’t click on the Trash icon and retrieve files you’ve accidentally deleted.

If I sound ominous when discussing the powers of the rm command, there’s a good reason. This command deletes files; it nukes them. You can’t get them back.

But there’s a safety net: it’s the -i, or interactive, flag. So if you’re feeling cautious, you could run the above commands with this flag as follows:

In each case, pressing Return won’t actually activate the rm command, because the -i flag acts as a pause button. You’ll see the following in Terminal when running these commands:

In order to proceed, you need to type yes, or simply y. In the case of multiple files, you’ll see one query for each file. Granted, it’s easy to get into the habit of quickly typing y, but the question is intended to make you stop and think very carefully about whether you really want to delete that file.

Delete directories (a.k.a folders)

Deleting directories, or folders, is a bit different. If you try to run the rm command on a directory, you’ll see the following message:

There’s a special command for deleting directories: rmdir. So to delete a directory named Archives, run this command:

rmdir Archives

You can’t use the -i flag with the rmdir command, so the command is a bit riskier.

Note that this command only deletes empty directories. If you want to delete a directory and the files it contains, read on.

Delete everything

The rm command has a powerful option, -R (or -r), otherwise known as the recursive option. When you run the rm -R command on a folder, you’re telling Terminal to delete that folder, any files it contains, any sub-folders it contains, and any files or folders in those sub-folders, all the way down.

When you run the rm -R command on a folder, you’re telling Terminal to delete that folder, any files it contains, any sub-folders it contains, and any files or folders in those sub-folders, all the way down.

For example, let’s say you have a directory full of archives, containing sub-directories and files. Deleting each item individually from the Finder or the command line can take a long time. So just run the command like this:

rm -R Archives

Remember, this deletion is final. But, as you probably suspect, you can use the -i flag for protection:

rm -iR Archives

This will ask you to confirm the deletion of each item. This can be annoying, but unless you’re really sure you want to delete all those files, it’s probably best to be safe.

A practical application

When can the rm -R command come in handy? Say you can’t empty the Trash on your Mac. A file might be locked or you may not have permission to delete one or more files. This sort of glitch is annoying, but you can use the command line to provide an easy solution.

In Terminal, type the following:

rm -R

Then type a space.

In the Finder, open the Trash, and then drag the items it contains to the Terminal window. You’ll see one or more files with paths such as /Users/.Trash/file.txt.

If there are lots of files, you may find that the resulting list—all on one long line, wrapping in the Terminal window—may be very long. If you’re absolutely sure that you want to delete all these items, press Return. Terminal will empty the Trash. Command line win!

Want to learn more? See our articles about navigating the file system with the command line, learning from man pages, and copying and moving files.