vim - Simple commands to remove unwanted whitespace
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的更多相关文章
- Asp.Net Remove Unwanted Headers
原文:http://blogs.msdn.com/b/varunm/archive/2013/04/23/remove-unwanted-http-response-headers.aspx 原文:h ...
- 大神的vim配置
大神的vim配置,O(∩_∩)O spf13-vim : Steve Francia's Vim Distribution __ _ _____ _ ___ _ __ / _/ |___ / __ _ ...
- vim php代码规范
vim 代码规范工具php-cs-fixer.phar (參考https://github.com/FriendsOfPHP/PHP-CS-Fixer) INSTALL curl http://get ...
- centos7安装YouCompleteMe,vim打造成C++的IDE
一.安装python3 1.安装编译工具 yum -y groupinstall "Development tools" yum -y install zlib-devel bzi ...
- vim : 依赖: vim-common (= 2:7.3.429-2ubuntu2.1) 但是
Ubuntu 12.10 安装vim出错[日期:2013-01-18] 来源:Linux社区 作者:Cubernet [字体:大 中 小] 在Ubuntu 12.10中安装vim时出现了如下提示:正在 ...
- Ubuntu 安装vim出错
在Ubuntu 12.10中安装vim时出现了如下提示: www.linuxidc.com @linuxidc:/etc/apt$ sudo apt-get install vim正在读取软件包列表. ...
- 从源码Build vim以及打包.deb
How to build vim 1. Build步骤 git clone --depth https://github.com/vim/vim.git # download the source c ...
- 3、树莓派的配置:改静态IP、连接ssh、安装中文字体、安装谷歌输入法、增加USB电流、修改触摸屏分辨率、扩展sd卡空间、修复vi和vim乱码问题、安装配置远程桌面vnc
本博文仅作本人操作过程的记录,留作备忘.自强不息 QQ1222698 1.连接上HDMI线,插上触摸屏,插上键盘,鼠标,网线,启动.系统正常启动,但是一直闪烁,不停的黑屏,是由于触摸屏的usb口供电不 ...
- 0. VIM 系列 - 源码升级最新版本vim
卸载原来的vim: $ sudo apt-get remove --purge vim $ suso apt-get clean 下载最新版本源码: $ git clone https://githu ...
随机推荐
- bzoj 1217 [HNOI2003]消防局的设立 Label:图论
题目描述 2020年,人类在火星上建立了一个庞大的基地群,总共有n个基地.起初为了节约材料,人类只修建了n-1条道路来连接这些基地,并且每两个基地都能够通过道路到达,所以所有的基地形成了一个巨大的树状 ...
- 【BZOJ】2876: [Noi2012]骑行川藏
题意 给出\(s_i, k_i, v_i', E\),满足\(\sum_{i=1}^{n} k_i s_i ( v_i - v_i' )^2 \le E, v_i > v_i'\),最小化$ \ ...
- 【POJ】3071 Football
http://poj.org/problem?id=3071 题意:2^n支队伍进行淘汰赛,每一轮都是第一个与第二个,第三个与第四个比赛,给出了这些人之间的胜率,赢了的进入下一轮,相对位置不变.一共n ...
- 【Oracle】同义词问题
优点: 1.节省数据库空间,多用户可以操作同一张表: 2.扩展的数据库的使用范围,能够在不同的数据库用户之间实现无缝交互: 3.利用Database Link.创建同义词可以实现不同数据库服务器之间的 ...
- JavaScript事件对象【转】
一. 事件对象 事件处理三部分组成:对象.事件处理函数=函数.例如:单击文档任意处. 1.鼠标按钮 非 IE(W3C)中的 button 属性 0 表示主鼠标按钮(常规一般是鼠标左键) 1 表示中间的 ...
- Oracle connect by 树查询之二
先用scott用户下的emp表做实验.emp表有个字段,一个是empno(员工编号),另一个是mgr(上级经理编号)下面是表中所有数据 1 select * from emp start with e ...
- java poi ppt操作示例
poi3.9版本,官网 http://poi.apache.org/slideshow/how-to-shapes.html import java.awt.Color; import java.io ...
- hdu1240 bfs 水题
原题链接 思路:水题,直接搜 #include "map" #include "queue" #include "math.h" #incl ...
- android-ProgressBar
制定ProgressBar显示风格 * 参考系统自带的进度条 * ProgressBar分类 * 可以精确显示进度(可以显示刻度和百分比) * 不可以精确显示进度 * 标题上ProgressBar的设 ...
- Struts的文件下载功能实现代码
Action: package com.tengfeiyang.action; import java.io.File; import java.io.FileInputStream; import ...