http://vim.wikia.com/wiki/Remove_unwanted_spaces

1. manual command
remove trailing whitespace:
:%s/\s\+$//e
remove heading whitespace:
:%s/^\s\+//e
:%le

2. binding key - press F5 to delete all trailing whitespace
:nnoremap <silent> <F5> :let _s=@/ <Bar> :%s/\s\+$//e <Bar> :let @/=_s <Bar> :nohl <Bar> :unlet _s <CR>

3. Display or remove unwanted whitespace with a script
function ShowSpaces(...)
  let @/='\v(\s+$)|( +\ze\t)'
  let oldhlsearch=&hlsearch
  if !a:0
    let &hlsearch=!&hlsearch
  else
    let &hlsearch=a:1
  end
  return oldhlsearch
endfunction

function TrimSpaces() range
  let oldhlsearch=ShowSpaces(1)
  execute a:firstline.",".a:lastline."substitute ///gec"
  let &hlsearch=oldhlsearch
endfunction

command -bar -nargs=? ShowSpaces call ShowSpaces(<args>)
command -bar -nargs=0 -range=% TrimSpaces <line1>,<line2>call TrimSpaces()
nnoremap <F12>     :ShowSpaces 1<CR>
nnoremap <S-F12>   m`:TrimSpaces<CR>``
vnoremap <S-F12>   :TrimSpaces<CR>

4. Automatically removing all trailing whitespace before saving.
modify vimrc file:
autocmd BufWritePre * %s/\s\+$//e
However, this is a very dangerous autocmd to have as it will always strip trailing whitespace from every file a user saves.
Sometimes, trailing whitespace is desired, or even essential in a file so be careful when implementing this autocmd.

One method to mitigate this issue in a .vimrc file, where trailing whitespace matters, is to change how .vimrc prepends wrapped lines.
For example, add the following into the .vimrc:
set wrap
set linebreak
" note trailing space at end of next line
set showbreak=>\ \ \
Now when saving the .vimrc it will use ">  \" instead of ">   " to prepend wrapped lines.

A user can also specify a particular filetype in an autocmd so that only that filetype will be changed when saving.
The following only changes files with the extension .pl:
autocmd BufWritePre *.pl %s/\s\+$//e
Additionally, a FileType autocommand can be used to restrict the autocmd to certain file types only.
autocmd FileType c,cpp,java,php autocmd BufWritePre <buffer> %s/\s\+$//e

vim - Simple commands to remove unwanted whitespace的更多相关文章

  1. Asp.Net Remove Unwanted Headers

    原文:http://blogs.msdn.com/b/varunm/archive/2013/04/23/remove-unwanted-http-response-headers.aspx 原文:h ...

  2. 大神的vim配置

    大神的vim配置,O(∩_∩)O spf13-vim : Steve Francia's Vim Distribution __ _ _____ _ ___ _ __ / _/ |___ / __ _ ...

  3. vim php代码规范

    vim 代码规范工具php-cs-fixer.phar (參考https://github.com/FriendsOfPHP/PHP-CS-Fixer) INSTALL curl http://get ...

  4. centos7安装YouCompleteMe,vim打造成C++的IDE

    一.安装python3 1.安装编译工具 yum -y groupinstall "Development tools" yum -y install zlib-devel bzi ...

  5. vim : 依赖: vim-common (= 2:7.3.429-2ubuntu2.1) 但是

    Ubuntu 12.10 安装vim出错[日期:2013-01-18] 来源:Linux社区 作者:Cubernet [字体:大 中 小] 在Ubuntu 12.10中安装vim时出现了如下提示:正在 ...

  6. Ubuntu 安装vim出错

    在Ubuntu 12.10中安装vim时出现了如下提示: www.linuxidc.com @linuxidc:/etc/apt$ sudo apt-get install vim正在读取软件包列表. ...

  7. 从源码Build vim以及打包.deb

    How to build vim 1. Build步骤 git clone --depth https://github.com/vim/vim.git # download the source c ...

  8. 3、树莓派的配置:改静态IP、连接ssh、安装中文字体、安装谷歌输入法、增加USB电流、修改触摸屏分辨率、扩展sd卡空间、修复vi和vim乱码问题、安装配置远程桌面vnc

    本博文仅作本人操作过程的记录,留作备忘.自强不息 QQ1222698 1.连接上HDMI线,插上触摸屏,插上键盘,鼠标,网线,启动.系统正常启动,但是一直闪烁,不停的黑屏,是由于触摸屏的usb口供电不 ...

  9. 0. VIM 系列 - 源码升级最新版本vim

    卸载原来的vim: $ sudo apt-get remove --purge vim $ suso apt-get clean 下载最新版本源码: $ git clone https://githu ...

随机推荐

  1. hbase regionserver挂掉的问题

    之前regionserver老挂,今天终于找到原因了. 是程序中一个函数不停的构造HTable.导致消耗内存.导致gc full. 这篇文章中得到提示:http://www.cnblogs.com/p ...

  2. 【HDU】1693 Eat the Trees

    http://acm.hdu.edu.cn/showproblem.php?pid=1693 题意:n×m的棋盘求简单回路(可以多条)覆盖整个棋盘的方案,障碍格不许摆放.(n,m<=11) #i ...

  3. POJ 1127 Jack Straws(计算几何)

    题目链接 抄的模版,居然1Y了.就是简单的线段相交+并查集. #include <iostream> #include <cstring> #include <cstdi ...

  4. 使用SDWebImage下载图片,sharedDownloader方法下载成功,new 方法下载失败

    一,经历 1.使用 new 方法创建下载对象时,下载图片总是失败,而且不会执行成功或失败后的回调. 2.参考别人的代码,用的是sharedDownloader来创建下载对象,可以顺利下载图片. 3.看 ...

  5. 开年钜献:华清远见金牌讲师名家大讲堂(Android开发篇)

        华清远见作为嵌入式培训领导品牌,嵌入式就业课程已成为业内公认的专业人才培养体系!华清远见致力于让更多嵌入式技术爱好者及在校大学生获得一线嵌入式系统开发关键技术应用的经验,于2009年始开办名家 ...

  6. C语言-结构体

    #include<stdio.h> struct stu //定义结构体类型 { int num; char *name; char *sex; float score; } boy[]= ...

  7. Node.js 手册查询-4-Express 方法

    express 标签(空格分隔): node.js express [TOC] 安装: 新版本中命令行工具分家了 npm install -g express //安装 express 然后 npm ...

  8. 手机访问pc网站自动跳转手机端网站代码

    <SCRIPT LANGUAGE="JavaScript">function mobile_device_detect(url){        var thisOS= ...

  9. First day in 阿里

    周五上午10点半的飞机,为了便宜选了CA的空客320的飞机,结果体验很差.飞机涂了层风骚的粉紫色,机内较旧,也很小,经过所谓的头等舱简直惨不忍睹.对比起去年飞去北京乘的波音真是没法比,波音上每个人都有 ...

  10. python 之ConfigParser

    ConfigParser 简介ConfigParser是用来操作配置文件的模块. 说明:[**]为配置文件的section,基本格式为 [section] key = valueeg: [db] db ...