Editing with vi
May 2, 2015 — 21:27

Author: silver  Category: bsd linux  Comments: Off

..actually mostly vim :)

Config

a few useful settings for ~/.vimrc

set number
filetype indent on
set expandtab
set shiftwidth=2
set softtabstop=2
syntax on
set background=dark
" or: set background=light
" colorscheme default

(" = comment)


Edit modes

i to insert and r to replace a to append d to delete

ESC always switches back to ‘command mode’

copy:

yank (copy) current character: l

yank n characters: yn

yank the current word: yw

vim visual mode:

visual line: SHIFT+V
visual block: CTRL+V

current word:

select the current word (visual)
viw

indent:

auto reindent file:

gg=G

indent line right: >>

indent line left: <<

case:

to upper/lower case: ~ (“tilde”)


Commands

:open <file>

:new <file>

:no paste

:set nu and :set nonu (number)

:set list and :set nolist

tabs:

:set noai tabstop=4 shiftwidth=4 softtabstop=4 noexpandtab

noai = no auto indent
tabstop = actual tab width in spaces
shiftwidth = indent size in spaces
expandtab = tab uses spaces instead of ^I (tab char)
softtabstop = tab is x number of spaces

syntax:

:set syntax=python

:set syntax=ps1.vim

:set filetype=xml

:filetype indent on

if needed, reindent (gg=G)

run external command:

:!<command>

:!ls -la /tmp

:sh (open shell)

format json:

:%!python -m json.tool


Moving

code blocks:

on bracket/curley brace/parentheses, press: %

goto column:

use "<NUMBER>|"
e.g. 80| moves to column 80

center:

to center the screen on your current position, press: zz


Search

history:

same as with ":"
press “/” then “UP” or “DOWN” key

word:

to search for the word you are on
*
#


Bookmarks

set a bookmark called ‘a’

ma

go to the bookmark called ‘a’

(on US kb layout the ` is the key left of 1)

`a


Modes

readonly:

$ vi -R  (or view)

diff:

$ vi -d (or vimdiff)


Package manager

since vim8, “pack” is built in

user location:

  • ~/.vim/pack/<package group>/start/<your package>
  • or, use “opt” dir instead of “start” to not autoload (optional pkg)

in vim:

:packadd <opt-pkg>
:helptags ~/.vim/pack/<package group>/start/<your package>/doc/
:h packages








We use Matomo free and open source web analytics
We also use Jetpack WordPress.com Stats which honors DNT