6

For new files, Sublime Text changes the file name to the first line of the file:

enter image description here

Is it possible to achieve this on Vim?

EvergreenTree
  • 8,180
  • 2
  • 37
  • 59
adelarsq
  • 604
  • 3
  • 13

1 Answers1

5

I don't use Sublime Text, but something like this sounds like what you want:

function! SetFileNameToFirstLine()
  silent execute "file ".fnameescape(getline(1))
endfunction

let s:set_file_name_to_first_line_active = 0
function! ToggleSetFileNameToFirstLine()
  if s:set_file_name_to_first_line_active
    silent! augroup! SFNTFL
    echo "SetFileNameToFirstLine disabled"
  else
    augroup SFNTFL
      au! InsertLeave,CursorHold,CursorHoldI <buffer> call SetFileNameToFirstLine()
    augroup END
    call SetFileNameToFirstLine()
    echo "SetFileNameToFirstLine enabled"
  endif
  let s:set_file_name_to_first_line_active = !s:set_file_name_to_first_line_active
endfunction

nnoremap <F9> :call ToggleSetFileNameToFirstLine()<CR>

F9 will start changing your filename to the first line. Hit it again to disable this behaviour. (You can make it default, but I really recommend against it...)

Amadan
  • 724
  • 6
  • 12