vim缩进参考线

左手 posted @ 2014年10月20日 22:09 in Vim with tags vim参考线 vim reference line vim缩进参考 vim indent reference , 5443 阅读

编辑缩进嵌套的文件时想找到对应的层级比较困难,写了一个函数,使用cc选项设定一条辅助线,标识到指定的缩进层级。代码如下:

" --------------------------------------------------
" [参考线切换] {{{
" --------------------------------------------------
fu! ReferenceLine(t)
	if exists('w:ccnum')
		let ccnum=w:ccnum
	elsei exists('b:ccnum')
		let ccnum=b:ccnum
	else
		let ccnum=0
	en
	let oldcc=ccnum
	" let ccc=&cc
	" ec oldcc
	let ccc=','.&cc.','
	" add/sub
	if a:t=='add' || a:t=='sub'
		" check old cc
		if match(ccc, ','.oldcc.',')<0
			let oldcc=0
			let ccnum=0
		en
		" step
		let csw=&sw
		if a:t=='add'
			let ccnum=ccnum + csw
		elsei a:t=='sub'
			let ccnum=ccnum - csw
			if ccnum < 0 | let ccnum=0 | en
		en
		if oldcc > 0 | let ccc=substitute(ccc, ','.oldcc.',', ',', '') | en
		let ccc=ccc.ccnum
		" ec ccc
		" ec ccnum
		let ccc=substitute(ccc, '^0,\|,0,\|,0$', ',', 'g')
		let ccc=substitute(ccc, '^,\+\|,\+$', '', 'g')
		" ec ccc
		let w:ccnum=ccnum
		let b:ccnum=ccnum
		exec "setl cc=".ccc
	" del
	elsei a:t=='del'
		let ccc=substitute(ccc, ','.oldcc.',', ',', '')
		let ccc=substitute(ccc, '^,\+\|,\+$', '', 'g')
		" ec ccc
		let w:ccnum=0
		let b:ccnum=0
		exec "setl cc=".ccc
	en
endf
nn <silent> <A-u> :call ReferenceLine('sub')<CR>
nn <silent> <A-o> :call ReferenceLine('add')<CR>
" }}}

使用方法:
Alt+o 增加参考线、Alt+u 减少参考线,最后两行是键映射,可以按照需求自行修改。
设定参考线后,如果想要设定cc做列宽参照,请使用set cc+=<num>来设定。

效果如下:左侧的是缩进辅助线,右侧的是手动设置的列宽参照。

Avatar_small
Iven 说:
2014年10月24日 21:49

用 IndentLine 不就好了……

Avatar_small
依云 说:
2014年10月27日 18:24

为什么不用 'cursorcolumn'?

Avatar_small
左手 说:
2014年11月06日 09:53

@依云: 习惯用简写,全称写得太累

Avatar_small
左手 说:
2014年11月06日 09:54

@Iven: 有点不方便,竖线太多滚到下一屏后有时候找不到对齐点。

Avatar_small
依云 说:
2014年11月06日 11:46

@左手: 我装了 neocomplete,有补全的。简写容易忘,不好理解,所以我在配置文件里都是写全称的。

Avatar_small
Best WiFi names 说:
2022年8月09日 11:27

Finding the best WiFi names for their new connections or routers is that they want some cool or funny names that make them feel nice and at the same time when your friends, family, or someone comes over to your place don’t you think a nice name would make the charm of the house as well. Best WiFi names Moreover, you can not just add cool names to your Network SSID but have some Puns or Jokes intended through the WiFi name that can bring a nice laugh out of anyone who comes across your connection in their WiFi settings finder. Also sometimes you want to hide WiFi SSID for security reasons.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter