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

时间:2019-03-31 19:16:39

标签: vim vi

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

例如,我使用 function func3(str, indexStart, indexEnd) { var result=""; for (var i=Math.max(0, indexStart); i<=indexEnd || i>str.lengths; i++) { result += str.charAt(i); } return result; } console.log(func3("abcd", 1, 10)); console.log(func3("abcd", -1, 10)); console.log(func3("abcd", 2, 2)); console.log(func3("abcd", 7, 10)); 表示空格:

有vim命令将上面的代码转换为下面的代码吗?

def⎵function(foo):
⎵⎵⎵⎵print(foo) 
⎵⎵
⎵
function(1)

2 个答案:

答案 0 :(得分:2)

:g/^\s\+$/s/\s\+//

说明:

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.

答案 1 :(得分:2)

我有一个可以解决此问题并保持光标位置的功能

combinations[1] // (1,4)
combinations[1][1] // 4 

您可以使用命令combinations[1] // '[(1, 4)]' 或快捷方式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() 。 实际上,您可以根据需要进行更改。