vim 稍微高級(jí)一些的設(shè)置

最近需要在linux系統(tǒng)下編輯fortran的代碼呕缭,但是系統(tǒng)自帶的編輯器vim本身對(duì)fortran的支持不夠強(qiáng)大嘱巾,但是好在vim本身是一個(gè)擴(kuò)展性極強(qiáng)的編輯器齐邦,在CSDN上查找到一篇搬運(yùn)的教程演侯,但是本身沒(méi)有寫(xiě)全炕吸,原鏈接又時(shí)效了伐憾,于是倒騰了一上午,結(jié)果如下:

實(shí)現(xiàn)了vim對(duì)fortran語(yǔ)法高亮的優(yōu)化

image.png

自動(dòng)識(shí)別自由格式赫模,自動(dòng)折疊特定模塊

image.png

編輯模式下树肃,按F7自動(dòng)補(bǔ)全語(yǔ)法結(jié)束語(yǔ)句。

image.png

敲F7
image.png

PS:本人額外添加了許多類(lèi)似的模塊結(jié)束語(yǔ)句補(bǔ)全功能瀑罗。

實(shí)現(xiàn)方法:

1.打開(kāi)vim對(duì)Python的支持

這一步最新版vim已經(jīng)不需要執(zhí)行了胸嘴,因?yàn)榇蠹叶伎缛雙y3的時(shí)代了莉钙,并且這個(gè)教程內(nèi)使用到的py2腳本我也已經(jīng)優(yōu)化成py3格式了~~~

這里說(shuō)的python是指python2+,所以需要用apt從新安裝
輸入sudo apt-get install vim-nox-py2 可以安裝vim的特定Python支持版本,之后需要切換版本則可以使用sudo update-alternatives --config vim來(lái)切換筛谚。
這時(shí)輸入vim --version | grep python 應(yīng)該可以看見(jiàn):

image.png

2.打開(kāi)~/.vimrc文件

添加如下內(nèi)容

"這是建立的vim配置文件磁玉,需要移植請(qǐng)拷貝致~.vimrc處。
" 這行定義了文字編碼規(guī)則序列
set encoding=utf-8
set fileencodings=ucs-bom,utf-8,utf-16,gbk,big5,gb18030,shift-jis,euc-jp,euc-kr,latin1
set fileencoding=utf-8
"fortran code improve set
let s:extfname=expand("%:e")
if s:extfname==?"f90"
        let fortran_free_source=1
        unlet! fortran_fixed_source
else
        let fortran_fixed_source=1
        unlet! fortran_free_source
endif
let fortran_more_precise=1
let fortran_do_enddo=1
"去掉固定格式每行開(kāi)頭的紅色區(qū)域
let fortran_have_tabs=1
"允許折疊
let fortran_fold=1
let fortran_fold_conditionals=1
"折疊方式
set foldmethod=syntax
"加載第三方插件
filetype plugin on

3.在~/.vim/after/indent文件夾內(nèi)添加如下文件

文件命名為fortran.vim
內(nèi)容為:

" Vim indent file
" Language:     Fortran 95, Fortran 90 (free source form)
" Description:  Indentation rules for continued statements and preprocessor
"               instructions
"               Indentation rules for subroutine, function and forall
"               statements
" Installation: Place this script in the $HOME/.vim/after/indent/ directory
"               and use it with Vim 7.1 and Ajit J. Thakkar's Vim scripts
"               for Fortran (http://www.unb.ca/chem/ajit/)
" Maintainer:   S閎astien Burton <sebastien.burton@gmail.com>
" License:      Public domain
" Version:      0.4
" Last Change:  2011 May 25

" Modified indentation rules are used if the Fortran source code is free
" source form, else nothing is done
if (b:fortran_fixed_source != 1)
    setlocal indentexpr=SebuFortranGetFreeIndent()
    setlocal indentkeys+==~subroutine,=~function,=~forall
    setlocal indentkeys+==~endsubroutine,=~endfunction,=~endforall
    " Only define the functions once
    if exists("*SebuFortranGetFreeIndent")
        finish
    endif
else
    finish
endif


" SebuFortranGetFreeIndent() is modified FortranGetFreeIndent():
" Returns the indentation of the current line
function SebuFortranGetFreeIndent()
    " No indentation for preprocessor instructions
    if getline(v:lnum) =~ '^\s*#'
        return 0
    endif
    " Previous non-blank non-preprocessor line
    let lnum = SebuPrevNonBlankNonCPP(v:lnum-1)
    " No indentation at the top of the file
    if lnum == 0
        return 0
    endif
    " Original indentation rules
    let ind = FortranGetIndent(lnum)
    " Continued statement indentation rule
    " Truth table (kind of)
    " Symbol '&'            |   Result
    " No            0   0   |   0   No change
    " Appearing     0   1   |   1   Indent
    " Disappering   1   0   |   -1  Unindent
    " Continued     1   1   |   0   No change
    let result = -SebuIsFortranContStat(lnum-1)+SebuIsFortranContStat(lnum)
    " One shiftwidth indentation for continued statements
    let ind += result*&sw
    " One shiftwidth indentation for subroutine, function and forall's bodies
    let line = getline(lnum)
    if line =~? '^\s*\(\(recursive\s*\)\=pure\|elemental\)\=\s*subroutine\|program\|module'
                \ || line =~? '^\s*\(\(recursive\s*\)\=pure\|elemental\)\=\s*'
                \ . '\(\(integer\|real\|complex\|logical\|character\|type\)'
                \ . '\((\S\+)\)\=\)\=\s*function'
                \ || line =~? '^\s*\(forall\)'
        let ind += &sw
    endif
    if getline(v:lnum) =~? '^\s*end\s*\(subroutine\|function\|forall\|program\|module\)'
        let ind -= &sw
    endif
    " You shouldn't use variable names begining with 'puresubroutine',
    " 'function', 'endforall', etc. as these would make the indentation
    " collapse: it's easier to pay attention than to implement the exceptions
    return ind
endfunction

" SebuPrevNonBlankNonCPP(lnum) is modified prevnonblank(lnum):
" Returns the line number of the first line at or above 'lnum' that is
" neither blank nor preprocessor instruction.
function SebuPrevNonBlankNonCPP(lnum)
    let lnum = prevnonblank(a:lnum)
    while getline(lnum) =~ '^#'
        let lnum = prevnonblank(lnum-1)
    endwhile
    return lnum
endfunction

" SebuIsFortranContStat(lnum):
" Returns 1 if the 'lnum' statement ends with the Fortran continue mark '&'
" and 0 else.
function SebuIsFortranContStat(lnum)
    let line = getline(a:lnum)
    return substitute(line,'!.*$','','') =~ '&\s*$'
endfunction

4.在~/.vim/ftplugin文件夾下新建

文件名問(wèn):fortran_codecomplete.vim
(文件本身原本使用的python2語(yǔ)法驾讲,我已經(jīng)優(yōu)化成py3語(yǔ)法了蚊伞,并且添加了對(duì)program和module塊的補(bǔ)完操作)
內(nèi)容為:

" File: fortran_codecomplete.vim
" Author: Michael Goerz (goerz AT physik DOT fu MINUS berlin DOT de)
" Version: 0.9
" Copyright: Copyright (C) 2008 Michael Goerz
"    This program is free software: you can redistribute it and/or modify
"    it under the terms of the GNU General Public License as published by
"    the Free Software Foundation, either version 3 of the License, or
"    (at your option) any later version.
"
"    This program is distributed in the hope that it will be useful,
"    but WITHOUT ANY WARRANTY; without even the implied warranty of
"    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
"    GNU General Public License for more details.
"
" Description: 
"    This maps the <F7> key to complete Fortran 90 constructs"

" Installation:
"    Copy this file into your ftplugin directory. 


python3 << EOF
import re
import vim

class SyntaxElement:
    def __init__(self, pattern, closingline):
        self.pattern = pattern
        self.closingline = closingline
    def match(self, line): 
        """ Return (indent, closingline) or (None, None)"""
        match = self.pattern.search(line)
        if match:
            indentpattern = re.compile(r'^\s*')
            variablepattern = re.compile(r'\$\{(?P<varname>[a-zA-Z0-9_]*)\}')
            indent = indentpattern.search(line).group(0)
            closingline = self.closingline
            # expand variables in closingline
            while True:
                variable_match = variablepattern.search(closingline)
                if variable_match:
                    try:
                        replacement = match.group(variable_match.group('varname'))
                    except:
                        print("Group %s is not defined in pattern" % variable_match.group('varname'))
                        replacement = variable_match.group('varname')
                    try:
                        closingline = closingline.replace(variable_match.group(0), replacement)
                    except TypeError:
                        if replacement is None:
                            replacement = ""
                        closingline = closingline.replace(variable_match.group(0), str(replacement))
                else:
                    break
        else:
            return (None, None)
        closingline = closingline.rstrip()
        return (indent, closingline)
            
        
def fortran_complete():

    syntax_elements = [
        SyntaxElement(re.compile(r'^\s*program\s+(?P<name>[a-zA-Z0-9_]+)'),
                      'end program ${name}' ),
        SyntaxElement(re.compile(r'^\s*type\s+(?P<name>[a-zA-Z0-9_]+)'),
                      'end type ${name}' ),
        SyntaxElement(re.compile(r'^\s*interface\s+'),
                      'end interface' ),
        SyntaxElement(re.compile(r'^\s*module\s+(?P<name>[a-zA-Z0-9_]+)'),
                      'end module ${name}' ),
        SyntaxElement(re.compile(r'^\s*subroutine\s+(?P<name>[a-zA-Z0-9_]+)'),
                      'end subroutine ${name}' ),
        SyntaxElement(re.compile(r'^\s*\w*\s*function\s+(?P<name>[a-zA-Z0-9_]+)'),
                      'end function ${name}' ),
        SyntaxElement(re.compile(r'^\s*((?P<name>([a-zA-Z0-9_]+))\s*:)?\s*if \s*\(.*\) \s*then'),
                      'end if ${name}' ),
        SyntaxElement(re.compile(r'^\s*((?P<name>([a-zA-Z0-9_]+))\s*:)?\s*do'),
                      'end do ${name}' ),
        SyntaxElement(re.compile(r'^\s*select case '),
                      'end select' )
    ]

    cb = vim.current.buffer
    line = vim.current.window.cursor[0] - 1
    cline = cb[line]

    for syntax_element in syntax_elements:
        (indent, closingline) = syntax_element.match(cline)
        if closingline is not None:
            vim.command('s/$/\x0D\x0D/') # insert two lines
            shiftwidth = int(vim.eval("&shiftwidth"))
            cb[line+1] = indent + (" " * shiftwidth)
            cb[line+2] = indent + closingline 
            vim.current.window.cursor = (line+2, 1)
EOF

nmap <F7> :python3 fortran_complete()<cr>A
imap <F7> ^[:python3 fortran_complete()<cr>A

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市吮铭,隨后出現(xiàn)的幾起案子时迫,更是在濱河造成了極大的恐慌,老刑警劉巖谓晌,帶你破解...
    沈念sama閱讀 219,270評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件掠拳,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡纸肉,警方通過(guò)查閱死者的電腦和手機(jī)溺欧,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,489評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)柏肪,“玉大人姐刁,你說(shuō)我怎么就攤上這事》澄叮” “怎么了聂使?”我有些...
    開(kāi)封第一講書(shū)人閱讀 165,630評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)谬俄。 經(jīng)常有香客問(wèn)我柏靶,道長(zhǎng),這世上最難降的妖魔是什么溃论? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,906評(píng)論 1 295
  • 正文 為了忘掉前任屎蜓,我火速辦了婚禮,結(jié)果婚禮上蔬芥,老公的妹妹穿的比我還像新娘梆靖。我一直安慰自己控汉,他們只是感情好笔诵,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,928評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著姑子,像睡著了一般乎婿。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上街佑,一...
    開(kāi)封第一講書(shū)人閱讀 51,718評(píng)論 1 305
  • 那天谢翎,我揣著相機(jī)與錄音捍靠,去河邊找鬼。 笑死森逮,一個(gè)胖子當(dāng)著我的面吹牛榨婆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播褒侧,決...
    沈念sama閱讀 40,442評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼良风,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了闷供?” 一聲冷哼從身側(cè)響起烟央,我...
    開(kāi)封第一講書(shū)人閱讀 39,345評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎歪脏,沒(méi)想到半個(gè)月后疑俭,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,802評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡婿失,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,984評(píng)論 3 337
  • 正文 我和宋清朗相戀三年钞艇,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片豪硅。...
    茶點(diǎn)故事閱讀 40,117評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡香璃,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出舟误,到底是詐尸還是另有隱情葡秒,我是刑警寧澤,帶...
    沈念sama閱讀 35,810評(píng)論 5 346
  • 正文 年R本政府宣布嵌溢,位于F島的核電站眯牧,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏赖草。R本人自食惡果不足惜学少,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,462評(píng)論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望秧骑。 院中可真熱鬧版确,春花似錦、人聲如沸乎折。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,011評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)骂澄。三九已至吓蘑,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背磨镶。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,139評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工溃蔫, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人琳猫。 一個(gè)月前我還...
    沈念sama閱讀 48,377評(píng)論 3 373
  • 正文 我出身青樓伟叛,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親脐嫂。 傳聞我的和親對(duì)象是個(gè)殘疾皇子痪伦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,060評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容