使用.s文件设置文件类型和注释键映射 [英] set filetype and comment key map with .s file

查看:294
本文介绍了使用.s文件设置文件类型和注释键映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在唤醒.s文件.虽然此文件是asm文件,但与.asm文件不同(注释以--开头,而不是#开头).

Im woking with .s file. Although this file is asm file, but it is not same as .asm file (comment begins by --, not #).

因此,当我打开.s文件时,它显示为asm文件.如何更改文件类型以正确显示(--符号后为蓝色)?

So when I open a .s file, it shows as asm file. How can I change filetype to display correctly (blue color after -- sign)?

而且,我已经将vimrc中的F1映射为如下注释:

And also, I have mapped F1 in vimrc to comment as below:

if (@% =~ "\.s") "if file is .s
   map <F1> :<ESC>0i-- <ESC>j
endif

此方法适用于其他文件,但不适用于.s文件.注释符号为#,因为我映射的F1以前是#.

This method works well with other files, but not with .s file. The comment sign is # because I map F1 is # previously.

你能帮我吗?

非常感谢您

推荐答案

站在巨人的肩膀上

我会高度推荐评论插件,例如评论(其中我使用),书呆子评论者 tComment ,...,.使用插件没有什么可耻的.尤其是由于评论切换似乎比预期的要复杂得多.

Standing on the shoulders of giants

I would highly recommend commenting plugin like commentary (which I use), Nerd Commenter, EnhCommentify, tComment, ..., etc. There is no shame in using a plugin. Especially since comment toggling is deceptively more complicated than expected.

只需将'commentstring'设置为文件类型的注释样式即可.如果Vim识别出您的文件类型,那么通常已经为您设置好了.参见:h 'commentstring'.

Simply set 'commentstring' to your comment style for your filetype. Most often this is already setup for you if Vim recognizes your filetype. See :h 'commentstring'.

为php更改'commentstring'的示例.将以下内容添加到~/.vim/ftplugin/asm.vim:

Example of changing 'commentstring' for php. Add the following to ~/.vim/ftplugin/asm.vim:

if expand('%:t') =~# '\.s$'
  set commentstring=--\ %s
endif

或者,如果您更喜欢vimrc文件中的所有内容,则:

Or if you rather everything in your vimrc file:

autocmd FileType asm if expand('%:t') =~# '\.s$' | set commentstring=--\ %s | endif

您可以将切换注释命令设置为<f1>,如下所示:

You can set the toggle comment command to <f1> like so:

nmap <f1> <Plug>CommentaryLine

但是注释离主行更近,并且与任何本机映射都没有冲突.

However commentary are much closer the the home row and do not conflict with any native mappings.

注意:我使用评论功能,这可能不适用于其他评论插件.与往常一样,请阅读文档.

Note: I use commentary this may not work for other comment plugins. As always, read the documentation.

您可能需要更改语法文件,以显示以--开头的行作为注释.将以下内容添加到~/.vim/after/syntax/asm.vim:

You may need to change you syntax file to show lines starting with -- as comments. Add the following to ~/.vim/after/syntax/asm.vim:

syntax match asmComment /--\_s.*$/

但是我不或者不能使用插件

有时不能选择使用插件,但是您应该知道,(取消)注释比最初看起来要复杂得多.特别是如果您需要跨多个文件类型执行此操作.诸如commentary.vim之类的插件可让您一次注释多行或使用动作,例如gcip.

最简单的方法.仅评论:

The simplest approach. Comment only:

autocmd FileType asm nnoremap <buffer> <F1> gI-- <ESC>j

现在可以使用map表达式进行一些切换了:

Now for some toggling with an map-expression:

autocmd FileType asm nnoremap <buffer> <expr> <f1> getline('.') =~ '^\s*--\_s' ? "^3"_x+" : "gI-- \<esc>+"

我还创建了一个天真commentary.vim风格注释切换映射.将以下内容放入您的vimrc文件:

I have also created a naive commentary.vim-style comment toggle mappings. Put the following in your vimrc file:

nnoremap gcc :<c-u>.,.+<c-r>=v:count<cr>call <SID>toggleComment()<cr>
nnoremap gc :<c-u>set opfunc=<SID>commentOp<cr>g@
xnoremap gc :call <SID>toggleComment()<cr>

function! s:commentOp(...)
  '[,']call s:toggleComment()
endfunction

function! s:toggleComment() range
  let comment = substitute(get(b:, 'commentstring', &commentstring), '\s*\(%s\)\s*', '%s', '')
  let pattern = '\V' . printf(escape(comment, '\'), '\(\s\{-}\)\s\(\S\.\{-}\)\s\=')
  let replace = '\1\2'
  if getline('.') !~ pattern
    let indent = matchstr(getline('.'), '^\s*')
    let pattern = '^' . indent . '\zs\(\s*\)\(\S.*\)'
    let replace = printf(comment, '\1 \2' . (comment =~ '%s$' ? '' : ' '))
  endif
  for lnum in range(a:firstline, a:lastline)
    call setline(lnum, substitute(getline(lnum), pattern, replace, ''))
  endfor
endfunction

更多信息

:h 'commentstring'
:h :set
:h filetype
:h new-filetype
:h ftdetect
:h ftplugins
:h after-directory
:h :syn-match
:h :autocmd
:h :map-expression
:h :map-local

这篇关于使用.s文件设置文件类型和注释键映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆