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
*
#


bookmark:

set a bookmark called ‘a’

ma

go to the bookmark called ‘a’ (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