Posts Tagged "vim"

Come configurare mc (midnight commander) per usare vim durante la modifica dei file

Se volete che premendo F4 in midnight commander vi esca il vostro caro vecchio vim invece dell’editor allora dovete andare in Opzioni->Configurazione e togliere la spunta sulla voce “Usa editor interno

Se dopo questa operazione ancora non vedete il vim ma per esempio nano, allora eseguite

sudo update-alternatives –config-editor

e selezionate dalla lista vim-basic

Sostituire i ^M con vim sotto linux

A volte capita di trovarsi per le mani un file formattato con i ritorni a capo (carriage returns) fatti in stile dos. La cosa potrebbe portare dei gravi danni soprattutto se il file in questione è un file di configurazione di qualche servizio.

Per sistemare questo problema basta aprire il file in vim e scrivere

:1,$s/^M//g

N.B non fare copia incolla del suddetto codice, in quanto per scrivere “^M” bisogna premere Ctrl-V seguito da enter.

Configurazione vimrc

filetype plugin indent on
"set mouse=a
scriptencoding utf-8
set viewoptions=folds,options,cursor,unix,slash " better unix / windows compatibility
set history=1000

set backup                      " backups are nice ...
"set cursorline                  " highlight current line
hi cursorline guibg=#333333     " highlight bg color of current line
hi CursorColumn guibg=#333333   " highlight cursor

if has('cmdline_info')
        set ruler                   " show the ruler
        set rulerformat=%30(%=\:b%n%y%m%r%w\ %l,%c%V\ %P%) " a ruler on steroids
        set showcmd                 " show partial commands in status line and
                                    " selected characters/lines in visual mode
    endif
if has('statusline')
        set laststatus=2

        " Broken down into easily includeable segments
        set statusline=%<%f\   " Filename
        set statusline+=%w%h%m%r " Options
        set statusline+=\ [%{&ff}/%Y]            " filetype
        set statusline+=\ [%{getcwd()}]          " current dir
        "set statusline+=\ [A=\%03.3b/H=\%02.2B] " ASCII / Hexadecimal value of char
        set statusline+=%=%-14.(%l,%c%V%)\ %p%%  " Right aligned file nav info
    endif

set backspace=indent,eol,start  " backspace for dummys
    set linespace=0                 " No extra spaces between rows
    set nu                          " Line numbers on
    set showmatch                   " show matching brackets/parenthesis
    set incsearch                   " find as you type search
    set hlsearch                    " highlight search terms
    set winminheight=0              " windows can be 0 line high
    set ignorecase                  " case insensitive search
    set smartcase                   " case sensitive when uc present
    set wildmenu                    " show list instead of just completing
    set wildmode=list:longest,full  " comand <Tab> completion, list matches, then longest common part, then all.
    set whichwrap=b,s,h,l,<,>,[,]   " backspace and cursor keys wrap to
    set scrolljump=5                " lines to scroll when cursor leaves screen
    set scrolloff=3                 " minimum lines to keep above and below cursor
    set foldenable                  " auto fold code
    set gdefault                    " the /g flag on :s substitutions by default

set nowrap                      " wrap long lines
    set autoindent                  " indent at the same level of the previous line
    set shiftwidth=4                " use indents of 4 spaces
    set noexpandtab                 " tabs are tabs, not spaces
    set tabstop=4                   " an indentation every four columns
    "set matchpairs+=<:>                " match, to be used with %
    set pastetoggle=<F12>           " pastetoggle (sane indentation on pastes)
    "set comments=sl:/*,mb:*,elx:*/  " auto format comment blocks
    " Remove trailing whitespaces and ^M chars
 autocmd FileType c,cpp,java,php,js,twig,xml,yml autocmd BufWritePre <buffer> :call setline(1,map(getline(1,"$"),'substitute(v:val,"\\s\\+$","","")'))

" visual shifting (does not exit Visual mode)
    vnoremap < <gv
    vnoremap > >gv

set smartindent
autocmd BufRead *.cfg inoremap # X<c-h>#

function! InitializeDirectories()
  let separator = "."
  let parent = $HOME
  let prefix = '.vim'
  let dir_list = {
              \ 'backup': 'backupdir',
              \ 'views': 'viewdir',
              \ 'swap': 'directory' }

  for [dirname, settingname] in items(dir_list)
      let directory = parent . '/' . prefix . dirname . "/"
      if exists("*mkdir")
          if !isdirectory(directory)
              call mkdir(directory)
          endif
      endif
      if !isdirectory(directory)
          echo "Warning: Unable to create backup directory: " . directory
          echo "Try: mkdir -p " . directory
      else
          let directory = substitute(directory, " ", "\\\\ ", "")
          exec "set " . settingname . "=" . directory
      endif
  endfor
endfunction
call InitializeDirectories()

Per disabilitare i numeri durante editing (per esempio per fare copia incolla) digitare

:set nu!

Impostare vim come editor predefinito in Ubuntu

Non so a quanti di voi è capitato di usare per esempio crontab e trovarsi di fronte invece del caro vecchio vim l’interfaccia di “nano”.

Per poter impostare vim come editor globale del sistema è sufficiente digitare il seguente commando:

Continua…

Scorciatoie per Vim

Da Tastiera

Ctrl-U : Page Up
Ctrl-D: Page Down
gg: Vai all’inizio del file
G: Vai alla fine del file
>: Indent (ripetere 2 volte)
<: Unindent
dd: elimina la riga corrente
yy: duplica la riga corrente (viene messo in buffer, dopo si puo incollare con “p”)

Commandi:

:sav filename – Salva con nome
:78 – Vai alla riga 78

Modificare le impostazioni di vim.

Creare il file .vimrc nella home directory, e inserire

syntax onnoremap <Space> <PageDown>