Vim indentation fails when copy paste

For example I want to copy

void print_syslog(char *message)
{
        openlog("ejudgelog", LOG_CONS|LOG_PID,LOG_USER);
        syslog(LOG_INFO, message);
        closelog();
}

Open in new window

Result
void print_syslog(char *message)
{
        openlog("ejudgelog", LOG_CONS|LOG_PID,LOG_USER);
            syslog(LOG_INFO, message);
                closelog();
}

Open in new window


I have to open nano, copy-paste it and then reopen in vim.
Don't worry, I'm copy pasting my own code.
Nusrat NuriyevAsked:
Who is Participating?
 
Gerwin Jansen, EE MVETopic Advisor Commented:
Try copying using visual mode:
- go to beginning of your text and press v
- move cursor to the end of your 'block' using j
- to copy your 'block' press y
- move cursor to where you want the 'block' copied and press p
0
 
Robert SchuttSoftware EngineerCommented:
Before pasting, turn off auto-indent using:
:se noai

Open in new window

0
 
Nusrat NuriyevAuthor Commented:
unfortunately, it does not help.
// noai

void print_syslog(char *message)
{
        openlog("ejudgelog", LOG_CONS|LOG_PID,LOG_USER);
            syslog(LOG_INFO, message);
                closelog();
}


// ai
void print_syslog(char *message)
{
        openlog("ejudgelog", LOG_CONS|LOG_PID,LOG_USER);
            syslog(LOG_INFO, message);
                closelog();
}

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Robert SchuttSoftware EngineerCommented:
Hmm, it does the trick for me but maybe something else is going on, because your last } doesn't indent while it does for me with auto-indent on.

Could you post the output of ":se" and ":se all" please? maybe there's an option set I don't realize has something to do with this. Maybe something to do with tabs in general or a language specific setting.

Also, could you try under a different user? Maybe it's a setting in your config (.vimrc?) which would make it easier to pin down.
0
 
Nusrat NuriyevAuthor Commented:
:se
--- Options ---
  cindent             hlsearch            syntax=cpp          t_Sb=^[[4%dm
  expandtab         nomodeline            tabstop=4           t_Sf=^[[3%dm
  filetype=cpp        ruler               ttyfast
  helplang=en         scroll=11           ttymouse=sgr
  history=50          shiftwidth=4        viminfo='20,"50
  backspace=indent,eol,start
  comments=sO:* -,mO:*  ,exO:*/,s1:/*,mb:*,ex:*/,://
  fileencoding=utf-8
  fileencodings=ucs-bom,utf-8,latin1
  formatoptions=croql
  guicursor=n-v-c:block,o:hor50,i-ci:hor15,r-cr:hor30,sm:block,a:blinkon0
  omnifunc=ccomplete#Complete
Press ENTER or type command to continue

Open in new window



:se all
--- Options ---
  aleph=224         nofileignorecase    nonumber              synmaxcol=3000
noarabic              filetype=cpp        numberwidth=4       syntax=cpp
  arabicshape       nofkmap               operatorfunc=       tabline=
noallowrevins         foldclose=        nopaste               tabpagemax=10
noaltkeymap           foldcolumn=0        pastetoggle=        tabstop=4
  ambiwidth=single    foldenable          patchexpr=          tagbsearch
noautochdir           foldexpr=0          patchmode=          taglength=0
noautoindent          foldignore=#      nopreserveindent      tagrelative
noautoread            foldlevel=0         previewheight=12    tagstack
noautowrite           foldlevelstart=-1 nopreviewwindow       term=xterm
noautowriteall        foldmethod=manual   printdevice=      notermbidi
  background=light    foldminlines=1      printencoding=      termencoding=
nobackup              foldnestmax=20      printfont=courier noterse
  backupcopy=auto     formatexpr=         printmbcharset=     textauto
  backupext=~         formatprg=          printmbfont=      notextmode
  backupskip=/tmp/*   fsync               printoptions=       textwidth=0
nobinary            nogdefault            prompt              thesaurus=
nobomb                helpheight=20       pumheight=0       notildeop
  bufhidden=          helplang=en         quoteescape=\       timeout
  buflisted         nohidden            noreadonly            timeoutlen=1000
  buftype=            history=50          redrawtime=2000   notitle
  cdpath=,,         nohkmap               regexpengine=0      titlelen=85
  cedit=^F          nohkmapp            norelativenumber      titlestring=
  charconvert=        hlsearch            remap             nottimeout
  cindent           noicon                report=2            ttimeoutlen=-1
  cinoptions=         iconstring=       norevins              ttybuiltin
  cmdheight=1       noignorecase        norightleft           ttyfast
  cmdwinheight=7      iminsert=0          ruler               ttymouse=sgr
  colorcolumn=        imsearch=0          rulerformat=        ttyscroll=999
  columns=89          includeexpr=        scroll=27           ttytype=xterm
nocompatible        noincsearch         noscrollbind          undodir=.
  concealcursor=      indentexpr=         scrolljump=1      noundofile
  conceallevel=0    noinfercase           scrolloff=0         undolevels=1000
  completefunc=     noinsertmode        nosecure              undoreload=10000
noconfirm             isprint=@,161-255   selectmode=         updatecount=200
nocopyindent          joinspaces          shell=/bin/bash     updatetime=4000
  cpoptions=aABceFs   key=                shellcmdflag=-c     verbose=0
  cryptmethod=zip     keymap=             shellquote=         verbosefile=
  cscopepathcomp=0    keymodel=           shelltemp           viminfo='20,"50
  cscopeprg=cscope    keywordprg=man      shellxquote=        virtualedit=
  cscopequickfix=     langmap=            shellxescape=     novisualbell
nocscoperelative      langmenu=         noshiftround          warn
nocscopetag           laststatus=1        shiftwidth=4      noweirdinvert
  cscopetagorder=0  nolazyredraw        noshortname           whichwrap=b,s
nocscopeverbose     nolinebreak           showbreak=          wildchar=<Tab>
nocursorbind          lines=55          noshowcmd             wildcharm=0
nocursorcolumn      nolisp              noshowfulltag         wildignore=
nocursorline        nolist              noshowmatch         nowildignorecase
  debug=              listchars=eol:$     showmode          nowildmenu
nodelcombine          loadplugins         showtabline=1       wildmode=full
  dictionary=         magic               sidescroll=0        wildoptions=
nodiff                makeef=             sidescrolloff=0     window=54
  diffexpr=           makeprg=make      nosmartcase           winheight=1
  diffopt=filler      matchtime=5       nosmartindent       nowinfixheight
nodigraph             maxcombine=2      nosmarttab          nowinfixwidth
  display=            maxfuncdepth=100    softtabstop=0       winminheight=1
  eadirection=both    maxmapdepth=1000  nospell               winminwidth=1
noedcompatible        maxmem=4057224      spellfile=          winwidth=20
  encoding=utf-8      maxmemtot=4057224   spelllang=en        wrap
  endofline           menuitems=25        spellsuggest=best   wrapmargin=0
  equalalways       nomodeline          nosplitbelow          wrapscan
  equalprg=           modelines=5       nosplitright          write
noerrorbells          modifiable          startofline       nowriteany
  esckeys           nomodified            statusline=         writebackup
  eventignore=        more                suffixesadd=        writedelay=0
  expandtab           mouse=              swapfile
noexrc                mousemodel=extend   swapsync=fsync
  fileformat=unix     mousetime=500       switchbuf=
  backspace=indent,eol,start
  backupdir=.,~/tmp,~/
  breakat= ^I!@*-+;:,./?
  casemap=internal,keepascii
  cinkeys=0{,0},0),:,0#,!^F,o,O,e
  cinwords=if,else,while,do,for,switch
  comments=sO:* -,mO:*  ,exO:*/,s1:/*,mb:*,ex:*/,://
  commentstring=/*%s*/
  complete=.,w,b,u,t,i
  completeopt=menu,preview
  define=^\s*#\s*define
  directory=.,~/tmp,/var/tmp,/tmp
  errorfile=errors.err
  errorformat=%*[^"]"%f"%*\D%l: %m,"%f"%*\D%l: %m,%-G%f:%l: (Each undeclared identifier i
s reported only once,%-G%f:%l: for each function it appears in.),%-GIn file included from
 %f:%l:%c:,%-GIn file included from %f:%l:%c\,,%-GIn file included from %f:%l:%c,%-GIn fi
le included from %f:%l,%-G%*[ ]from %f:%l:%c,%-G%*[ ]from %f:%l:,%-G%*[ ]from %f:%l\,,%-G
%*[ ]from %f:%l,%f:%l:%c:%m,%f(%l):%m,%f:%l:%m,"%f"\, line %l%*\D%c%*[^ ] %m,%D%*\a[%*\d]
: Entering directory %*[`']%f',%X%*\a[%*\d]: Leaving directory %*[`']%f',%D%*\a: Entering
 directory %*[`']%f',%X%*\a: Leaving directory %*[`']%f',%DMaking %*\a in %f,%f|%l| %m
  fileencoding=utf-8
  fileencodings=ucs-bom,utf-8,latin1
  fileformats=unix,dos
  fillchars=vert:|,fold:-
  foldmarker={{{,}}}
  foldopen=block,hor,mark,percent,quickfix,search,tag,undo
  foldtext=foldtext()
  formatoptions=croql
  formatlistpat=^\s*\d\+[\]:.)}\t ]\s*
  grepformat=%f:%l:%m,%f:%l%m,%f  %l%m
  grepprg=grep -n $* /dev/null
  guicursor=n-v-c:block,o:hor50,i-ci:hor15,r-cr:hor30,sm:block,a:blinkon0
  helpfile=/usr/share/vim/vim74/doc/help.txt
  highlight=8:SpecialKey,@:NonText,d:Directory,e:ErrorMsg,i:IncSearch,l:Search,m:MoreMsg,
M:ModeMsg,n:LineNr,N:CursorLineNr,r:Question,s:StatusLine,S:StatusLineNC,c:VertSplit,t:Ti
tle,v:Visual,V:VisualNOS,w:WarningMsg,W:WildMenu,f:Folded,F:FoldColumn,A:DiffAdd,C:DiffCh
ange,D:DiffDelete,T:DiffText,>:SignColumn,-:Conceal,B:SpellBad,P:SpellCap,R:SpellRare,L:S
pellLocal,+:Pmenu,=:PmenuSel,x:PmenuSbar,X:PmenuThumb,*:TabLine,#:TabLineSel,_:TabLineFil
  foldopen=block,hor,mark,percent,quickfix,search,tag,undo
  foldtext=foldtext()
  formatoptions=croql
  formatlistpat=^\s*\d\+[\]:.)}\t ]\s*
  grepformat=%f:%l:%m,%f:%l%m,%f  %l%m
  grepprg=grep -n $* /dev/null
  guicursor=n-v-c:block,o:hor50,i-ci:hor15,r-cr:hor30,sm:block,a:blinkon0
  helpfile=/usr/share/vim/vim74/doc/help.txt
  highlight=8:SpecialKey,@:NonText,d:Directory,e:ErrorMsg,i:IncSearch,l:Search,m:MoreMsg,
M:ModeMsg,n:LineNr,N:CursorLineNr,r:Question,s:StatusLine,S:StatusLineNC,c:VertSplit,t:Ti
tle,v:Visual,V:VisualNOS,w:WarningMsg,W:WildMenu,f:Folded,F:FoldColumn,A:DiffAdd,C:DiffCh
ange,D:DiffDelete,T:DiffText,>:SignColumn,-:Conceal,B:SpellBad,P:SpellCap,R:SpellRare,L:S
pellLocal,+:Pmenu,=:PmenuSel,x:PmenuSbar,X:PmenuThumb,*:TabLine,#:TabLineSel,_:TabLineFil
l,!:CursorColumn,.:CursorLine,o:ColorColumn
  include=^\s*#\s*include
  indentkeys=0{,0},:,0#,!^F,o,O,e
  isfname=@,48-57,/,.,-,_,+,,,#,$,%,~,=
  isident=@,48-57,_,192-255
  iskeyword=@,48-57,_,192-255
  lispwords=defun,define,defmacro,set!,lambda,if,case,let,flet,let*,letrec,do,do*,define-
syntax,let-syntax,letrec-syntax,destructuring-bind,defpackage,defparameter,defstruct,deft
ype,defvar,do-all-symbols,do-external-symbols,do-symbols,dolist,dotimes,ecase,etypecase,e
val-when,labels,macrolet,multiple-value-bind,multiple-value-call,multiple-value-prog1,mul
tiple-value-setq,prog1,progv,typecase,unless,unwind-protect,when,with-input-from-string,w
ith-open-file,with-open-stream,with-output-to-string,with-package-iterator,define-conditi
on,handler-bind,handler-case,restart-bind,restart-case,with-simple-restart,store-value,us
e-value,muffle-warning,abort,continue,with-slots,with-slots*,with-accessors,with-accessor
s*,defclass,defmethod,print-unreadable-object
  matchpairs=(:),{:},[:]
  maxmempattern=1000
  mkspellmem=460000,2000,500
  nrformats=octal,hex
  omnifunc=ccomplete#Complete
  paragraphs=IPLPPPQPP TPHPLIPpLpItpplpipbp
  path=.,/usr/include,,
  printexpr=system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fna
me_in) . delete(v:fname_in) + v:shell_error
  printheader=%<%f%h%m%=Page %N
  rightleftcmd=search
  runtimepath=~/.vim,/usr/share/vim/vimfiles,/usr/share/vim/vim74,/usr/share/vim/vimfiles
/after,~/.vim/after
  scrollopt=ver,jump
  sections=SHNHH HUnhsh
  selection=inclusive
  sessionoptions=blank,buffers,curdir,folds,help,options,tabpages,winsize
  shellpipe=2>&1| tee
  shellredir=>%s 2>&1
  shortmess=filnxtToO
  spellcapcheck=[.?!]\_[\])'"^I ]\+
  suffixes=.bak,~,.o,.h,.info,.swp,.obj
  tags=./tags,./TAGS,tags,TAGS
  titleold=Thanks for flying Vim
  viewdir=~/.vim/view
  viewoptions=folds,options,cursor

Open in new window

0
 
Nusrat NuriyevAuthor Commented:
I have not set anything in .vimrc

What am I suppose to do in .vimrc?
0
 
Robert SchuttSoftware EngineerCommented:
In .vimrc you can set options you want as default (amongst others).

it looks like it could be the expandtab setting, which is not set in my vim. So maybe try ":se noet"
0
 
Robert SchuttSoftware EngineerCommented:
But also "cindent" and "syntax=cpp" which seems to indicate indentation specifically for the C language, which I haven't used.
0
 
Robert SchuttSoftware EngineerCommented:
I have an old version with a lot less options:
capture of options
0
 
Nusrat NuriyevAuthor Commented:
Gerwin Jansen, great.
This solved problem.


I have tryied to remove
set expandtab

Open in new window

from .vimrc
But that does not helped.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.