[!TIP|label:references:]

shortcuts

combine multiple lines with or without space

  • with space: J
  • without space: gJ
J-gJ
1.3.5.1 -- J-gJ

Capitalize words and regions easily

shortcut comments
gcw capitalize word (from cursor position to end of word)
gcW capitalize WORD (from cursor position to end of WORD)
gciw capitalize inner word (from start to end)
gciW capitalize inner WORD (from start to end)
gcis capitalize inner sentence
gc$ capitalize until end of line (from cursor postition)
gcgc capitalize whole line (from start to end)
gcc capitalize whole line
{Visual}gc capitalize highlighted text
" vimrc
if ( &tildeop )
  nmap gcw  guw~l
  nmap gcW  guW~l
  nmap gciw guiw~l
  nmap gciW guiW~l
  nmap gcis guis~l
  nmap gc$  gu$~l
  nmap gcgc guu~l
  nmap gcc  guu~l
  vmap gc   gu~l
else
  nmap gcw  guw~h
  nmap gcW  guW~h
  nmap gciw guiw~h
  nmap gciW guiW~h
  nmap gcis guis~h
  nmap gc$  gu$~h
  nmap gcgc guu~h
  nmap gcc  guu~h
  vmap gc   gu~h
endif

Switching case of characters

[!NOTE|label:references:]

  • lowercase

    gu
    
    " example
    Hello -> hello
    
  • uppercase

    gU
    
    " example
    Hello -> HELLO
    
  • reverse

    g~
    
    " example
    Hello -> hELLO
    
  • more

    • g~3w : toggle case of the next three words
    • g~$ : toggle case to the end of line
    • g~iw : toggle case of the current word (inner word – cursor anywhere in word)
    • g~~ == g~g~ : toggle case of the current line (same as V~ - cursor anywhere in the line)
    • gUU == gUgU : to uppercase of the current line (same as V~ - cursor anywhere in the line)
    • guu == gugu : to lowercase of the current line (same as V~ - cursor anywhere in the line)

counter

[!NOTE|label:references]

nnoremap <leader>cr  0yt=A<C-r>=<C-r>"<CR><Esc>
vim calculator
1.3.5.2 -- vim calculator

commands

search (in)sensitive

[!NOTE|label:reference:]

CMD ignorecase smartcase MATCHES
foo off - foo
foo on - foo Foo FOO
foo on on foo Foo FOO
Foo on on Foo
Foo on - foo Foo FOO
\cfoo - - foo Foo FOO
foo\C - - foo
:set ignorecase
:set smartcase
/example      " Case insensitive
/Example      " Case sensitive
/example\C    " Case sensitive
/Example\c    " Case insensitive
search-case-sensitive
1.3.5.3 -- search-case-sensitive

search with \V

pattern result
/a.k.a<CR> backward
a.k.a
/a\.k\.a<CR> backward
a.k.a
/Va.k.a<CR> backward
a.k.a

search in visual mode

[!NOTE]

  • v go to visual mode
  • /KEYWORDS search next KEYWORDS | ?KEYWORDS search previous KEYWORDS
  • enter
select until find
1.3.5.4 -- select until find

sort lines

[!NOTE|label:references:]

  • sort

    :{range}sort
    
    sort lines
    1.3.5.5 -- sort lines
  • sort and unique

    :{range}sort u
    
    sort lines
    1.3.5.6 -- sort lines

list all filetype

:echo getcompletion('', 'filetype')
  • or

    :echo getcompletion('c', 'filetype')
    
  • or and use: for f in GetFiletypes() | echo f | endfor

    function! GetFiletypes()
      " Get a list of all the runtime directories by taking the value of that
      " option and splitting it using a comma as the separator.
      let rtps = split(&runtimepath, ",")
      " This will be the list of filetypes that the function returns
      let filetypes = []
    
      " Loop through each individual item in the list of runtime paths
      for rtp in rtps
        let syntax_dir = rtp . "/syntax"
        " Check to see if there is a syntax directory in this runtimepath.
        if (isdirectory(syntax_dir))
          " Loop through each vimscript file in the syntax directory
          for syntax_file in split(glob(syntax_dir . "/*.vim"), "\n")
            " Add this file to the filetypes list with its everything
            " except its name removed.
            call add(filetypes, fnamemodify(syntax_file, ":t:r"))
          endfor
        endif
      endfor
    
      " This removes any duplicates and returns the resulting list.
      " NOTE: This might not be the best way to do this, suggestions are welcome.
      return uniq(sort(filetypes))
    endfunction
    

newline \r

redirect cmd

[!NOTE|label:references:]

  • redir to file

    :redir > ~/Desktop/debug.txt
    :silent highlight
    :redir END
    
    • or
      :write | redir >> % | silent registers | redir END | edit
      
  • to new window

    :redir @a | silent digraph | redir END | new +setl\ buftype=nofile\ bufhidden=wipe | put! a
    
  • to TabMessage

    [!NOTE|label:references:]

    function! TabMessage( cmd )
      redir => message
      silent execute a:cmd
      redir END
      if empty( message )
        echoerr "no output"
      else
        tabnew               " use "new" instead of "tabnew" below if you prefer split windows instead of tabs
        setlocal buftype=nofile bufhidden=wipe noswapfile nobuflisted nomodified
        silent put=message
      endif
    endfunction
    command! -nargs=+ -complete=command TabMessage call TabMessage(<q-args>)
    
    " usage:
    :TabMessage highlight
    
redir to debug
1.3.5.7 -- redir to debug

format json in vim

[!NOTE|label:references:]

:%!jq .

" or
:%!python -m json.tool

run command in multiple buffers

related commands:

  • :argdo : all files in argument list
  • :bufdo : all buffers
  • :tabdo : all tabs
  • :windo : all windows

reference:

:bufdo <command>
  • replace

    # regular
    :%s/<str>/<str_new>/ge
    
    # for all buffers
    :bufdo %s/<str>/<str_new>/ge | update
    
  • force the bufdo to continue without saving files via :bufdo!

show ascii under cursor

[!NOTE|label:references:]

  • keyboard
    • ga
  • commands
    :as
    " or
    :ascii
    
ascii
1.3.5.8 -- ascii

config

disable vim beep

# ~/.vimrc
set noerrorbells novisualbell visualbell                            " ┐ turn off
set t_vb=                                                           " ┘ error/normal beep/flash

run vim commands in terminal

[!NOTE|label:manual:]

$ man vim
...
OPTIONS
  +{command}
  -c {command}
             {command} will be executed after the first file has been read.  {command} is interpreted
             as an Ex command.  If the {command} contains spaces it must be enclosed in double quotes
             (this depends on the shell that is used).  Example: Vim "+set si" main.c
             Note: You can use up to 10 "+" or "-c" commands.

  --cmd {command}
             Like using "-c", but the command is executed just before processing any vimrc file.  You
             can use up to 10 of these commands, independently from "-c" commands.
$ vim -es -c "set ff? | q"
  fileformat=unix

vim open file and go to specific function or linenumber

$ vim +commandHere filename

# or
$ vim +linenumber filename

using vim as a man-page viewer under unix

export PAGER="/bin/sh -c \"unset PAGER;col -b -x | \
       vim -R -c 'set ft=man nomod nolist' -c 'map q :q<CR>' \
       -c 'map <SPACE> <C-D>' -c 'map b <C-U>' \
       -c 'nmap K :Man <C-R>=expand(\\\"<cword>\\\")<CR><CR>' -\""
  • additional highlight

    " DrChip's additional man.vim stuff
    syn match manSectionHeading "^\s\+[0-9]\+\.[0-9.]*\s\+[A-Z].*$" contains=manSectionNumber
    syn match manSectionNumber "^\s\+[0-9]\+\.[0-9]*" contained
    syn region manDQString start='[^a-zA-Z"]"[^", )]'lc=1 end='"' contains=manSQString
    syn region manSQString start="[ \t]'[^', )]"lc=1 end="'"
    syn region manSQString start="^'[^', )]"lc=1 end="'"
    syn region manBQString start="[^a-zA-Z`]`[^`, )]"lc=1 end="[`']"
    syn region manBQSQString start="``[^),']" end="''"
    syn match manBulletZone transparent "^\s\+o\s" contains=manBullet
    syn case match
    syn keyword manBullet contained o
    syn match manBullet contained "\[+*]"
    syn match manSubSectionStart "^\*" skipwhite nextgroup=manSubSection
    syn match manSubSection ".*$" contained
    
    hi link manSectionNumber Number
    hi link manDQString String
    hi link manSQString String
    hi link manBQString String
    hi link manBQSQString String
    hi link manBullet Special
    hi manSubSectionStart term=NONE cterm=NONE gui=NONE ctermfg=black ctermbg=black guifg=navyblue guibg=navyblue
    hi manSubSection term=underline cterm=underline gui=underline ctermfg=green guifg=green
    

vim regex

vim pattern

reference:

overview of multi items

pattern magic nomagic matches of the preceding atom
/star * \* 0 or more   (as many as possible)
/\+ \+ \+ 1 or more   (as many as possible)
/\= \= \= 0 or 1   (as many as possible)
/\? \? \? 0 or 1   (as many as possible)
/\{ \{n,m} \{n,m} n to m   (as many as possible)
\{n} \{n} n   exactly
\{n,} \{n,} at least n   (as many as possible)
\{,m} \{,m} 0 to m   (as many as possible)
\{} \{} 0 or more   (as many as possible. same as *)
/\{- \{-n,m} \{-n,m} n to m   (as few as possible)
\{-n} \{-n} n    exactly
\{-n,} \{-n,} at least n   (as few as possible)
\{-,m} \{-,m} 0 to m   (as few as possible)
\{-} \{-} 0 or more   (as few as possible)

overview of ordinary atoms

pattern magic nomagic matches
/^ ^ ^ start-of-line (at start of pattern) /zero-width
/\^ \^ \^ literal '^'
/\_^ \_^ \_^ start-of-line (used anywhere) /zero-width
/$ $ $ end-of-line (at end of pattern) /zero-width
/\$ \$ \$ literal '$'
/\_$ \_$ \_$ end-of-line (used anywhere) /zero-width
/. . \. any single character (not an end-of-line)
/\_. \_. \_. any single character or end-of-line
/\< \< \< beginning of a word /zero-width
/\> \> \> end of a word /zero-width
/\zs \zs \zs anything, sets start of match
/\ze \ze \ze anything, sets end of match
/\%^ \%^ \%^ beginning of file /zero-width
E71
/\%$ \%$ \%$ end of file /zero-width
/\%V \%V \%V inside Visual area /zero-width
/\%# \%# \%# cursor position /zero-width
/\%'m \%'m \%'m mark m position /zero-width
/\%l \%23l \%23l in line 23 /zero-width
/\%c \%23c \%23c in column 23 /zero-width
/\%v \%23v \%23v in virtual column 23 /zero-width

matches the N pattern

  • every 3rd

    \(.\{-}\zsfoo\)\{3}
    
    regex every third
    1.3.5.9 -- regex every third
  • the 3rd

    ^\(.\{-}\zsPATTERN\)\{3}
    
    regex every third
    1.3.5.10 -- regex every third

\v: the following chars in the pattern are "very magic":

  • ^\(.\{-}\zsPATTERN\)\{N} == > \v^(.{-}\zsPATTERN){N}
  • ^\(.\{-}\zs=\)\{N} == > \v^(.{-}\zs\=){N}

NOTICE: after using \v the = should using \= instead

characters

[!NOTE|label:references:]

first char mode max nr of chars max value
(none) decimal 3 255 -
o or O octal 3 377 (255)
x or X hexadecimal 2 ff (255)
u hexadecimal 4 ffff (65535)
U hexadecimal 8 7fffffff (2147483647)
  • show all digraphs

    [!NOTE|label:references:]

    :redir @a | silent digraph | redir END | new +setl\ buftype=nofile\ bufhidden=wipe | put! a
    
    : or
    :redir @a | silent digraph | redir END | new +setl\ buftype=nofile\ bufhidden=wipe | put! a | on
    

insert unicode

micro

[!NOTE|label:references:]

stop macro at the end of line

:let a=line('.')
<....>                  " do micro
:if line('.')==a | exec 'normal @q' | endif
"                                |
"                                v
"                           micro name
stop recursive macro at the end of line
1.3.5.14 -- stop recursive macro at the end of line

others

[!NOTE|label:references:]

startup-options

[!NOTE|label:references:]

  • binary mode by -b
    • binary on
    • textwidth 0
    • modeline off
    • expandtab off

How to Open Files with Vim

comments

[!NOTE|label:references:]

statueline

[!NOTE|label:references:]

  • sample

    if has('statusline')
      set laststatus=2
      set statusline=%#User2#%m%r%*\ %F\ %y,%{&fileformat}
      " set statusline+=\ %{FugitiveStatusline()}                     " set statusline+=\ %{fugitive#statusline()}
      set statusline+=%=\ %-{strftime(\"%H:%M\ %d/%m/%Y\")}\ %b[A],0x%B\ %c%V,%l/%L\ %1*--%n%%--%*\ %p%%\ |
    endif
    
  • vim-airline

ctags

[!NOTE|label:references:]

# Generates ctags.
#   Usage: exfind folly,common cpp,h,thrift | genctags
function genctags () {
  ctags --c++-kinds=+p --extras=+q -L -
}
Copyright © marslo 2020-2023 all right reserved,powered by GitbookLast Modified: 2024-03-12 15:01:30

results matching ""

    No results matching ""