Skip to content

hookCode quality

Ensure Final Newline

A Claude Code hook that makes sure each edited text file ends with exactly one newline, matching the line endings the file already uses.

npx workspacetemplates add ensure-final-newline --tool claude-code

Writes 1 file to .claude/

.claude/settings.jsonmerge into your file16 lines · 958 BFile
{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "hooks": [
          {
            "type": "command",
            "command": "node -e \"process.on('uncaughtException',()=>process.exit(0));let d='';process.stdin.on('data',c=>d+=c).on('end',()=>{let j={};try{j=JSON.parse(d)||{}}catch(e){process.exit(0)}const i=j.tool_input||{};const f=String(i.file_path||'');const fs=require('fs');const rd=()=>{try{return fs.readFileSync(f,'utf8')}catch(e){return null}};const e=require('path').extname(f).toLowerCase();if(['.diff','.patch'].includes(e)||f.includes('.min.'))process.exit(0);const t=rd();if(t===null||t.includes(String.fromCharCode(0)))process.exit(0);let k=t.length;while(k>0&&(t[k-1]==='\\n'||t[k-1]==='\\r'))k--;if(k===0)process.exit(0);const o=t.slice(0,k)+(t.includes('\\r\\n')?'\\r\\n':'\\n');if(o!==t)fs.writeFileSync(f,o);process.exit(0)})\"",
            "timeout": 15
          }
        ]
      }
    ]
  }
}

What it does

Many tools complain when a file has no newline at the end, and extra blank lines at the end show up in review. This PostToolUse hook fixes both after every Edit or Write.

It keeps CRLF for files that already use it, leaves empty and binary files alone, and skips diff, patch and minified files.

Example

you › Create a .gitignore for a Node project.

agent › The file was saved without a final newline. The hook adds one so the diff shows a clean last line.

Questions

What if my file uses Windows line endings?
If the file already contains CRLF pairs the added ending is CRLF too. Otherwise a plain newline is used.
Does it change the middle of a file?
No. It only trims blank lines after the last real content and adds one ending.
  • Block Generated File Edits

    HookCode quality

    Free
  • Block Large Writes

    HookCode quality

    Free
  • Block Vendor Edits

    HookCode quality

    Free
  • Format After Edit

    HookCode quality

    Free