用vim中的空格替換製表符

我想將tab轉換爲gVim中的空格。 我_vimrc下行添加到個人_vimrcvim

set tabstop=2

它能夠在兩個空格處中止,但它仍然看起來像是插入了一個tab鍵(我嘗試使用h鍵來計算空格)。 spa

我不知道如何將gVim轉換爲空格? rest


#1樓

本文有一個很好的vimrc腳本,用於處理選項卡+空格,並在它們之間進行轉換。 code

提供如下命令: ip

Space2Tab僅在縮進中將空格轉換爲製表符。 get

Tab2Space僅將選項卡轉換爲空格。 string

RetabIndent執行Space2Tab(若是設置了'expandtab')或Tab2Space(不然)。 it

每一個命令都接受一個參數,該參數指定選項卡列中的空格數。 默認狀況下,使用'tabstop'設置。 io

資料來源: http//vim.wikia.com/wiki/Super_retab#Script function

" Return indent (all whitespace at start of a line), converted from
" tabs to spaces if what = 1, or from spaces to tabs otherwise.
" When converting to tabs, result has no redundant spaces.
function! Indenting(indent, what, cols)
  let spccol = repeat(' ', a:cols)
  let result = substitute(a:indent, spccol, '\t', 'g')
  let result = substitute(result, ' \+\ze\t', '', 'g')
  if a:what == 1
    let result = substitute(result, '\t', spccol, 'g')
  endif
  return result
endfunction

" Convert whitespace used for indenting (before first non-whitespace).
" what = 0 (convert spaces to tabs), or 1 (convert tabs to spaces).
" cols = string with number of columns per tab, or empty to use 'tabstop'.
" The cursor position is restored, but the cursor will be in a different
" column when the number of characters in the indent of the line is changed.
function! IndentConvert(line1, line2, what, cols)
  let savepos = getpos('.')
  let cols = empty(a:cols) ? &tabstop : a:cols
  execute a:line1 . ',' . a:line2 . 's/^\s\+/\=Indenting(submatch(0), a:what, cols)/e'
  call histdel('search', -1)
  call setpos('.', savepos)
endfunction

command! -nargs=? -range=% Space2Tab call IndentConvert(<line1>,<line2>,0,<q-args>)
command! -nargs=? -range=% Tab2Space call IndentConvert(<line1>,<line2>,1,<q-args>)
command! -nargs=? -range=% RetabIndent call IndentConvert(<line1>,<line2>,&et,<q-args>)

當我第一次尋找解決方案時,這對個人幫助有所幫助。


#2樓

首先在文件中搜索選項卡:/ ^ I:設置expandtab:retab

將工做。


#3樓

gg=G將從新整理整個文件,並刪除大部分(若是不是所有)我從同事那裏獲得的文件。


#4樓

將如下行添加到.vimrc中

set expandtab
set tabstop=4
set shiftwidth=4
map <F2> :retab <CR> :wq! <CR>

在vim中打開文件並按F2鍵。選項卡將轉換爲4個空格,文件將自動保存。


#5樓

這對我有用:

你能夠看到首先這樣作的標籤:

:set list

而後能夠替換製表符而後執行如下操做:

:set expandtab

而後

:retab

如今全部選項卡都已替換爲空格,而後您能夠返回到正常查看,以下所示:

:set nolist
相關文章
相關標籤/搜索