FANDOM


(Language fascism. Unpersonaized. Proper code tags. Using <Leader>.)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(3 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Tips related to the Clojure language
+
{{TipNew
+
|id=1631
== How to Comment / Uncomment in Clojure: ==
+
|previous=1630
+
|next=1632
The comment leader for Clojure is the <tt>;</tt> character. Here's a snippet you can use to comment and uncomment lines in Clojure files
+
|created=2009
  +
|complexity=basic
  +
|author=
  +
|version=7.0
  +
|subpage=/200909
  +
|category1=
  +
|category2=
  +
}}
  +
Tips relating to the [[wikipedia:Clojure|Clojure]] language will be given here.
   
  +
==Commenting and uncommenting==
  +
The comment leader for Clojure is the <code>;</code> character. This snippet allows easy commenting and uncommenting of lines.
 
<pre>
 
<pre>
  +
map <Leader>. :call ClojureCommentUncomment()<CR>
 
function! ClojureCommentUncomment()
 
function! ClojureCommentUncomment()
"search for the ; character at the beginning of the line
+
let search_saved = @/
let search_saved = @/ " save the contents of the search register
+
if getline('.') =~ '^;'
if getline(".") =~ '\;'
+
s/^;// " remove ';' at beginning of line
s/^\;// " remove the ; at the beginning of the line
 
 
else
 
else
s/^/\;/ "add a ; at the beginning of the line
+
s/^/;/ " insert ';' at beginning of line
 
endif
 
endif
let @/ = search_saved " restore the contents of the search register
+
let @/ = search_saved
 
endfunction
 
endfunction
map <Leader>. :call ClojureCommentUncomment()<CR>
 
 
</pre>
 
</pre>
   
Typing <tt><Leader>.</tt> {{help|mapleader}} will now add a <tt>;</tt> at the beginning of the current line, if there isn't already one, or remove it, if there is one. You could make this more restrictive by only adding the mapping for files of the Clojure filetype, or by adding it to <tt>~/.vim/after/ftplugin/clojure.vim</tt>.
+
Typing <code>\.</code> (assuming the default backslash leader) will remove <code>;</code> from the beginning of the current line if it is present (uncomment), or will insert it otherwise (comment). You could make this more restrictive by only adding the mapping for files of the Clojure filetype, or by adding it to <code>~/.vim/after/ftplugin/clojure.vim</code>.
   
 
==References==
 
==References==
* {{help|:map}}
+
*{{help|filetype}}
* {{help|mapleader}}
+
*{{help|after-directory}}
* {{help|filetype}}
 
* {{help|FileType}}
 
* {{help|after-directory}}
 
   
 
==Comments==
 
==Comments==
  +
{{todo}}
  +
*May need some information similar to [[VimTip1565]] for making Clojure a known filetype.
  +
*The tip used <code>'\;'</code> but the backslash is redundant (I removed it).
  +
*The tip had <code>if getline('.') =~ '\;'</code> but <code>s/^;//</code>. The former matches <code>;</code> anywhere in the line, while the latter only matches at the left margin. I changed it.
  +
Please fix it if my changes broke anything. [[User:JohnBeckett|JohnBeckett]] 10:25, September 5, 2009 (UTC)

Latest revision as of 06:41, July 13, 2012

Tip 1631 Printable Monobook Previous Next

created 2009 · complexity basic · version 7.0


Tips relating to the Clojure language will be given here.

Commenting and uncommentingEdit

The comment leader for Clojure is the ; character. This snippet allows easy commenting and uncommenting of lines.

map <Leader>. :call ClojureCommentUncomment()<CR>
function! ClojureCommentUncomment()
  let search_saved = @/
  if getline('.') =~ '^;'
    s/^;//  " remove ';' at beginning of line
  else
    s/^/;/  " insert ';' at beginning of line
  endif
  let @/ = search_saved
endfunction

Typing \. (assuming the default backslash leader) will remove ; from the beginning of the current line if it is present (uncomment), or will insert it otherwise (comment). You could make this more restrictive by only adding the mapping for files of the Clojure filetype, or by adding it to ~/.vim/after/ftplugin/clojure.vim.

ReferencesEdit

CommentsEdit

 TO DO 

  • May need some information similar to VimTip1565 for making Clojure a known filetype.
  • The tip used '\;' but the backslash is redundant (I removed it).
  • The tip had if getline('.') =~ '\;' but s/^;//. The former matches ; anywhere in the line, while the latter only matches at the left margin. I changed it.

Please fix it if my changes broke anything. JohnBeckett 10:25, September 5, 2009 (UTC)

Community content is available under CC-BY-SA unless otherwise noted.