VIM mind Beginner’s Mind

Giacomo Mantani

About Us

Me

  • Ex-student
  • Command-Line addicted
  • Stand on the shoulders of giants
  • CeSeNA Security Member

CeSeNA Security Team

  • we are ~10 active members
  • we love learn and share experiences
  • we meet twice a week (Thursday, Saturday)
  • we partecipate to international competitions

Disclaimer

This presentation was prepared by me (Giacomo Mantani) in his personal capacity. The opinions expressed are the author’s own and do not reflect the view of the CeSeNA Security Team.

First things first

What Is Vim?

We focus on Vim, not Vi.

  • highly configurable text editor built to enable efficient text editing
  • improved version of the Vi editor distributed with most UNIX systems
  • useful for programming, many consider it an entire IDE (me too)
  • born in 1991 (Amiga), 1992 (Unix)

What Vim Is Not?

  • Vim isn’t an editor designed to hold its users’ hands. It is a tool, the use of which must be learned
  • Vim isn’t a word processor. Although it can display text with various forms of highlighting and formatting, it isn’t there to provide WYSIWYG editing of typeset documents (It is great for editing TeX, though)

First steps

Open a File

$ vim FILENAME

or

$ gvim FILENAME
+---------------------------------------+
|#                                      |
|~                                      |
|~                                      |
|~                                      |
|~                                      |
|"file.txt" [New file]                  |
+---------------------------------------+
('#" is the cursor position.)

Modes

NORMAL (<Esc>)

INSERT (i, a, I, A, o, O)

VISUAL (v, V)

REPLACE (r, R)

Command-Line (:)

The editor behaves differently, depending on which mode you are in.

Move Around

Do not ever touch your mouse or arrow keys!

hjkl

gg

G

f

F

}

{

Move Around

Movements Image
Movements Image

Change mode

In NORMAL mode we can switch to:

INSERT VISUAL REPLACE

i, I

v, V

r, R

a, A

o, O

Plus:

  • Command-Line (:)
  • Back to NORMAL (<Esc>)

Close A File

Command-Line Mode

Cmd Action

:q

quit without save

:wq or :x

write and quit

Normal Mode

ZZ

write and quit

Awesomeness!

Move Quickly

Cmd Action

gi

Goto last inserted text

g;

Goto older position in change list

gf

Edit the file under or after the cursor

gd

Goto definition of word under the cursor

Magic

  1. :TOHtml
  2. gzip autocmd
$ vim phrack49.tar.gz # ;)
Demo.

Encryption

:set noundofile viminfo=
:noswapfile edit secrets.txt
  • Use :X instead :x. It prompts a password for write the current file encrypted.
  • key option will be set with :X. Never set it manually, it will be into viminfo*!
  • cryptmethod option. blowfish2 is the best choice.

* The viminfo file is designed to store status information.

Better Encryption

GnuPG Plugin

GnuPG, free implementation of the OpenPGP, allow you to encrypt and sign your data.

Arguing that you don’t care about the right to privacy because you have nothing to hide is no different from saying you don’t care about free speech because you have nothing to say. - Edward Snowden

Rot13

g??

Before

Rot13 on the fly!

After

Ebg13 ba gur syl!

Customize

.vimrc

jak3’s vimrc

Developement

Completation

CTRL-P (previous) CTRL-N (next) match.

  • Whole lines (CTRL-X CTRL-L)
  • keywords in the current file (CTRL-X CTRL-N)
  • keywords in ‘dictionary’ option (CTRL-X CTRL-K)
  • keywords in ‘thesaurus’ option (CTRL-X CTRL-T)
  • keywords in the current and included files (CTRL-X CTRL-I)
  • tags (CTRL-X CTRL-])
  • file names (CTRL-X CTRL-F)
  • definitions or macros (CTRL-X CTRL-D)
  • Vim command-line (CTRL-X CTRL-V)
  • User defined completion (CTRL-X CTRL-U)
  • omni completion (CTRL-X CTRL-O)
  • Spelling suggestions (CTRL-X s )
  • keywords in ‘complete’ option (CTRL-N )

Completion

Do you prefer using <Tab>?

function! CleverTab()
   if strpart( getline('.'), 0, col('.')-1 ) =~ '^\s*$'
      return "\<Tab>"
   else
      return "\<C-N>"
   endif
endfunction
inoremap <Tab> <C-R>=CleverTab()<CR>

Syntax Highlighted

:syntax enable

:set background=dark or :set background=light

:colorscheme solarized

:syntax clear

:syntax off

It is also possible to write your own syntax highlighted.

Documentation

K run a program to lookup the keyword under the cursor. The name of the program is given with the ‘keywordprg’ option (default is “man”).

Demo.

Sessions

Contains information about the file list, window layout, global variables, options and more. Exactly what is remembered is controlled by the ‘sessionoptions’ option.

Cmd Action

:mksession project-euler.vim

Create

:source vimbook.vim

Load

vim -S vimbook.vim

Restore

Tags

A tag is a location where an identifier is defined. Vim let you to directly jump from any place to the tag. Create them with Exuberant CTAGS or Universal CTAGS.

Cmd Action

:tag startlist

Goto startlist definition

CTRL-]

Goto definition under the cursor

CTRL-t

Jump back

:tags

Show traversed tags

Compile

Vim has a set of so called “quickfix” commands. They enable you to compile a program from within Vim and then go through the errors generated and fix them (hopefully).

:make {arguments}

If errors were generated, they are captured and the editor positions you where the first error occurred.

:set makeprg=gcc\ -Wall\ -ggdb3\ -o\ %<\ %

Directory Tree

Netrw

Netrw makes reading files, writing files, browsing over a network or locally easy!

vim scp://hostname/path/to/file

vim ftp://hostname/path/to/file

vim /local/path/

Plugins

  • Completion
  • Highlighting
  • Snippets/Templates
  • Tags
  • Statusline

Vimly

Newsletter

starly.info

HelpDesk

I really enjoy spread knowledge and being helpful and I love to talk with smart people about everything.

  • Email: name dot surname at gmail or studio.unibo.it
  • Facebook: Page

Keep Practice

Try to practice whenever it is possible!

  • vimperator/pentadactyl/vimium
  • use always Vim keybindings when available
  • reply to Vimly questions ;)
  • just use VIM