Ubuntu/Linux - Grunder [Mobil, Eng, Eng/Mob] | GRUB | GRUB2 | PXE | skript | ssh | mail | Internet | backup
vi, vim - first steps
samtliga sidor
hide fine print
NOTE! This page has a new address:
Please update your bookmark.
Latest update at:
October 20, 2012

[Tillbaka till: Ubuntu - Installation, partitionering. Grunder.]
[Tillbaka till: GRUB - Reparera starthanteraren.]

The "Hello World" of ssh

Some pointers on:
vi/vim, screen, ssh, mutt, cron, kill

This page just gives a few pointers on how to get started with some programs often used over ssh to control servers via the command line.


vi is a text editing program with many possibilities. It does not use a graphic environment and can be run over f.ex. SSH connection.
The most commonly used editor for Ubuntu is Gedit which, in its basic form, does not support macros (the storage of keystroke combinations). For the use of macros and other editing functions, vi/vim can be useful.

There are many good vi/vim and screen manuals on the net. This page just tries to list a few commands that are useful during your first sessions.


To invoke vim type: "vim myfile.txt". If myfile.txt does not exist, it will be created.

vim starts in command mode. To enter text, enter insert mode by pressing "i". To leave insert mode and return to command mode, press [escape]. In command mode, some commands start with ":", these commands are said to be executed in ex mode. Commands in ex mode are displayed at the bottom of the screen.

i insert; leave command mode, enter insert mode
[escape] leave insert mode, enter command mode
:wq write, quit; save changes, exit vim.
:!q force quit; changes discarded, exit vim.

In command mode the keys "pg up/down" or arrows can be used to move the cursor. To find text, just type / followed by the text.
[pg down] go down one screen
[pg up] go up one screen
[arrow keys] move cursor
w move cursor one word
b move cursor one word backwards
/foo find (go to) "foo"
n find next "foo"
N find preceding "foo"
J join line with following line

Delete one character by typing x, delete one line by typing dd. Copy one line by by typing yy ("yank"). Paste is p. repeat by preceding command with numbers: 5yy will copy 5 lines. undo is u.
x delete one character
dd delete one line
3yy copy three lines ("yank")
y5w copy five words
p paste
10dd delete ten lines
u undo

There are different registers (indicated by a, b, c ...) to where you can copy text. You can cut, paste and append to registers.
You can use a register for storing and executing a macro. To store a macro just enter the keystrokes on a line. After that store the line ("yank" it) into a register. If you wish to edit the macro, just paste the register (the line of keystrokes you stored as a macro) into the file. After that just edit the line and store it again into the register. If you want to include special characters, precede them with ctrl/v (sometimes ctrl/q). Sometimes ctrl/v is mapped to paste, then use ctrl/q instead.
If you are entering text that will be stored as a macro and you wish the macro to leave insert mode for command mode you should thus press [ctrl/q] [escape]

Registers are stored between sessions.
"k2yy yank (copy) 2 lines to register k
"Ky2w yank (copy and append) 2 words to register k
"kp paste register k
@k execute register k as a macro
@@ execute last executed macro again

Lines are indicated with numbers, the current line is indicated by ".".
:set number show line numbers
:set nonumber hide line numbers
:15,17d delete from line 15 to 17
:.-3,.+4y yank, starting 3 lines before current to 4 lines after

If you need to move the cursor in a macro, you must use the basic cursor movement commands of vi (not arrow keys). If you want to enter a special character, preceed it with ctrl/v. (ctrl/q if ctrl/v is mapped to paste) To make a macro leave insert mode you can therefore press ctrl/v and then <escape> when you edit the macro. (Corresponds to pressing <escape>, the usual command for leaving insert mode.)
If you want to search in a macro in command mode (/foo<carriage return>) you enter /foo ctrl/v <carriage return>.
If you want to use commands in ex mode you simply enter : before the command as you sould when using the editor without macros.
h cursor left
l cursor right
j cursor down
k cursor up
0 beginning of line
$ end of line
:0 go to first line
:$ go to last line
+ go down one line
- go up one line
5+ go down five lines

There is a visual mode in vim. Press v and move the crusor. The text covered will be marked by white background. Press y (yank) or d (delete) to end visual mode. The text copied can be pasted by pressing p.

The visual mode is one way to "cut and paste" content within a line. Another way is to use markers. ma will introduce a marker (named a) at cursor. (Use any letter a-z: ma, mb, mc ...) Move to marker by pressing `a, `b etc. (`ad`b will delete between markers a and b, same for yank `ay`b) A third way to find references to lines is to introduce line numbers: ":set number"; Naturally you can also move material within a line by yanking/deleting it. 5x will delete five characters that can later be pasted.

Please note this:
Lines are pasted after the line the cursor is on. When you yank (copy) and paste in visual mode the selection starts before the cursor and ends after the cursor. - In non-graphc editors like vi/vim the cursor is over a character (like in the "old days" ;) ). (In graphical editors the cursor is often an I-beam between characters.) Using non-graphical editors one must therefore be aware of if one will introce/include material before (like command i) or after (like command a) the cursor.

Substitution is made by the command s. Regular expressions are ON. g is the "global" command.
:s/foo/bar/gi Change every occurence of "foo" for "bar" (case insensitive).
:3,34s/foo/bar/gi same as above, from line 3 to 34
:g/foo/d delete all lines where foo is found

If you open several files, they are stored in buffers. To be able to leave buffers without saving changes you must issue the command ":set hidden". ":n#3" will display buffer #3. ":ls" will list buffers ":bd" will dekete the current buffer. (screen is another way to open several files for editing. screen will open more shells.)
:set hidden allow hiding of buffers without saving
:n myFile2 open new file (buffer)
:n# jump to most recently used buffer
:n#3 jump to buffer #3
:ls list buffers
:bd delete the curent buffer (:bd! if modifications not written)
:bd4 delete buffer #4 (:bd!4 if modifications not written)

Some useful I/O commands:
:w file2 write file (current buffer) to file2
:2,7w >> file append from line 2 to 7 to file file2
:r file2 the contents of file2 are included below the current line

To edit more quickly:
A append at end of line
xp change order of two characters
r replace one character
The change command will indicate by "$" what you decided to change.
cw change word
cc change line
C change to end of line
dw delete word
dd delete line
D delete to end of line


The command screen makes it possible to open several windows over f.ex. a SSH connection. If the connection breaks the windows with their content will still be accessible next time you connect to screen in the host computer.

screen start screen (one window opens)
screen -ls list available screens (one is usually enough)
screen -r attach to detached screen (which may contain several windows)
screen -x attach to attached screen, f.ex. after connection broke (the screen may contain several windows)

How to manipulate windows within screen
ctrl/a c create new window
ctrl/a n go to next window
ctrl/a d detach from screen
c/a c/w list windows
ctrl/a ? help
ctrl/d or exit exit to another window or from last window to bash
- Don't mix up c/a c/d (detach) with c/d (exit).
- - detach means leaving the entire screen with all windows in it
- - exit (ctrl/d) means terminating one window. When the last window in a screen is terminated, you come back to bash. This is indicated by a message ("screen terminating"). If you press ctrl/d or issue command exit in bash, the entire connection terminates.
- You can also often use the control character before the second character "ctrl/a d" will be the same as "ctrl/a ctrl/d"
- c/a c/w will help you see if you are still in screen or if all windows (and screen) have terminated and you are back in bash. If you make exit from a screen-window you just get back to another screen-window or bash. But if you make exit from bash your connection will terminate. You can also often see if you are in a screen-window. When backspacing reaches start of line, the screen flashes. It does not do that in bash. When you exit from the last screen window (and command screen terminates) you get a message ("screen terminating").
If you detach the screen, and all its windows, (by ctrl/a ctrl/d) you can later, at any time, reattach to the screen and its windows by command "screen -r". If you have several screens running (usually not necessary) you can indicate to which screen you want to reattach by isssuing the command "screen -x <name of screen as given in "screen -ls">".


ssh -D 8080 -f -C -q -N myuser@myserver.com port forwarding
ssh -X myuser@myserver.com X-forwarding

If you use Windows for the ssh client, the lightweight ssh client Putty is excellent. For port forwarding choose connection>ssh>tunnels>: dynamic, source:8080 don't forget to press "add"

To connect to the forwarded port using Firefox on your local computer (ssh-client). Choose Tools>Options>Advanced>Network>Connection>Settings>Manual proxy config: socks proxy socks5, proxy:localhost, port:8080 Firefox will now communicate over localhost, port 8080 ( = loopback:8080). localhost:8080 will just "bounce" the data between Firefox and the ssh-encrypted tunnel to the ssh server. You can verify that the forwarding works by surfint to sites like whatsmyip.org and iplocation.net .

To make X-forwarding with Putty you just choose "enable X forwarding" under the ssh menu in Putty. (no need to choose tunnels in Putty) You must also download the server Xming which makes it possible for Windows to accept X (Linux graphics) data. Xming installs easily without intimidating questions or choices ;) X-forwarding will show the graphic interface of each program run on the server. If you want the entire desktop environment and programs forwarded you must install VNC server (like Vino, for example). Any kind of X-forwarding will require more bandwidth than port forwarding (as described above).


mutt is a CLI mail client. It will read the mail spool queue and, if you choose s (save), move emails from the spool queue to mbox files. Mutt can store emails in both common formats (mbox and mdir).

mutt start mutt and read mail spool queue
s save mail to mbox file and mark it for deletion (will be deleted after confirmation when mutt exits)
i exit [is shown in the top row]
q quit [is shown in the top row]
y send [is shown in the top row]
c? show available mailboxes
m compose email (your favorite editor will be started)


Running the command "crontab -e" will start an editor with which you can edit a file that controls how to schedule jobs.

# http://www.linuxhelp.net/guides/cron/
# */1 is the same as *
# .---------------- minute (0 - 59)<>
# |   .------------- hour (0 - 23)
# |   |   .---------- day of month (1 - 31)
# |   |   |   .------- month (1 - 12) OR jan,feb,mar,apr ...
# |   |   |   |  .----- day of week (0-7) (Sunday=0 or 7) OR sun,mon...
# |   |   |   |  |
# *   *   *   *  *  command to be executed
# (table modified from wikipedia)
# examples
55 */2 * * *  /home/yngve/Downloads/dns/yldns  >> /dev/null 2>&1
*/1 * * * *  /home/yngve/Downloads/dns/yldns  >> /home/yngve/Downloads/dns/yl_dns_log 2>&1
55 * * * *  /home/yngve/Downloads/dns/yldns  >> /home/yngve/Downloads/dns/yl_dns_log 2>&1
*/3 * * * *  /home/yngve/Downloads/dns/yl_port_fwd  >> /home/yngve/Downloads/dns/yl_port_fwd_log 2>&1
40 00-23 * * *  /home/yngve/Downloads/dns/yl_port_fwd  >> /home/yngve/Downloads/dns/yl_port_fwd_log 2>&1
# STDIN STDOUT STDERR are numbered 0, 1, 2
# if no number is given you refer to STDOUT
# yldns  >> /dev/null redirects STDOUT to waste
# yldns  >> /dev/null 2>&1 will redirect both STDOUT and STDER


ps -ux find all my processes
ps -u joe find all processes for user "joe"
kill -9 1234 kill process #1234

Länkar till övriga sidor om Ubuntu och Linux på denna domän finns här.


Sidan underhålls av ycc, en av Ubuntu Sveriges medlemmar. Innehåll och design avspeglar nödvändigtvis inte Ubuntu Sveriges uppfattning.