Reemplazar Tabulación con Espacios en Vim


Me gustaría convertir tab a espacios en gVim. He añadido la siguiente línea a mi _vimrc:

set tabstop=2

Funciona para detenerse en dos espacios, pero todavía parece que se inserta una tecla tab (traté de usar la tecla h para contar espacios después).

No estoy seguro de qué debo hacer para que gVim convierta pestañas en espacios?

 309
vim
Author: Matthias Braun, 2009-01-09

10 answers

IIRC, algo así como:

set tabstop=2 shiftwidth=2 expandtab

Debería hacer el truco. Si ya tiene pestañas, luego siga con un buen RE global para reemplazarlas con espacios dobles.

 332
Author: D.Shawley,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-09 03:19:02

Una vez que tenga expandtab activado según las otras respuestas, la forma extremadamente conveniente de convertir archivos existentes de acuerdo con su nueva configuración es:

:retab

Funcionará en el búfer actual.

 729
Author: ʞɔıu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-08-29 12:32:54

Intenta

set expandtab

Para soft tabs.

Para arreglar pestañas preexistentes:

:%s/\t/  /g

Usé dos espacios ya que ya configuraste tu tabstop en 2 espacios.

 95
Author: Hank Gay,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-01-09 03:30:26

Agregue las siguientes líneas a su .vimrc

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

Abra un archivo en vim y presione F2 Las pestañas se convertirán en 4 espacios y el archivo se guardará automáticamente.

 38
Author: anish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2010-01-20 02:42:28

Esto funcionó para mí:

Puedes ver las pestañas primero haciendo esto:

:set list

Luego, para hacer posible reemplazar las pestañas, haga lo siguiente:

:set expandtab

Entonces

:retab

Ahora todas las pestañas han sido reemplazadas con espacios a continuación, puede volver a la visualización normal de esta manera:

:set nolist
 38
Author: serup,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-10-14 10:11:43

gg=G reindent todo el archivo y elimina la mayoría, si no todas, las pestañas que obtengo en los archivos de los compañeros de trabajo.

 35
Author: Jake Sellers,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-09-05 21:42:50

Si desea mantener su \t igual a 8 espacios, considere la opción:

   set softtabstop=2 tabstop=8 shiftwidth=2

Esto le dará dos espacios por <TAB> presione, pero el \t real en su código todavía se verá como 8 caracteres.

 13
Author: pk.,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-02-12 14:09:36

Primero busque pestañas en su archivo: / ^I : set expandtab : retab

Funcionará.

 3
Author: Shekar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-03-28 06:18:48

Este artículo tiene un excelente script vimrc para manejar pestañas+espacios, y convertir entre ellos.

Se proporcionan estos comandos:

Space2Tab Convierte espacios en pestañas, solo en sangrías.

Tab2Space Convierte pestañas en espacios, solo en sangrías.

RetabIndent Ejecuta Space2Tab (si 'expandtab' está establecido), o Tab2Space (de lo contrario).

Cada comando acepta un argumento que especifica el número de espacios en una columna de tabulación. De forma predeterminada, se utiliza la configuración' tabstop'.

Fuente: http://vim.wikia.com/wiki/Super_retab#Script

" 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>)

Esto me ayudó un poco más que las respuestas aquí lo hicieron cuando fui por primera vez en busca de una solución.

 1
Author: Adam Eberlin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-08-30 16:14:45

expand es una utilidad unix para convertir pestañas en espacios. Si no quieres set nada en vim, puedes usar un comando de shell de vim:

:!% expand -t8
 1
Author: qwr,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-03-25 02:12:24