edgecase
The spice must flow.
Author: StJohn Piano
Published: 2019-01-26
Datafeed Article 82
This article has been digitally signed by Edgecase Datafeed.
3634 words - 1261 lines - 32 pages





GOAL



A remote VPS has an instance of the Vim text editor installed. Test a list of commands for this editor.




CONTENTS



- Goal
- Contents
- Brief Summary
- Summary
- Basic Vim Commands
- Project Log




BRIEF SUMMARY



In this project, I tested a list of basic commands for the Vim editor. The tested commands are listed (with descriptions) in the Basic Vim Commands section.




SUMMARY



In a previous project, I found a list of basic
vi
commands at:
www.cs.colostate.edu/helpdocs/vi.html

Some reading indicated that:
- Vim is an expansion of the original vi editor.
- Vim is backwards compatible with vi (the vi commands should all work the same way in Vim).

In this project, I tested each command in a Vim instance, running on a CentOS 6.9 x64 virtual machine on the DigitalOcean service.

The version of Vim was 7.4. I tested the commands in both the small version (invoked with the
vi
command) and the huge version (invoked with the
vim
command).

The tested commands are listed (with descriptions) in the Basic Vim Commands section.


Acryonyms and terms

- VPS = Virtual Private Server
- DigitalOcean = A cloud infrastructure provider
- Droplet = DigitalOcean's term for a VPS
- VIM = Vi IMproved










BASIC VIM COMMANDS





Working definition of a "word" in Vim: Sequence of non-whitespace characters. Words are separated by a sequence of whitespace characters, where whitespace characters are newline, tab, and space.

The Vim editor is included as a default text editor in many Linux systems. "Vim" stands for "Vi IMproved".

Vim is a full screen editor and has two modes of operation:
- Command mode: Keystrokes are interpreted as commands that affect the existing data.
- Insert mode: Keystrokes are interpreted as new data, which is inserted into the existing data.

In command mode, some keystrokes cause the Vim editor to enter insert mode. In insert mode, pressing the Escape key causes the editor to exit insert mode and re-enter command mode.

In command mode, some commands begin with a colon (:). If a colon is typed, the cursor moves to the bottom of the screen. This type of command is completed by pressing the Return / Enter key.

[Untested] On some UNIX platforms, the arrow keys may be used; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi (and Vim?) and should be avoided.

Future: Find out how the history feature for the undo and redo commands works (how far back it goes, when it gets erased, etc). Does it work differently in the huge and small versions?

Notes:
- Unless otherwise specified, the commands in this list work in command mode.
- Named keys are enclosed in square brackets and their names may be abbreviated. Examples: [Return] = Return key, [Esc] = Escape key, [Ctrl] = Control key.
- The symbol ^ indicates that the [Ctrl] key should be held down while the letter key is pressed.
- "Current line" = the line where the cursor is currently present.
- In a tested instance of Vim 7.4, I found that there was a small version (invoked with the
vi
command) and a huge version (invoked with the
vim
command).
- When searching, huge-version Vim highlights all results. Small-version Vim does not. Some reading indicates that there are various approaches for configuring the highlighting in huge-version Vim.




# To enter and exit the Vim editor

[(huge version of Vim) In the command line, open a file for editing]
$ vim [filepath]

Example:
$ vim test.txt

[(small version of Vim) In the command line, open a file for editing]
$ vi [filepath]

Example:
$ vi test.txt

[Quit Vim, writing out the modified file to the file named in original invocation. This is a shortcut for :wq]
:x[Return]

[Quit / exit Vim]
:q[Return]

[Quit Vim even though the latest changes have not been saved]
:q![Return]





# Moving the cursor

[Move cursor down one line]
j or down-arrow

[Move cursor down to the beginning of next line]
[Return]

[Move cursor up one line]
k or up-arrow

[Move cursor left one character]
h or left-arrow

[Move cursor left one character. If cursor is at beginning of line, move up to end of previous line.]
[Backspace]

[Move cursor right one character]
l or right-arrow

[Move cursor right one character. If cursor is at end of line, move down to beginning of next line.]
[Space]

[Move cursor to start of current line]
0 [zero]

[Move cursor to end of current line]
$

[Move cursor to beginning of next word]
w

[Move cursor back to beginning of preceding word]
b

[Move cursor to first line in file]
:0[Return] or 1G

[Move cursor to line N]
:N[Return] or NG

[Move cursor to last line in file]
:$[Return] or G





# Screen manipulation

[Move forward one screen]
^f

[Move backward one screen]
^b

[Move down (forward) one half screen]
^d

[Move up (back) one half screen]
^u

[Redraws the screen]
^l

[Some commands (e.g. ^g) leave a message displayed at the bottom of the screen. The command ^l will redraw the screen using the actual data, effectively removing the message.]





# Undo and redo

[Undo the previous action]
u

[Redo the previous action]
^r





# Inserting or adding text

Each of these commands puts the Vim editor into insert mode - the [Esc] key must be pressed to terminate insert mode and to put the Vim editor back into command mode.

[Insert text before cursor]
i

[Insert text at beginning of current line]
I

[Append text after cursor]
a

[Append text to end of current line]
A

[Open and put text in a new line below current line]
o

[Open and put text in a new line above current line]
O

[Read file named "filename" and insert its contents after current line]
:r filename[Return]





# Changing text

I think that in this group of commands:
- "r" stands for "replace".
- "c" stands for "change".
- "w" stands for "word".

[Replace single character under cursor (no [Esc] needed)]
r

[Replace characters, starting with current cursor position, until [Esc] hit]
R

[Delete all subsequent characters until end of word and move into insert mode.]
cw

[Delete all subsequent characters until end of N words and move into insert mode. Example: c5w]
cNw

[Delete all subsequent characters until end of line and move into insert mode.]
C

[Delete entire line and move into insert mode.]
cc

[Delete next N lines, starting with the current line, and move into insert mode.]
Ncc or cNc





# Deleting text

These commands do not enter insert mode.

[Delete single character under cursor]
x

[Delete N characters, starting with character under cursor, up to end of line]
Nx

[Delete the single word beginning with character under cursor]
dw

[Delete N words beginning with character under cursor. Example: d5w deletes 5 words]
dNw

[Delete the remainder of the line, starting with current cursor position]
D

[Delete entire current line]
dd

[Delete N lines, beginning with the current line. Example: 5dd deletes 5 lines.]
Ndd or dNd





# Copying and pasting text

[Copy / yank the current line into the buffer]
yy

[Copy / yank the next N lines, including the current line, into the buffer]
Nyy or yNy

[Put / paste the line(s) in the buffer into the text after the current line]
p





# Searching text

[Search forward for occurrence of string "foo" in text]
/foo

[Search backward for occurrence of string "foo" in text]
?foo

[Move to next occurrence of current search string]
n

[Move to next occurrence of current search string in opposite direction]
N





# Get line number information

[Displays line number of current line (at bottom of screen)]
:.=

[Displays the total number of lines (at bottom of screen)]
:=

[Displays the current line number, along with the total number of lines in the file, at the bottom of the screen]
^g





# Writing files

[Write current contents to file named in original Vim call]
:w[Return]

[Write current contents to a new file named "newfile"]
:w newfile[Return]

[Write the contents of the lines numbered 12 through 35 to a new file named "smallfile"]
:12,35w smallfile[Return]

[Write current contents over a file named "filename", even if it already exists]
:w! filename[Return]
















PROJECT LOG





VPS = Virtual Private Server

From previous projects, I have a VPS (a DigitalOcean droplet) that I can access via openSSH from a Windows laptop.


Local system:
- Name = SP_THINKPAD_1. SP stands for StJohn Piano.
- Hardware = Lenovo Thinkpad X200 (Laptop). Two internal hard drives, each of 75 GB capacity.
- Processor = Intel(R) Core(TM)2 Duo CPU, P8600 @ 2.40GHz
- Memory (RAM) = 4.00 GB
- Operating System = Windows 7 Professional (Service Pack 1), 64-bit

I installed Cygwin on SP_THINKPAD_1 during a previous project:
Installing Cygwin on Windows 7

I installed OpenSSH on Cygwin on SP_THINKPAD_1 during another previous project:
Installing OpenSSH on Cygwin and using it to access a VPS

Remote system (a VPS):
- Provider: DigitalOcean
- Distribution = CentOS 6.9 x64
- Memory = 1 GB
- vCPUS = 1 vCPU
- SSD Disk = 25 GB
- Transfer = 1 TB
- Price = $5/mo, $0.007/hr
- Datacentre region = London
- hostname = spt1
- SSH Key = SP_THINKPAD_1
- IP address = 178.62.36.113
- domain = edgecase-test.net



In a previous project, Verifying a signed deed of the GPG 1.4.10 source code, I found that an instance of Vim was installed on this VPS.



Excerpt:

[root@spt1 work]# vim --version

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 21 2016 17:10:41) Included patches: 1-207, 209-629 Modified by <bugzilla@redhat.com> Compiled by <bugzilla@redhat.com> Huge version without GUI. Features included (+) or not (-): +acl +farsi +mouse_netterm +syntax +arabic +file_in_path +mouse_sgr +tag_binary +autocmd +find_in_path -mouse_sysmouse +tag_old_static -balloon_eval +float +mouse_urxvt -tag_any_white -browse +folding +mouse_xterm -tcl ++builtin_terms -footer +multi_byte +terminfo +byte_offset +fork() +multi_lang +termresponse +cindent +gettext -mzscheme +textobjects -clientserver -hangul_input +netbeans_intg +title -clipboard +iconv +path_extra -toolbar +cmdline_compl +insert_expand +perl +user_commands +cmdline_hist +jumplist +persistent_undo +vertsplit +cmdline_info +keymap +postscript +virtualedit +comments +langmap +printer +visual +conceal +libcall +profile +visualextra +cryptv +linebreak +python/dyn +viminfo +cscope +lispindent -python3 +vreplace +cursorbind +listcmds +quickfix +wildignore +cursorshape +localmap +reltime +wildmenu +dialog_con -lua +rightleft +windows +diff +menu -ruby +writebackup +digraphs +mksession +scrollbind -X11 -dnd +modify_fname +signs -xfontset -ebcdic +mouse +smartindent -xim +emacs_tags -mouseshape -sniff -xsmp +eval +mouse_dec +startuptime -xterm_clipboard +ex_extra +mouse_gpm +statusline -xterm_save +extra_search -mouse_jsbterm -sun_workshop -xpm system vimrc file: "/etc/vimrc" user vimrc file: "$HOME/.vimrc" 2nd user vimrc file: "~/.vim/vimrc" user exrc file: "$HOME/.exrc" fall-back for $VIM: "/etc" f-b for $VIMRUNTIME: "/usr/share/vim/vim74" Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -O2 -g -pipe -Wall -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 Linking: gcc -Wl,-E -Wl,-rpath,/usr/lib64/perl5/CORE -L/usr/local/lib -Wl,--as-needed -o vim -lm -lnsl -lselinux -ltinfo -lacl -lattr -lgpm -Wl,-E -Wl,-rpath,/usr/lib64/perl5/CORE -fstack-protector -L/usr/lib64/perl5/CORE -lperl -lresolv -lnsl -ldl -lm -lcrypt -lutil -lpthread -lc



Vim version = 7.4.



Some reading indicates that
vim
is an extension of the
vi
editor, and that the
vi
commands can be expected to work in the same way in
vim
.


Excerpt from:
www.vim.org/viusers.php

Vim is backwards compatible with vi. Switching from vi to Vim is easy: you can keep all the things that Vi offers and gain a large range of new features.




A previous project, Offline installation of a C compiler on Centos 6.9 Minimal on Kalkin, includes a basic guide to
vi
commands. Browse to the article and search for the string "# WHAT IS VI?".


Full excerpt:

Let's look at basic
vi
commands.

The following material originally came from:
www.cs.colostate.edu/helpdocs/vi.html
and has been edited.

# WHAT IS VI?

The default editor that comes with the UNIX operating system is called vi (visual editor).

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, and
- Insert mode in which entered text is inserted into the file.

In the command mode, every character typed is a command that does something to the text file being edited; a character typed in the command mode may even cause the vi editor to enter the insert mode. In the insert mode, every character typed is added to the text in the file; pressing the [Esc] (Escape) key turns off the Insert mode.


# NOTES

The cursor moves to the bottom of the screen whenever a colon (:) is typed. This type of command is completed by hitting the Return (or Enter) key.

The symbol ^ before a letter means that the [Ctrl] key should be held down while the letter key is pressed.


# TO GET INTO AND OUT OF VI

[open file for editing]
$ vi [filepath]

[quit vi, writing out modified file to file named in original invocation]
:x[Return]

[quit vi, writing out modified file to file named in original invocation]
:wq[Return]

[quit (or exit) vi]
:q[Return]

[quit vi even though latest changes have not been saved for this vi call]
:q![Return]


# MOVING THE CURSOR

On some UNIX platforms, the arrow keys may be used; however, since vi was designed with the Qwerty keyboard (containing no arrow keys) in mind, the arrow keys sometimes produce strange effects in vi and should be avoided.

[move cursor down one line]
j or [Return] or down-arrow

[move cursor up one line]
k or up-arrow

[move cursor left one character]
h or [Backspace] or left-arrow

[move cursor right one character]
l or [Space] or right-arrow

[move cursor to start of current line (the one with the cursor)]
0 [zero]

[move cursor to end of current line]
$

[move cursor to beginning of next word]
w

[move cursor back to beginning of preceding word]
b

[move cursor to first line in file]
:0[Return] or 1G

[move cursor to line n]
:n[Return] or nG

[move cursor to last line in file]
:$[Return] or G


# SCREEN MANIPULATION

The following commands allow the vi editor screen (or window) to move up or down several lines and to be refreshed.

[move forward one screen]
^f

[move backward one screen]
^b

[move down (forward) one half screen]
^d

[move up (back) one half screen]
^u

[redraws the screen]
^l

[redraws the screen, removing deleted lines]
^r


# ADDING, CHANGING, AND DELETING TEXT

The main purpose of an editor is to create, add, or modify text for a file.

Perhaps the most important command is the one that allows you to back up and undo your last action. Unfortunately, this command acts like a toggle, undoing and redoing your most recent action. You cannot go back more than one step.

[UNDO WHATEVER YOU JUST DID; a simple toggle]
u


## INSERTING OR ADDING TEXT

The following commands allow you to insert and add text. Each of these commands puts the vi editor into insert mode; thus, the [Esc] key must be pressed to terminate the entry of text and to put the vi editor back into command mode.

[insert text before cursor, until [Esc] hit]
i

[insert text at beginning of current line, until [Esc] hit]
I

[append text after cursor, until [Esc] hit]
a

[append text to end of current line, until [Esc] hit]
A

[open and put text in a new line below current line, until [Esc] hit]
o

[open and put text in a new line above current line, until [Esc] hit]
O


## CHANGING TEXT

The following commands allow you to modify text.

[replace single character under cursor (no [Esc] needed)]
r

[replace characters, starting with current cursor position, until [Esc] hit]
R

[change the current word with new text, starting with the character under cursor, until [Esc] hit]
cw

[change N words beginning with character under cursor, until [Esc] hit; e.g., c5w changes 5 words]
cNw

[change (replace) the characters in the current line, until [Esc] hit]
C

[change (replace) the entire current line, stopping when [Esc] is hit]
cc

[change (replace) the next N lines, starting with the current line, stopping when [Esc] is hit]
Ncc or cNc


## DELETING TEXT

The following commands allow you to delete text.

[delete single character under cursor]
x

[delete N characters, starting with character under cursor]
Nx

[delete the single word beginning with character under cursor]
dw

[delete N words beginning with character under cursor; e.g., d5w deletes 5 words]
dNw

[delete the remainder of the line, starting with current cursor position]
D

[delete entire current line]
dd

[delete N lines, beginning with the current line; e.g., 5dd deletes 5 lines]
Ndd or dNd


## CUTTING AND PASTING TEXT

The following commands allow you to copy and paste text.

[copy (yank, cut) the current line into the buffer]
yy

[copy (yank, cut) the next N lines, including the current line, into the buffer]
Nyy or yNy

[put (paste) the line(s) in the buffer into the text after the current line]
p


# OTHER COMMANDS


## SEARCHING TEXT

A common occurrence in text editing is to replace one word or phase by another. To locate instances of particular sets of characters (or strings), use the following commands.

[search forward for occurrence of string in text]
/string

[search backward for occurrence of string in text]
?string

[move to next occurrence of search string]
n

[move to next occurrence of search string in opposite direction]
N


## DETERMINING LINE NUMBERS

Being able to determine the line number of the current line or the total number of lines in the file being edited is sometimes useful.

[returns line number of current line at bottom of screen]
:.=

[returns the total number of lines at bottom of screen]
:=

[provides the current line number, along with the total number of lines, in the file at the bottom of the screen]
^g


# SAVING AND READING FILES

These commands permit you to input and output files other than the named file with which you are currently working.

[read file named "filename" and insert after current line (the line with cursor)]
:r filename[Return]

[write current contents to file named in original vi call]
:w[Return]

[write current contents to a new file named "newfile"]
:w newfile[Return]

[write the contents of the lines numbered 12 through 35 to a new file named "smallfile"]
:12,35w smallfile[Return]

[write current contents over a pre-existing file named "prevfile"]
:w! prevfile[Return]





On my local machine:

Create new project directory:
testing_a_list_of_vim_commands

Create new work directory named "work" in the project directory.


Open a Cygwin64 Terminal and use
ssh
to log into the CentOS VPS. I was working recently on another project in
/home/work
, so I'll create a second work directory "work2".

Admin@Admin-PC ~

$ ssh root@edgecase-test.net
Last login: Sat Oct 6 16:08:14 2018 from [IP address string]

[root@spt1 ~]# cd /home


[root@spt1 home]# ls -1

work

[root@spt1 home]# mkdir work2


[root@spt1 home]# cd work2


[root@spt1 work2]# ls -1


[root@spt1 work2]# vi --version

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Dec 21 2016 17:10:08) Included patches: 1-207, 209-629 Modified by <bugzilla@redhat.com> Compiled by <bugzilla@redhat.com> Small version without GUI. Features included (+) or not (-): +acl -farsi -mouse_sgr -tag_old_static -arabic -file_in_path -mouse_sysmouse -tag_any_white -autocmd -find_in_path -mouse_urxvt -tcl -balloon_eval -float -mouse_xterm +terminfo -browse -folding +multi_byte -termresponse +builtin_terms -footer -multi_lang -textobjects -byte_offset +fork() -mzscheme -title -cindent -gettext -netbeans_intg -toolbar -clientserver -hangul_input -path_extra -user_commands -clipboard +iconv -perl -vertsplit -cmdline_compl -insert_expand -persistent_undo -virtualedit +cmdline_hist +jumplist -printer +visual -cmdline_info -keymap -profile -visualextra -comments -langmap -python -viminfo -conceal -libcall -python3 -vreplace -cryptv -linebreak -quickfix +wildignore -cscope -lispindent -reltime -wildmenu -cursorbind -listcmds -rightleft +windows -cursorshape -localmap -ruby +writebackup -dialog -lua -scrollbind -X11 -diff -menu -signs -xfontset -digraphs -mksession -smartindent -xim -dnd -modify_fname -sniff -xsmp -ebcdic -mouse -startuptime -xterm_clipboard -emacs_tags -mouse_dec -statusline -xterm_save -eval -mouse_gpm -sun_workshop -xpm -ex_extra -mouse_jsbterm -syntax -extra_search -mouse_netterm -tag_binary system vimrc file: "/etc/virc" user vimrc file: "$HOME/.vimrc" 2nd user vimrc file: "~/.vim/vimrc" user exrc file: "$HOME/.exrc" fall-back for $VIM: "/etc" f-b for $VIMRUNTIME: "/usr/share/vim/vim74" Compilation: gcc -c -I. -Iproto -DHAVE_CONFIG_H -O2 -g -pipe -Wall -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1 Linking: gcc -L/usr/local/lib -Wl,--as-needed -o vim -lm -lselinux -ltinfo -lacl -lattr





Note that the program accessed with the command
vi
is the "small version" and that the program accessed with the command
vim
(as shown in an earlier excerpt) is the "huge version". The huge version has most of the available features enabled, and the small one does not.


Now, test all of the
vi
commands shown in the excerpt containing a command list. Write text in the Vim editor as is necessary for testing.


[root@spt1 work2]# vi test.txt



[some time passes]


Done.


Let's clean up.


[root@spt1 work2]# ls -1

foo.txt
fo.txt
test.txt

[root@spt1 work2]# cd ..


[root@spt1 home]# ls -1

work
work2

[root@spt1 home]# rm -rf work2


[root@spt1 home]# ls -1

work

[root@spt1 home]# exit

logout
Connection to edgecase-test.net closed.

Admin@Admin-PC ~

$










Let's also test the commands in the huge version.

Open a Cygwin64 Terminal and use
ssh
to log into the CentOS VPS. Create a work directory named "work3".

Admin@Admin-PC ~

$ ssh root@edgecase-test.net
Last login: Thu Jan 17 11:19:21 2019 from [address string]

[root@spt1 ~]# cd /home


[root@spt1 home]# ls -1

work

[root@spt1 home]# mkdir work3


[root@spt1 home]# cd work3


[root@spt1 work3]# ls -1


[root@spt1 work3]# vim test.txt



Note the use of the
vim
command, in order to use the huge version.

Test all of the
vi
commands shown in the excerpt containing a command list. Write text in the Vim editor as is necessary for testing.


[some time passes]


Done.


Let's clean up.



[root@spt1 work3]# ls -1

hello.txt
test.txt

[root@spt1 work3]# cd ..


[root@spt1 home]# ls -1

work
work3

[root@spt1 home]# rm -rf work3


[root@spt1 home]# ls -1

work

[root@spt1 home]# exit

logout
Connection to edgecase-test.net closed.

Admin@Admin-PC ~

$








Good. That's the end of this project.