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. Codeforces Round #253 (Div. 2) B - Kolya and Tandem Repeat

    本题要考虑字符串本身就存在tandem, 如测试用例 aaaaaaaaabbb 3 输出结果应该是8而不是6,因为字符串本身的tanderm时最长的 故要考虑字符串本身的最大的tanderm和添加k个 ...

  2. Leetcode Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  3. 【BZOJ1002】[FJOI2007]轮状病毒 递推+高精度

    Description 给定n(N<=100),编程计算有多少个不同的n轮状病毒. Input 第一行有1个正整数n. Output 将编程计算出的不同的n轮状病毒数输出 Sample Inpu ...

  4. LightOJ 1245 数学题,找规律

    1.LightOJ 1245   Harmonic Number (II)   数学题 2.总结:看了题解,很严谨,但又确实恶心的题 题意:求n/1+n/2+....+n/n,n<=2^31. ...

  5. C语言工具---Code::Blocks

    Code::Blocks Code::Blocks 是一个开源的全功能的跨平台C/C++集成开发环境. Code::Blocks是开放源码软件.由纯粹的C++语言开发完成,它使用了著名的图形界面库wx ...

  6. My first blog!!!!!

    这是我的第一篇blog,希望以后学的知识都能够记录下来,形成记忆,方便查看和学习.如果有朋友看到我blog中的错误,欢迎批评指正,谢谢~\(≧▽≦)/~啦啦啦 以后学的知识要整理出来,方便自己的同时也 ...

  7. jsp页面价格

    <div class='flo_left'> <h2 class='red'>订货价¥${productEntity.agentsPrice}</h2> <h ...

  8. 学习bootstrap心得

    Bootstrap 使用了一些 HTML5 元素和 CSS 属性.为了让这些正常工作,您需要使用 HTML5 文档类型(Doctype). 因此,请在使用 Bootstrap 项目的开头包含下面的代码 ...

  9. 遍历Map的两种方法(有排序)

    初始化一个map Map<String, String> map = new HashMap<String, String>(); map.put("1", ...

  10. EditText自定义边框

    1.EditText代码如下 (View代替EditText获取焦点): <View android:focusable="true" android:focusableIn ...