Lecture-3

Vim

Video: Vim usage on Ubuntu terminal

Vim is a text editor can be used on the terminal window.

It is usually used to make coding process faster.

Let's make Lecture3 directory on your home directory and create three empty files in it.

You can open file1.txt via vi file1.txt or vim file1.txt command.

Vim is an improved version of Vi. The differences between those are not important for this course.

In Vim, there are three modes,

insert mode

Press i for the insert mode to start writing on the document. When you press i you will see on the bottom-left corner that -- INSERT -- will be printed out.

Type something and press ESC to open the command mode.

command mode

After pressing ESC you can type

Try these options by yourself.

Later open the saved document again using Vim.

insert mode again

It will open in command mode. You can press

Try these options by yourself.

Press o and type something.

You can press Enter for the next line in the insert mode.

Undo and Redo

In the command mode, you can undo your changes or redo by pressing u and ctrl+r respectively.

Move the cursor on the text

In the command mode, you can move on the text using,

for one character (or line).

To move word-by-word, you can use

These commands can also be used with numbers.

Moreover, you can move by pressing,

Specially, 0 (zero) moves to the first character of the line.

Select and copy or cut

You can select, in Vim command mode

After selecting whatever you want to copy, press y to copy or press x to cut. Then move whereever you want to paste and press p.

Working on multiple files

In the command mode, you can open multiple files

In the command mode, it is possible to move between files by pressing ctrl+w+w

You can save files and exit with :wq in the command mode or you can use :wqall to save all and exit from all files.

Other way to open files in split mode is opening them together from terminal commandline.

Here -o option splits view horizontally and (capital) -O splits it vertically.

You can split the views more typing more file names in the commandline or using :split file3.txt for example in the command mode.

Hidden recovery files

If you press ctrl+z when you are in command mode, Vim will immediately close and recovery files will be written.

If you want to open those files back you will see a window as follows.

Here you can choose what to do,

I do not use ctrl+z when I use Vim. If I do it accidentally, I just remove the hidden files to get rid of this warning.

Settings

You can personalize Vim according to your needs.

Some of the settings are listed below.

You can set these in the command mode but the changes will not be applied in the later sessions. To apply them for later add your favourite settings into ~\.vimrc file.

Running terminal commands in Vim

:! command allows you to run terminal commands without leaving Vim. Try to do following simple Python example.

Open firstPythonCode.py file using Vim.

Write the simple code print("Hellow World!") and save with :w.

Use :! python3 firstPythonCode.py to run the code in the command mode.

Check the output.

Screen

Screen is used to run terminal commands in the background, divide the terminal window and use multiple terminals on one window.

You can start screen by typing screen on the commandline.

Split

Then divide your window vertically by ctrl+z and |.

And/or divide your window horizontally by ctrl+z and shift+s.

Move

Press ctrl+z and tab for jumping to the next half, or ctrl+z and shift+tab for the previous half.

ctrl+z and c opens new terminal on the current half. You can see the numbres at the bottom-left corner of each space.

Or you can print all the terminal numbers by pressing ctrl+z and w.

You can change the active terminal by ctrl+z and space.

Resize

You can change the size of any split using ctrl+z and :resize and the number of characters (horizontally) or lines (vertically).

In the below example ctrl+z and :resize 7 is used for the terminal number 2 and ctrl+z and :rezie 25 is used for the terminal number 1.

To cancel all the splits and focus on only one screen you can press ctrl+z and shift+q.

The other screens are not killed with this. You can see it with ctrl+z and w.

You can change see the active terminal with ctrl+z and w. There is * near to the terminal number.

To change the active terminal here, you can again use ctrl+z and space. Other ways to change the active terminal are listed below.

Name the Screen windows

When you press ctrl+z and ", you will see that all the screens are named as zsh as default.

You can change it by pressing ctrl+z and shift+a, then deleting zsh and writing the new name.

In the below picture, the names are changed and shown by pressing ctrl+z and ".

You can also see the changes when you split your terminal window vertically into 3 for example by pressing ctrl+z and | two times.

Kill a Screen window

Here you can kill a window by pressing ctrl+z and k. It asks you to press y if you are sure to kill that window.

You can kill all the windows in this Screen session by ctrl+z and \.

Name, detach and attach

screen -S name is the command to give a name to a Screen session.

Use ctrl+z and d to detach from a Screen session.

Use screen -r name to reattach to the Screen session.

You will notice that working jobs continue working even we detach from a screen.