Vim:如何删除空行中的空白?

Vim:如何删除空行中的空白?,vim,vi,Vim,Vi,如何使用vim检测包含空格的空行并删除空格 例如,我使用⎵ 要表示空白,请执行以下操作: def⎵函数foo: ⎵⎵⎵⎵printfoo ⎵⎵ ⎵ 功能1 是否有vim命令将上述代码转换为以下代码 def⎵函数foo: ⎵⎵⎵⎵printfoo 功能1 说明: g — execute the command globally (for all lines) /^\s\+$/ — search lines that contain only whitespaces s/\s\+// — for e

如何使用vim检测包含空格的空行并删除空格

例如,我使用⎵ 要表示空白,请执行以下操作:

def⎵函数foo: ⎵⎵⎵⎵printfoo ⎵⎵ ⎵ 功能1 是否有vim命令将上述代码转换为以下代码

def⎵函数foo: ⎵⎵⎵⎵printfoo 功能1 说明:

g — execute the command globally (for all lines)
/^\s\+$/ — search lines that contain only whitespaces
s/\s\+// — for every found line execute this
           search and replace command:
           search whitespaces and replace with an empty string.
可以简化为

:%s/^\s\+$//

% — execute for all lines
s/^\s\+$// — search and replace command:
             search lines that only have whitespaces
             and replace with an empty string.
说明:

g — execute the command globally (for all lines)
/^\s\+$/ — search lines that contain only whitespaces
s/\s\+// — for every found line execute this
           search and replace command:
           search whitespaces and replace with an empty string.
可以简化为

:%s/^\s\+$//

% — execute for all lines
s/^\s\+$// — search and replace command:
             search lines that only have whitespaces
             and replace with an empty string.

我有一个函数可以解决这个问题并保持光标的位置

if !exists('*StripTrailingWhitespace')
    function! StripTrailingWhitespace()
        if !&binary && &filetype != 'diff'
            let b:win_view = winsaveview()
            silent! keepjumps keeppatterns %s/\s\+$//e
            call winrestview(b:win_view)
        endif
    endfunction
endif
command! Cls call StripTrailingWhitespace()
cnoreabbrev cls Cls
cnoreabbrev StripTrailingSpace Cls
nnoremap <Leader>s :call StripTrailingWhitespace()
您可以使用命令cls或快捷方式s。
实际上,您可以根据需要更改它。

我有一个函数可以解决此问题并保持光标位置

if !exists('*StripTrailingWhitespace')
    function! StripTrailingWhitespace()
        if !&binary && &filetype != 'diff'
            let b:win_view = winsaveview()
            silent! keepjumps keeppatterns %s/\s\+$//e
            call winrestview(b:win_view)
        endif
    endfunction
endif
command! Cls call StripTrailingWhitespace()
cnoreabbrev cls Cls
cnoreabbrev StripTrailingSpace Cls
nnoremap <Leader>s :call StripTrailingWhitespace()
您可以使用命令cls或快捷方式s。
实际上,您可以根据自己的需要更改它。

有没有理由改为:g而不是:%s?@Michail更新,谢谢!我习惯于使用:g,因为我通常会删除带有:g/^$/normal dd:-是否有理由在此处使用:g而不是:%s?@Michail已更新,谢谢!我习惯于使用:g,因为我通常使用:g/^$/normal dd:-